forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
179 lines (157 loc) · 3.84 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
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
package i2c
import (
"errors"
"fmt"
"sync"
)
var rgb = map[string]interface{}{
"red": 1.0,
"green": 1.0,
"blue": 1.0,
}
func castColor(color string) byte {
return byte(rgb[color].(float64))
}
var red = castColor("red")
var green = castColor("green")
var blue = castColor("blue")
type i2cTestAdaptor struct {
name string
written []byte
mtx sync.Mutex
i2cConnectErr bool
i2cReadImpl func([]byte) (int, error)
i2cWriteImpl func([]byte) (int, error)
}
func (t *i2cTestAdaptor) Testi2cConnectErr(val bool) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.i2cConnectErr = val
}
func (t *i2cTestAdaptor) Testi2cReadImpl(f func([]byte) (int, error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.i2cReadImpl = f
}
func (t *i2cTestAdaptor) Testi2cWriteImpl(f func([]byte) (int, error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.i2cWriteImpl = f
}
func (t *i2cTestAdaptor) Read(b []byte) (count int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.i2cReadImpl(b)
}
func (t *i2cTestAdaptor) Write(b []byte) (count int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.written = append(t.written, b...)
return t.i2cWriteImpl(b)
}
func (t *i2cTestAdaptor) Close() error {
return nil
}
func (t *i2cTestAdaptor) ReadByte() (val byte, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
bytes := []byte{0}
bytesRead, err := t.i2cReadImpl(bytes)
if err != nil {
return 0, err
}
if bytesRead != 1 {
return 0, fmt.Errorf("Buffer underrun")
}
val = bytes[0]
return
}
func (t *i2cTestAdaptor) ReadByteData(reg uint8) (val uint8, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
bytes := []byte{0}
bytesRead, err := t.i2cReadImpl(bytes)
if err != nil {
return 0, err
}
if bytesRead != 1 {
return 0, fmt.Errorf("Buffer underrun")
}
val = bytes[0]
return
}
func (t *i2cTestAdaptor) ReadWordData(reg uint8) (val uint16, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
bytes := []byte{0, 0}
bytesRead, err := t.i2cReadImpl(bytes)
if err != nil {
return 0, err
}
if bytesRead != 2 {
return 0, fmt.Errorf("Buffer underrun")
}
low, high := bytes[0], bytes[1]
return (uint16(high) << 8) | uint16(low), err
}
func (t *i2cTestAdaptor) WriteByte(val byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.written = append(t.written, val)
bytes := []byte{val}
_, err = t.i2cWriteImpl(bytes)
return
}
func (t *i2cTestAdaptor) WriteByteData(reg uint8, val uint8) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.written = append(t.written, reg)
t.written = append(t.written, val)
bytes := []byte{val}
_, err = t.i2cWriteImpl(bytes)
return
}
func (t *i2cTestAdaptor) WriteWordData(reg uint8, val uint16) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.written = append(t.written, reg)
low := uint8(val & 0xff)
high := uint8((val >> 8) & 0xff)
t.written = append(t.written, low)
t.written = append(t.written, high)
bytes := []byte{low, high}
_, err = t.i2cWriteImpl(bytes)
return
}
func (t *i2cTestAdaptor) WriteBlockData(reg uint8, b []byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.written = append(t.written, reg)
t.written = append(t.written, b...)
_, err = t.i2cWriteImpl(b)
return
}
func (t *i2cTestAdaptor) GetConnection( /* address */ int /* bus */, int) (connection Connection, err error) {
if t.i2cConnectErr {
return nil, errors.New("Invalid i2c connection")
}
return t, nil
}
func (t *i2cTestAdaptor) GetDefaultBus() int {
return 0
}
func (t *i2cTestAdaptor) Name() string { return t.name }
func (t *i2cTestAdaptor) SetName(n string) { t.name = n }
func (t *i2cTestAdaptor) Connect() (err error) { return }
func (t *i2cTestAdaptor) Finalize() (err error) { return }
func newI2cTestAdaptor() *i2cTestAdaptor {
return &i2cTestAdaptor{
i2cConnectErr: false,
i2cReadImpl: func([]byte) (int, error) {
return 0, nil
},
i2cWriteImpl: func([]byte) (int, error) {
return 0, nil
},
}
}