-
Notifications
You must be signed in to change notification settings - Fork 39
/
driver_mock.go
286 lines (225 loc) · 6.44 KB
/
driver_mock.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package hwio
// A mock driver used for unit testing.
import (
// "errors"
"fmt"
)
type testDriverPin struct {
names []string
modules []string
extra string
}
type testDriverPinMap map[Pin]*testDriverPin
type TestDriver struct {
pinDefs []*testDriverPin
modules map[string]Module
verbose bool
}
func (d *TestDriver) Init() error {
d.createPinData()
d.initialiseModules()
return nil
}
func (d *TestDriver) Close() {
}
func (d *TestDriver) MatchesHardwareConfig() bool {
return true
}
// func (d *TestDriver) SetVerbosity(verbose bool) {
// d.verbose = verbose
// }
// // Mock records the pin mode being assigned.
// func (d *TestDriver) PinMode(pin Pin, mode PinIOMode) (e error) {
// if d.verbose {
// fmt.Printf("PinMode(%d, %s)\n", pin, mode.String())
// }
// m := d.getPinModes()
// m[pin] = mode
// return nil
// }
// // Mock emulates DigitalWrite by writing the value to pinValues.
// func (d *TestDriver) DigitalWrite(pin Pin, value int) (e error) {
// if d.verbose {
// fmt.Printf("DigitalWrite(%d, %d)\n", pin, value)
// }
// m := d.getPinValues()
// m[pin] = value
// return nil
// }
// func (d *TestDriver) DigitalRead(pin Pin) (value int, e error) {
// m := d.getPinValues()
// return m[pin], nil
// }
// func (d *TestDriver) AnalogWrite(pin Pin, value int) (e error) {
// // just the same as DigitalWrite, store value in the pinValues map
// return DigitalWrite(pin, value)
// }
// func (d *TestDriver) AnalogRead(pin Pin) (value int, e error) {
// if pin == 6 {
// return 1, nil
// }
// if pin == 7 {
// return 1000, nil
// }
// return 0, errors.New("analog read got error")
// }
// // Mock has a fixed set of hardcoded pins with different capabilities
func (d *TestDriver) PinMap() HardwarePinMap {
result := make(HardwarePinMap)
for i, hw := range d.pinDefs {
result.Add(Pin(i), hw.names, hw.modules)
}
return result
}
// // Getter that gets the pinModes map on demand, and creating it on first
// // instance.
// func (d *TestDriver) getPinModes() map[Pin]PinIOMode {
// if d.pinModes == nil {
// d.pinModes = make(map[Pin]PinIOMode)
// }
// return d.pinModes
// }
// func (d *TestDriver) getPinValues() map[Pin]int {
// if d.pinValues == nil {
// d.pinValues = make(map[Pin]int)
// }
// return d.pinValues
// }
// func (d *TestDriver) MockGetPinMode(pin Pin) PinIOMode {
// m := d.getPinModes()
// return m[pin]
// }
// func (d *TestDriver) MockGetPinValue(pin Pin) int {
// m := d.getPinValues()
// return m[pin]
// }
// func (d *TestDriver) MockSetPinValue(pin Pin, value int) {
// m := d.getPinValues()
// m[pin] = value
// }
func (d *TestDriver) GetModules() map[string]Module {
return d.modules
}
func (d *TestDriver) createPinData() {
d.pinDefs = []*testDriverPin{
d.makeTestPin([]string{"P1", "gpio1"}, []string{"gpio"}, "1"),
d.makeTestPin([]string{"P2", "gpio2"}, []string{"gpio"}, "2"),
d.makeTestPin([]string{"P3", "gpio3"}, []string{"gpio"}, "3"),
d.makeTestPin([]string{"P4", "gpio4"}, []string{"gpio"}, "4"),
d.makeTestPin([]string{"P5", "gpio5"}, []string{"gpio"}, "5"),
d.makeTestPin([]string{"P6", "gpio6"}, []string{"gpio"}, "6"),
d.makeTestPin([]string{"P7", "gpio7"}, []string{"gpio"}, "7"),
d.makeTestPin([]string{"P8", "gpio8"}, []string{"gpio"}, "8"),
d.makeTestPin([]string{"P9", "gpio9"}, []string{"gpio"}, "9"),
d.makeTestPin([]string{"P10", "gpio10"}, []string{"gpio"}, "10"),
d.makeTestPin([]string{"P11", "ain4"}, []string{"analog"}, "11"),
d.makeTestPin([]string{"P12", "ain6"}, []string{"analog"}, "12"),
}
}
func (d *TestDriver) makeTestPin(names []string, modules []string, extra string) *testDriverPin {
result := &testDriverPin{names, modules, extra}
return result
}
func (d *TestDriver) initialiseModules() {
d.modules = make(map[string]Module)
gpio := newTestGPIOModule("gpio")
gpio.SetOptions(d.getModuleOptions("gpio"))
analog := newTestAnalogModule("analog")
analog.SetOptions(d.getModuleOptions("analog"))
// i2c1 := NewDTI2CModule("i2c1")
d.modules["gpio"] = gpio
d.modules["analog"] = analog
// d.modules["i2c1"] = i2c1
}
func (d *TestDriver) getModuleOptions(module string) map[string]interface{} {
result := make(map[string]interface{})
pins := make(testDriverPinMap)
for i, hw := range d.pinDefs {
if hw.modules[0] == module {
pins[Pin(i)] = hw
}
}
result["pins"] = pins
return result
}
// Mock module to replicate GPIO behaviour
type testGPIOModule struct {
name string
pinDefs testDriverPinMap
pinModes map[Pin]PinIOMode
// this simulates actual pin values. DigitalWrite ends up settin
pinValues map[Pin]int
}
func newTestGPIOModule(name string) *testGPIOModule {
result := &testGPIOModule{name: name}
result.pinModes = make(map[Pin]PinIOMode)
result.pinValues = make(map[Pin]int)
return result
}
func (module *testGPIOModule) SetOptions(map[string]interface{}) error {
return nil
}
func (module *testGPIOModule) Enable() error {
return nil
}
func (module *testGPIOModule) Disable() error {
return nil
}
func (module *testGPIOModule) GetName() string {
return module.name
}
func (module *testGPIOModule) PinMode(pin Pin, mode PinIOMode) error {
module.pinModes[pin] = mode
return nil
}
func (module *testGPIOModule) DigitalWrite(pin Pin, value int) error {
if module.pinModes[pin] == 0 {
return fmt.Errorf("Pin %d has not had mode set", pin)
}
module.pinValues[pin] = value
return nil
}
func (module *testGPIOModule) DigitalRead(pin Pin) (int, error) {
return module.pinValues[pin], nil
}
func (module *testGPIOModule) ClosePin(pin Pin) error {
return nil
}
func (module *testGPIOModule) MockGetPinMode(pin Pin) PinIOMode {
return module.pinModes[pin]
}
func (module *testGPIOModule) MockGetPinValue(pin Pin) int {
return module.pinValues[pin]
}
func (module *testGPIOModule) MockSetPinValue(pin Pin, value int) {
module.pinValues[pin] = value
}
// Mock module to replicate analog module behaviour.
type testAnalogModule struct {
name string
pinDefs testDriverPinMap
}
func newTestAnalogModule(name string) *testAnalogModule {
return &testAnalogModule{name: name}
}
func (module *testAnalogModule) SetOptions(map[string]interface{}) error {
return nil
}
func (module *testAnalogModule) Enable() error {
return nil
}
func (module *testAnalogModule) Disable() error {
return nil
}
func (module *testAnalogModule) GetName() string {
return module.name
}
func (module *testAnalogModule) AnalogRead(pin Pin) (result int, e error) {
if pin == 10 {
return 1, nil
}
if pin == 11 {
return 1000, nil
}
return 0, nil
}