-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathspi_config.go
125 lines (108 loc) · 2.87 KB
/
spi_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package spi
type spiConfig struct {
bus int
chip int
mode int
bits int
speed int64
}
// NewConfig returns a new SPI Config.
func NewConfig() Config {
return &spiConfig{
bus: NotInitialized,
chip: NotInitialized,
mode: NotInitialized,
bits: NotInitialized,
speed: NotInitialized,
}
}
// WithBusNumber sets which bus to use as a optional param.
func WithBusNumber(busNum int) func(Config) {
return func(s Config) {
s.SetBusNumber(busNum)
}
}
// WithChipNumber sets which chip to use as a optional param.
func WithChipNumber(chipNum int) func(Config) {
return func(s Config) {
s.SetChipNumber(chipNum)
}
}
// WithMode sets which mode to use as a optional param.
func WithMode(mode int) func(Config) {
return func(s Config) {
s.SetMode(mode)
}
}
// WithBitCount sets how many bits to use as a optional param.
func WithBitCount(bitCount int) func(Config) {
return func(s Config) {
s.SetBitCount(bitCount)
}
}
// WithSpeed sets what speed to use as a optional param.
func WithSpeed(speed int64) func(Config) {
return func(s Config) {
s.SetSpeed(speed)
}
}
// SetBusNumber sets preferred bus to use.
func (s *spiConfig) SetBusNumber(bus int) {
s.bus = bus
}
// GetBusNumberOrDefault returns which bus to use, either the one set using WithBus(),
// or the default value which is passed in as the one param.
func (s *spiConfig) GetBusNumberOrDefault(d int) int {
if s.bus == NotInitialized {
return d
}
return s.bus
}
// SetChipNumber sets preferred chip to use.
func (s *spiConfig) SetChipNumber(chip int) {
s.chip = chip
}
// GetChipNumberOrDefault returns which chip to use, either the one set using WithChip(),
// or the default value which is passed in as the one param.
func (s *spiConfig) GetChipNumberOrDefault(d int) int {
if s.chip == NotInitialized {
return d
}
return s.chip
}
// SetMode sets SPI mode to use.
func (s *spiConfig) SetMode(mode int) {
s.mode = mode
}
// GetModeOrDefault returns which mode to use, either the one set using WithChip(),
// or the default value which is passed in as the one param.
func (s *spiConfig) GetModeOrDefault(d int) int {
if s.mode == NotInitialized {
return d
}
return s.mode
}
// SetBitCount sets how many SPI bits to use.
func (s *spiConfig) SetBitCount(bits int) {
s.bits = bits
}
// GetBitCountOrDefault returns how many to use, either the one set using WithBits(),
// or the default value which is passed in as the one param.
func (s *spiConfig) GetBitCountOrDefault(d int) int {
if s.bits == NotInitialized {
return d
}
return s.bits
}
// SetSpeed sets which SPI speed to use.
func (s *spiConfig) SetSpeed(speed int64) {
s.speed = speed
}
// GetSpeedOrDefault returns what speed to use, either the one set using WithSpeed(),
// or the default value which is passed in as the one param.
func (s *spiConfig) GetSpeedOrDefault(d int64) int64 {
if s.speed == NotInitialized {
return d
}
return s.speed
}