-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
hd44780_driver_test.go
282 lines (241 loc) · 7.11 KB
/
hd44780_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
package gpio
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/aio"
)
var _ gobot.Driver = (*HD44780Driver)(nil)
func initTestHD44780Driver() *HD44780Driver {
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
if err := d.Start(); err != nil {
panic(err)
}
return d
}
func initTestHD44780Driver4BitModeWithStubbedAdaptor() (*HD44780Driver, *gpioTestAdaptor) {
a := newGpioTestAdaptor()
dataPins := HD44780DataPin{
D4: "22",
D5: "18",
D6: "16",
D7: "12",
}
return NewHD44780Driver(a, 2, 16, HD44780_4BITMODE, "13", "15", dataPins), a
}
func initTestHD44780Driver8BitModeWithStubbedAdaptor() (*HD44780Driver, *gpioTestAdaptor) {
a := newGpioTestAdaptor()
dataPins := HD44780DataPin{
D0: "31",
D1: "33",
D2: "35",
D3: "37",
D4: "22",
D5: "18",
D6: "16",
D7: "12",
}
return NewHD44780Driver(a, 2, 16, HD44780_8BITMODE, "13", "15", dataPins), a
}
func TestNewHD44780Driver(t *testing.T) {
// arrange
a := newGpioTestAdaptor()
dataPins := HD44780DataPin{
D4: "22",
D5: "18",
D6: "16",
D7: "12",
}
// act
d := NewHD44780Driver(a, 16, 2, HD44780_4BITMODE, "13", "15", dataPins)
// assert
assert.IsType(t, &HD44780Driver{}, d)
// assert: gpio.driver attributes
require.NotNil(t, d.driver)
assert.True(t, strings.HasPrefix(d.driverCfg.name, "HD44780"))
assert.Equal(t, a, d.connection)
assert.NotNil(t, d.afterStart)
assert.NotNil(t, d.beforeHalt)
assert.NotNil(t, d.Commander)
assert.NotNil(t, d.mutex)
// assert: driver specific attributes
assert.Equal(t, "", d.hd44780Cfg.pinRW)
assert.Equal(t, 16, d.cols)
assert.Equal(t, 2, d.rows)
assert.Equal(t, HD44780_4BITMODE, d.busMode)
assert.NotNil(t, d.pinRS)
assert.NotNil(t, d.pinEN)
assert.Nil(t, d.pinRW) // will be set optionally
assert.NotNil(t, d.pinRS)
assert.Equal(t, [4]int{0, 64, 16, 80}, d.rowOffsets)
assert.Len(t, d.pinDataBits, 4)
for _, b := range d.pinDataBits {
assert.NotNil(t, b)
}
assert.Equal(t, 0, d.displayCtrl)
assert.Equal(t, 0, d.displayFunc)
assert.Equal(t, 0, d.displayMode)
}
func TestNewHD44780Driver_options(t *testing.T) {
// This is a general test, that options are applied in constructor by using the common WithName() option, least one
// option of this driver and one of another driver (which should lead to panic). Further tests for options can also
// be done by call of "WithOption(val).apply(cfg)".
// arrange
const (
myName = "LCD output"
pinRW = "3"
)
dataPins := HD44780DataPin{
D4: "22",
D5: "18",
D6: "16",
D7: "12",
}
panicFunc := func() {
NewHD44780Driver(newGpioTestAdaptor(), 16, 2, HD44780_4BITMODE, "1", "2", dataPins, WithName("crazy"),
aio.WithActuatorScaler(func(float64) int { return 0 }))
}
// act
d := NewHD44780Driver(newGpioTestAdaptor(), 16, 2, HD44780_4BITMODE, "1", "2", dataPins, WithName(myName),
WithHD44780RWPin(pinRW))
// assert
assert.Equal(t, pinRW, d.hd44780Cfg.pinRW)
assert.Equal(t, myName, d.Name())
assert.PanicsWithValue(t, "'scaler option for analog actuators' can not be applied on 'crazy'", panicFunc)
}
func TestHD44780Start(t *testing.T) {
// arrange
d, _ := initTestHD44780Driver4BitModeWithStubbedAdaptor()
// act & assert: tests also initialize()
require.NoError(t, d.Start())
}
func TestHD44780StartError(t *testing.T) {
a := newGpioTestAdaptor()
var pins HD44780DataPin
var d *HD44780Driver
pins = HD44780DataPin{
D4: "22",
D5: "18",
D6: "16",
D7: "",
}
d = NewHD44780Driver(a, 2, 16, HD44780_4BITMODE, "13", "15", pins)
require.EqualError(t, d.Start(), "Initialization error")
pins = HD44780DataPin{
D0: "31",
D1: "33",
D2: "35",
D3: "37",
D4: "22",
D5: "18",
D6: "16",
D7: "",
}
d = NewHD44780Driver(a, 2, 16, HD44780_8BITMODE, "13", "15", pins)
require.EqualError(t, d.Start(), "Initialization error")
}
func TestHD44780Write(t *testing.T) {
var d *HD44780Driver
d, _ = initTestHD44780Driver4BitModeWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Write("hello gobot"))
d, _ = initTestHD44780Driver8BitModeWithStubbedAdaptor()
_ = d.Start()
require.NoError(t, d.Write("hello gobot"))
}
func TestHD44780WriteError(t *testing.T) {
var d *HD44780Driver
var a *gpioTestAdaptor
d, a = initTestHD44780Driver4BitModeWithStubbedAdaptor()
a.digitalWriteFunc = func(string, byte) error {
return errors.New("write error")
}
_ = d.Start()
require.EqualError(t, d.Write("hello gobot"), "write error")
d, a = initTestHD44780Driver8BitModeWithStubbedAdaptor()
a.digitalWriteFunc = func(string, byte) error {
return errors.New("write error")
}
_ = d.Start()
require.EqualError(t, d.Write("hello gobot"), "write error")
}
func TestHD44780Clear(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Clear())
}
func TestHD44780Home(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Home())
}
func TestHD44780SetCursor(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.SetCursor(0, 3))
}
func TestHD44780SetCursorInvalid(t *testing.T) {
d := initTestHD44780Driver()
require.EqualError(t, d.SetCursor(-1, 3), "Invalid position value (-1, 3), range (1, 15)")
require.EqualError(t, d.SetCursor(2, 3), "Invalid position value (2, 3), range (1, 15)")
require.EqualError(t, d.SetCursor(0, -1), "Invalid position value (0, -1), range (1, 15)")
require.EqualError(t, d.SetCursor(0, 16), "Invalid position value (0, 16), range (1, 15)")
}
func TestHD44780DisplayOn(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Display(true))
}
func TestHD44780DisplayOff(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Display(false))
}
func TestHD44780CursorOn(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Cursor(true))
}
func TestHD44780CursorOff(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Cursor(false))
}
func TestHD44780BlinkOn(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Blink(true))
}
func TestHD44780BlinkOff(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.Blink(false))
}
func TestHD44780ScrollLeft(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.ScrollLeft())
}
func TestHD44780ScrollRight(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.ScrollRight())
}
func TestHD44780LeftToRight(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.LeftToRight())
}
func TestHD44780RightToLeft(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.RightToLeft())
}
func TestHD44780SendCommand(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.SendCommand(0x33))
}
func TestHD44780WriteChar(t *testing.T) {
d := initTestHD44780Driver()
require.NoError(t, d.WriteChar(0x41))
}
func TestHD44780CreateChar(t *testing.T) {
d := initTestHD44780Driver()
charMap := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
require.NoError(t, d.CreateChar(0, charMap))
}
func TestHD44780CreateCharError(t *testing.T) {
d := initTestHD44780Driver()
charMap := [8]byte{1, 2, 3, 4, 5, 6, 7, 8}
require.EqualError(t, d.CreateChar(8, charMap), "can't set a custom character at a position greater than 7")
}