forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssd1306_driver_test.go
289 lines (237 loc) · 8.18 KB
/
ssd1306_driver_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
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
287
288
289
package i2c
import (
"errors"
"fmt"
"image"
"reflect"
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*SSD1306Driver)(nil)
func TestDisplayBuffer(t *testing.T) {
width := 128
height := 64
size := 1024 // (width*height) / 8
display := NewDisplayBuffer(width, height, 8)
if display.Size() != size {
t.Errorf("invalid Size() (%d, expected %d)",
display.Size(), size)
}
if len(display.buffer) != size {
t.Errorf("allocated buffer size invalid (%d, expected %d)",
len(display.buffer), size)
}
gobottest.Assert(t, display.buffer[0], byte(0))
gobottest.Assert(t, display.buffer[1], byte(0))
display.SetPixel(0, 0, 1)
display.SetPixel(1, 0, 1)
display.SetPixel(2, 0, 1)
display.SetPixel(0, 1, 1)
gobottest.Assert(t, display.buffer[0], byte(3))
gobottest.Assert(t, display.buffer[1], byte(1))
display.SetPixel(0, 1, 0)
gobottest.Assert(t, display.buffer[0], byte(1))
gobottest.Assert(t, display.buffer[1], byte(1))
}
// --------- HELPERS
func initTestSSD1306Driver(width, height int, externalVCC bool) (driver *SSD1306Driver) {
driver, _ = initTestSSD1306DriverWithStubbedAdaptor(width, height, externalVCC)
return
}
func initTestSSD1306DriverWithStubbedAdaptor(width, height int, externalVCC bool) (*SSD1306Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewSSD1306Driver(adaptor, WithSSD1306DisplayWidth(width), WithSSD1306DisplayHeight(height), WithSSD1306ExternalVCC(externalVCC)), adaptor
}
// --------- TESTS
func TestNewSSD1306Driver(t *testing.T) {
// Does it return a pointer to an instance of SSD1306Driver?
var bm interface{} = NewSSD1306Driver(newI2cTestAdaptor())
_, ok := bm.(*SSD1306Driver)
if !ok {
t.Errorf("new should have returned a *SSD1306Driver")
}
b := NewSSD1306Driver(newI2cTestAdaptor())
gobottest.Refute(t, b.Connection(), nil)
}
// Methods
func TestSSD1306DriverStartDefaul(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
gobottest.Assert(t, s.Start(), nil)
}
func TestSSD1306DriverStart128x32(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 32, false)
gobottest.Assert(t, s.Start(), nil)
}
func TestSSD1306DriverStart96x16(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor(96, 16, false)
gobottest.Assert(t, s.Start(), nil)
}
func TestSSD1306DriverStartExternalVCC(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 32, true)
gobottest.Assert(t, s.Start(), nil)
}
func TestSSD1306StartConnectError(t *testing.T) {
d, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestSSD1306StartSizeError(t *testing.T) {
d, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 54, false)
//adaptor.Testi2cConnectErr(false)
gobottest.Assert(t, d.Start(), errors.New("128x54 resolution is unsupported, supported resolutions: 128x64, 128x32, 96x16"))
}
func TestSSD1306DriverHalt(t *testing.T) {
s := initTestSSD1306Driver(128, 64, false)
gobottest.Assert(t, s.Halt(), nil)
}
// Test Name & SetName
func TestSSD1306DriverName(t *testing.T) {
s := initTestSSD1306Driver(96, 16, false)
gobottest.Assert(t, strings.HasPrefix(s.Name(), "SSD1306"), true)
s.SetName("Ole Oled")
gobottest.Assert(t, s.Name(), "Ole Oled")
}
func TestSSD1306DriverOptions(t *testing.T) {
s := NewSSD1306Driver(newI2cTestAdaptor(), WithBus(2), WithSSD1306DisplayHeight(32), WithSSD1306DisplayWidth(128))
gobottest.Assert(t, s.GetBusOrDefault(1), 2)
gobottest.Assert(t, s.displayHeight, 32)
gobottest.Assert(t, s.displayWidth, 128)
}
func TestSSD1306DriverDisplay(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor(96, 16, false)
s.Start()
gobottest.Assert(t, s.Display(), nil)
}
func TestSSD1306DriverShowImage(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
gobottest.Assert(t, s.ShowImage(img), errors.New("image must match display width and height: 128x64"))
img = image.NewRGBA(image.Rect(0, 0, 128, 64))
gobottest.Assert(t, s.ShowImage(img), nil)
}
func TestSSD1306DriverCommand(t *testing.T) {
s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
adaptor.i2cWriteImpl = func(got []byte) (int, error) {
expected := []byte{0x80, 0xFF}
if !reflect.DeepEqual(got, expected) {
t.Logf("sequence error, got %+v, expected %+v", got, expected)
return 0, fmt.Errorf("oops")
}
return 0, nil
}
err := s.command(0xFF)
gobottest.Assert(t, err, nil)
}
func TestSSD1306DriverCommands(t *testing.T) {
s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
adaptor.i2cWriteImpl = func(got []byte) (int, error) {
expected := []byte{0x80, 0x00, 0x80, 0xFF}
if !reflect.DeepEqual(got, expected) {
t.Logf("sequence error, got %+v, expected %+v", got, expected)
return 0, fmt.Errorf("oops")
}
return 0, nil
}
err := s.commands([]byte{0x00, 0xFF})
gobottest.Assert(t, err, nil)
}
func TestSSD1306DriverOn(t *testing.T) {
s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
adaptor.i2cWriteImpl = func(got []byte) (int, error) {
expected := []byte{0x80, ssd1306SetDisplayOn}
if !reflect.DeepEqual(got, expected) {
t.Logf("sequence error, got %+v, expected %+v", got, expected)
return 0, fmt.Errorf("oops")
}
return 0, nil
}
err := s.On()
gobottest.Assert(t, err, nil)
}
func TestSSD1306DriverOff(t *testing.T) {
s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
adaptor.i2cWriteImpl = func(got []byte) (int, error) {
expected := []byte{0x80, ssd1306SetDisplayOff}
if !reflect.DeepEqual(got, expected) {
t.Logf("sequence error, got %+v, expected %+v", got, expected)
return 0, fmt.Errorf("oops")
}
return 0, nil
}
err := s.Off()
gobottest.Assert(t, err, nil)
}
func TestSSD1306Reset(t *testing.T) {
s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
adaptor.i2cWriteImpl = func(got []byte) (int, error) {
expectedOff := []byte{0x80, ssd1306SetDisplayOff}
expectedOn := []byte{0x80, ssd1306SetDisplayOn}
if !reflect.DeepEqual(got, expectedOff) && !reflect.DeepEqual(got, expectedOn) {
t.Logf("sequence error, got %+v, expected: %+v or %+v", got, expectedOff, expectedOn)
return 0, fmt.Errorf("oops")
}
return 0, nil
}
err := s.Reset()
gobottest.Assert(t, err, nil)
}
// COMMANDS
func TestSSD1306DriverCommandsDisplay(t *testing.T) {
s := initTestSSD1306Driver(128, 64, false)
s.Start()
result := s.Command("Display")(map[string]interface{}{})
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestSSD1306DriverCommandsOn(t *testing.T) {
s := initTestSSD1306Driver(128, 64, false)
s.Start()
result := s.Command("On")(map[string]interface{}{})
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestSSD1306DriverCommandsOff(t *testing.T) {
s := initTestSSD1306Driver(128, 64, false)
s.Start()
result := s.Command("Off")(map[string]interface{}{})
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestSSD1306DriverCommandsClear(t *testing.T) {
s := initTestSSD1306Driver(128, 64, false)
s.Start()
result := s.Command("Clear")(map[string]interface{}{})
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestSSD1306DriverCommandsSetContrast(t *testing.T) {
s, adaptor := initTestSSD1306DriverWithStubbedAdaptor(128, 64, false)
s.Start()
adaptor.i2cWriteImpl = func(got []byte) (int, error) {
expected := []byte{0x80, ssd1306SetContrast, 0x80, 0x10}
if !reflect.DeepEqual(got, expected) {
t.Logf("sequence error, got %+v, expected %+v", got, expected)
return 0, fmt.Errorf("oops")
}
return 0, nil
}
result := s.Command("SetContrast")(map[string]interface{}{
"contrast": byte(0x10),
})
gobottest.Assert(t, result.(map[string]interface{})["err"], nil)
}
func TestSSD1306DriverCommandsSet(t *testing.T) {
s := initTestSSD1306Driver(128, 64, false)
s.Start()
gobottest.Assert(t, s.buffer.buffer[0], byte(0))
s.Command("Set")(map[string]interface{}{
"x": int(0),
"y": int(0),
"c": int(1),
})
gobottest.Assert(t, s.buffer.buffer[0], byte(1))
}