forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_config.go
72 lines (58 loc) · 1.65 KB
/
i2c_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
70
71
72
package i2c
type i2cConfig struct {
bus int
address int
}
// Config is the interface which describes how a Driver can specify
// optional I2C params such as which I2C bus it wants to use.
type Config interface {
// WithBus sets which bus to use
WithBus(bus int)
// GetBusOrDefault gets which bus to use
GetBusOrDefault(def int) int
// WithAddress sets which address to use
WithAddress(address int)
// GetAddressOrDefault gets which address to use
GetAddressOrDefault(def int) int
}
// NewConfig returns a new I2c Config.
func NewConfig() Config {
return &i2cConfig{bus: BusNotInitialized, address: AddressNotInitialized}
}
// WithBus sets preferred bus to use.
func (i *i2cConfig) WithBus(bus int) {
i.bus = bus
}
// GetBusOrDefault returns which bus to use, either the one set using WithBus(),
// or the default value which is passed in as the one param.
func (i *i2cConfig) GetBusOrDefault(d int) int {
if i.bus == BusNotInitialized {
return d
}
return i.bus
}
// WithBus sets which bus to use as a optional param.
func WithBus(bus int) func(Config) {
return func(i Config) {
i.WithBus(bus)
}
}
// WithAddress sets which address to use.
func (i *i2cConfig) WithAddress(address int) {
i.address = address
}
// GetAddressOrDefault returns which address to use, either
// the one set using WithBus(), or the default value which
// is passed in as the param.
func (i *i2cConfig) GetAddressOrDefault(a int) int {
if i.address == AddressNotInitialized {
return a
}
return i.address
}
// WithAddress sets which address to use as a optional param.
func WithAddress(address int) func(Config) {
return func(i Config) {
i.WithAddress(address)
}
}