-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdio_test.go
227 lines (193 loc) · 5.04 KB
/
dio_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
// Copyright © 2017 Kent Gibson <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//
// Test suite for dio module.
//
// Tests use J8 pins 7 (mostly) and 15 and 16 (for looped tests)
//
package gpio_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/warthog618/gpio"
)
func setupDIO(t *testing.T) {
assert.Nil(t, gpio.Open())
}
func teardownDIO() {
gpio.Close()
}
func TestUninitialisedPanic(t *testing.T) {
assert.Panics(t, func() {
gpio.NewPin(gpio.J8p7)
})
}
func TestClosedPanic(t *testing.T) {
setupDIO(t)
teardownDIO()
assert.Panics(t, func() {
gpio.NewPin(gpio.J8p7)
})
}
func TestNew(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.MaxGPIOPin)
assert.Nil(t, pin)
}
func TestRead(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.J8p7)
// A basic read test - assuming the pin is input and pulled high
// which is the default state for this pin on a Pi.
assert.Equal(t, gpio.Input, pin.Mode())
// Assumes pin is initially pulled up and set as an input.
assert.Equal(t, gpio.High, pin.Read())
}
func TestMode(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.J8p7)
assert.Equal(t, gpio.Input, pin.Mode())
pin.SetMode(gpio.Output)
assert.Equal(t, gpio.Output, pin.Mode())
pin.SetMode(gpio.Input)
assert.Equal(t, gpio.Input, pin.Mode())
pin.Output()
assert.Equal(t, gpio.Output, pin.Mode())
pin.Input()
assert.Equal(t, gpio.Input, pin.Mode())
}
func TestPull(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.J8p7)
defer pin.PullUp()
// A basic read test - using the pull up/down to drive the pin.
assert.Equal(t, gpio.Input, pin.Mode())
pin.PullUp()
pullSettle := time.Microsecond
time.Sleep(pullSettle)
assert.Equal(t, gpio.High, pin.Read())
pin.PullDown()
time.Sleep(pullSettle)
assert.Equal(t, gpio.Low, pin.Read())
pin.SetPull(gpio.PullUp)
time.Sleep(pullSettle)
assert.Equal(t, gpio.High, pin.Read())
// no real way of testing this, but to trick coverage...
pin.PullNone()
}
func TestPin(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.J8p7)
assert.Equal(t, gpio.J8p7, pin.Pin())
pin = gpio.NewPin(gpio.J8p16)
assert.Equal(t, gpio.J8p16, pin.Pin())
}
func TestWrite(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.J8p7)
mode := pin.Mode()
assert.Equal(t, gpio.Input, mode)
defer pin.SetMode(gpio.Input)
pin.Write(gpio.Low)
pin.SetMode(gpio.Output)
assert.Equal(t, gpio.Output, pin.Mode())
assert.Equal(t, gpio.Low, pin.Read())
pin.Write(gpio.High)
assert.Equal(t, gpio.High, pin.Shadow())
assert.Equal(t, gpio.High, pin.Read())
pin.Write(gpio.Low)
assert.Equal(t, gpio.Low, pin.Shadow())
assert.Equal(t, gpio.Low, pin.Read())
pin.High()
assert.Equal(t, gpio.High, pin.Shadow())
assert.Equal(t, gpio.High, pin.Read())
pin.Low()
assert.Equal(t, gpio.Low, pin.Shadow())
assert.Equal(t, gpio.Low, pin.Read())
}
// Looped tests require a jumper across Raspberry Pi J8 pins 15 and 16.
func TestWriteLooped(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pinIn := gpio.NewPin(gpio.J8p15)
pinOut := gpio.NewPin(gpio.J8p16)
pinIn.SetMode(gpio.Input)
defer pinOut.SetMode(gpio.Input)
pinOut.Write(gpio.Low)
pinOut.SetMode(gpio.Output)
assert.Equal(t, gpio.Low, pinIn.Read())
pinOut.Write(gpio.High)
assert.Equal(t, gpio.High, pinIn.Read())
pinOut.Write(gpio.Low)
assert.Equal(t, gpio.Low, pinIn.Read())
}
func TestToggle(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pin := gpio.NewPin(gpio.J8p7)
defer pin.SetMode(gpio.Input)
pin.Write(gpio.Low)
pin.SetMode(gpio.Output)
assert.Equal(t, gpio.Output, pin.Mode())
assert.Equal(t, gpio.Low, pin.Read())
pin.Toggle()
assert.Equal(t, gpio.High, pin.Shadow())
assert.Equal(t, gpio.High, pin.Read())
pin.Toggle()
assert.Equal(t, gpio.Low, pin.Shadow())
assert.Equal(t, gpio.Low, pin.Read())
}
// Looped tests require a jumper across Raspberry Pi J8 pins 15 and 16.
func TestToggleLooped(t *testing.T) {
setupDIO(t)
defer teardownDIO()
pinIn := gpio.NewPin(gpio.J8p15)
pinOut := gpio.NewPin(gpio.J8p16)
pinIn.SetMode(gpio.Input)
defer pinOut.SetMode(gpio.Input)
pinOut.Write(gpio.Low)
pinOut.SetMode(gpio.Output)
assert.Equal(t, gpio.Output, pinOut.Mode())
assert.Equal(t, gpio.Low, pinOut.Read())
pinOut.Toggle()
assert.Equal(t, gpio.High, pinIn.Read())
pinOut.Toggle()
assert.Equal(t, gpio.Low, pinIn.Read())
}
func BenchmarkRead(b *testing.B) {
assert.Nil(b, gpio.Open())
defer gpio.Close()
pin := gpio.NewPin(gpio.J8p7)
for i := 0; i < b.N; i++ {
_ = pin.Read()
}
}
func BenchmarkWrite(b *testing.B) {
err := gpio.Open()
assert.Nil(b, err)
defer gpio.Close()
pin := gpio.NewPin(gpio.J8p7)
for i := 0; i < b.N; i++ {
pin.Write(gpio.High)
}
}
func BenchmarkToggle(b *testing.B) {
assert.Nil(b, gpio.Open())
defer gpio.Close()
pin := gpio.NewPin(gpio.J8p7)
defer pin.SetMode(gpio.Input)
pin.Write(gpio.Low)
pin.SetMode(gpio.Output)
for i := 0; i < b.N; i++ {
pin.Toggle()
}
}