forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
127 lines (107 loc) · 3.59 KB
/
helpers_test.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
package gpio
import (
"fmt"
"sync"
"gobot.io/x/gobot/v2"
)
type gpioTestBareAdaptor struct{}
func (t *gpioTestBareAdaptor) Connect() (err error) { return }
func (t *gpioTestBareAdaptor) Finalize() (err error) { return }
func (t *gpioTestBareAdaptor) Name() string { return "" }
func (t *gpioTestBareAdaptor) SetName(n string) {}
type digitalPinMock struct {
writeFunc func(val int) (err error)
}
type gpioTestAdaptor struct {
name string
pinMap map[string]gobot.DigitalPinner
port string
mtx sync.Mutex
digitalReadFunc func(ping string) (val int, err error)
digitalWriteFunc func(pin string, val byte) (err error)
pwmWriteFunc func(pin string, val byte) (err error)
servoWriteFunc func(pin string, val byte) (err error)
}
func newGpioTestAdaptor() *gpioTestAdaptor {
t := gpioTestAdaptor{
name: "gpio_test_adaptor",
pinMap: make(map[string]gobot.DigitalPinner),
port: "/dev/null",
digitalWriteFunc: func(pin string, val byte) (err error) {
return nil
},
servoWriteFunc: func(pin string, val byte) (err error) {
return nil
},
pwmWriteFunc: func(pin string, val byte) (err error) {
return nil
},
digitalReadFunc: func(pin string) (val int, err error) {
return 1, nil
},
}
return &t
}
// DigitalRead capabilities (interface DigitalReader)
func (t *gpioTestAdaptor) DigitalRead(pin string) (val int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.digitalReadFunc(pin)
}
// DigitalWrite capabilities (interface DigitalWriter)
func (t *gpioTestAdaptor) DigitalWrite(pin string, val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.digitalWriteFunc(pin, val)
}
// PwmWrite capabilities (interface PwmWriter)
func (t *gpioTestAdaptor) PwmWrite(pin string, val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.pwmWriteFunc(pin, val)
}
// ServoWrite capabilities (interface ServoWriter)
func (t *gpioTestAdaptor) ServoWrite(pin string, val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.servoWriteFunc(pin, val)
}
func (t *gpioTestAdaptor) Connect() (err error) { return }
func (t *gpioTestAdaptor) Finalize() (err error) { return }
func (t *gpioTestAdaptor) Name() string { return t.name }
func (t *gpioTestAdaptor) SetName(n string) { t.name = n }
func (t *gpioTestAdaptor) Port() string { return t.port }
// DigitalPin (interface DigitalPinnerProvider) return a pin object
func (t *gpioTestAdaptor) DigitalPin(id string) (gobot.DigitalPinner, error) {
if pin, ok := t.pinMap[id]; ok {
return pin, nil
}
return nil, fmt.Errorf("pin '%s' not found in '%s'", id, t.name)
}
// ApplyOptions (interface DigitalPinOptionApplier by DigitalPinner) apply all given options to the pin immediately
func (d *digitalPinMock) ApplyOptions(options ...func(gobot.DigitalPinOptioner) bool) error {
return nil
}
// Export (interface DigitalPinner) exports the pin for use by the adaptor
func (d *digitalPinMock) Export() error {
return nil
}
// Unexport (interface DigitalPinner) releases the pin from the adaptor, so it is free for the operating system
func (d *digitalPinMock) Unexport() error {
return nil
}
// Read (interface DigitalPinner) reads the current value of the pin
func (d *digitalPinMock) Read() (n int, err error) {
return 0, err
}
// Write (interface DigitalPinner) writes to the pin
func (d *digitalPinMock) Write(b int) error {
return d.writeFunc(b)
}
func (t *gpioTestAdaptor) addDigitalPin(id string) *digitalPinMock {
dpm := &digitalPinMock{
writeFunc: func(val int) (err error) { return nil },
}
t.pinMap[id] = dpm
return dpm
}