-
Notifications
You must be signed in to change notification settings - Fork 110
/
config.go
69 lines (61 loc) · 2.78 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package genericlinux
import (
"fmt"
"go.viam.com/rdk/components/board"
"go.viam.com/rdk/components/board/mcp3008helper"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
// A Config describes the configuration of a board and all of its connected parts.
type Config struct {
AnalogReaders []mcp3008helper.MCP3008AnalogConfig `json:"analogs,omitempty"`
DigitalInterrupts []board.DigitalInterruptConfig `json:"digital_interrupts,omitempty"`
}
// Validate ensures all parts of the config are valid.
func (conf *Config) Validate(path string) ([]string, error) {
for idx, c := range conf.AnalogReaders {
if err := c.Validate(fmt.Sprintf("%s.%s.%d", path, "analogs", idx)); err != nil {
return nil, err
}
}
for idx, c := range conf.DigitalInterrupts {
if err := c.Validate(fmt.Sprintf("%s.%s.%d", path, "digital_interrupts", idx)); err != nil {
return nil, err
}
}
return nil, nil
}
// LinuxBoardConfig is a struct containing absolutely everything a genericlinux board might need
// configured. It is a union of the configs for the customlinux boards and the genericlinux boards
// with static pin definitions, because those components all use the same underlying code but have
// different config types (e.g., only customlinux can change its pin definitions during
// reconfiguration). The LinuxBoardConfig struct is a unification of the two of them. Whenever we
// go through reconfiguration, we convert the provided config into a LinuxBoardConfig, and then
// reconfigure based on it.
type LinuxBoardConfig struct {
AnalogReaders []mcp3008helper.MCP3008AnalogConfig
DigitalInterrupts []board.DigitalInterruptConfig
GpioMappings map[string]GPIOBoardMapping
}
// ConfigConverter is a type synonym for a function to turn whatever config we get during
// reconfiguration into a LinuxBoardConfig, so that we can reconfigure based on that. We return a
// pointer to a LinuxBoardConfig instead of the struct itself so that we can return nil if we
// encounter an error.
type ConfigConverter = func(resource.Config, logging.Logger) (*LinuxBoardConfig, error)
// ConstPinDefs takes in a map from pin names to GPIOBoardMapping structs, and returns a
// ConfigConverter that will use these pin definitions in the underlying config. It is intended to
// be used for board components whose pin definitions are built into the RDK, such as the
// BeagleBone or Jetson boards.
func ConstPinDefs(gpioMappings map[string]GPIOBoardMapping) ConfigConverter {
return func(conf resource.Config, logger logging.Logger) (*LinuxBoardConfig, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
return &LinuxBoardConfig{
AnalogReaders: newConf.AnalogReaders,
DigitalInterrupts: newConf.DigitalInterrupts,
GpioMappings: gpioMappings,
}, nil
}
}