-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathgpio_test.go
248 lines (221 loc) · 5.63 KB
/
gpio_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
// Copyright 2017 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package sysfs
import (
"errors"
"testing"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/pin"
)
func TestPin_String(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if s := p.String(); s != "foo" {
t.Fatal(s)
}
}
func TestPin_Name(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if s := p.Name(); s != "foo" {
t.Fatal(s)
}
}
func TestPin_Number(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if n := p.Number(); n != 42 {
t.Fatal(n)
}
}
func TestPin_Func(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
// Fails because open is not mocked.
if s := p.Func(); s != pin.FuncNone {
t.Fatal(s)
}
p = Pin{
number: 42,
name: "foo",
root: "/tmp/gpio/priv/",
fDirection: &fakeGPIOFile{},
}
if s := p.Func(); s != pin.FuncNone {
t.Fatal(s)
}
p.fDirection = &fakeGPIOFile{data: []byte("foo")}
if s := p.Func(); s != pin.FuncNone {
t.Fatal(s)
}
p.fDirection = &fakeGPIOFile{data: []byte("in")}
if s := p.Func(); s != gpio.IN_LOW {
t.Fatal(s)
}
p.fDirection = &fakeGPIOFile{data: []byte("out")}
if s := p.Func(); s != gpio.OUT_LOW {
t.Fatal(s)
}
}
func TestPin_In(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if p.In(gpio.PullNoChange, gpio.NoEdge) == nil {
t.Fatal("can't open")
}
p = Pin{
number: 42,
name: "foo",
root: "/tmp/gpio/priv/",
fDirection: &fakeGPIOFile{},
}
if p.In(gpio.PullNoChange, gpio.NoEdge) == nil {
t.Fatal("can't read direction")
}
p.fDirection = &fakeGPIOFile{data: []byte("out")}
if err := p.In(gpio.PullNoChange, gpio.NoEdge); err != nil {
t.Fatal(err)
}
if p.In(gpio.PullDown, gpio.NoEdge) == nil {
t.Fatal("pull not supported on sysfs-gpio")
}
if p.In(gpio.PullNoChange, gpio.BothEdges) == nil {
t.Fatal("can't open edge")
}
p.fEdge = &fakeGPIOFile{}
if p.In(gpio.PullNoChange, gpio.NoEdge) == nil {
t.Fatal("edge I/O failed")
}
p.fEdge = &fakeGPIOFile{data: []byte("none")}
if err := p.In(gpio.PullNoChange, gpio.NoEdge); err != nil {
t.Fatal(err)
}
if err := p.In(gpio.PullNoChange, gpio.RisingEdge); err != nil {
t.Fatal(err)
}
if err := p.In(gpio.PullNoChange, gpio.FallingEdge); err != nil {
t.Fatal(err)
}
if err := p.In(gpio.PullNoChange, gpio.BothEdges); err != nil {
t.Fatal(err)
}
}
func TestPin_Read(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if l := p.Read(); l != gpio.Low {
t.Fatal("broken pin is always low")
}
p.fValue = &fakeGPIOFile{}
if l := p.Read(); l != gpio.Low {
t.Fatal("broken pin is always low")
}
p.fValue = &fakeGPIOFile{data: []byte("0")}
if l := p.Read(); l != gpio.Low {
t.Fatal("pin is low")
}
p.fValue = &fakeGPIOFile{data: []byte("1")}
if l := p.Read(); l != gpio.High {
t.Fatal("pin is high")
}
p.fValue = &fakeGPIOFile{data: []byte("2")}
if l := p.Read(); l != gpio.Low {
t.Fatal("pin is unknown")
}
}
func TestPin_WaitForEdges(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if p.WaitForEdge(-1) {
t.Fatal("broken pin doesn't have edge triggered")
}
}
func TestPin_Pull(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if pull := p.Pull(); pull != gpio.PullNoChange {
t.Fatal(pull)
}
}
func TestPin_Out(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/", direction: dIn, edge: gpio.NoEdge}
if p.Out(gpio.High) == nil {
t.Fatal("can't open fake root")
}
p.fDirection = &fakeGPIOFile{}
if p.Out(gpio.High) == nil {
t.Fatal("failed to write to direction")
}
p.fDirection = &fakeGPIOFile{data: []byte("dummy")}
if err := p.Out(gpio.High); err != nil {
t.Fatal(err)
}
p.direction = dIn
if err := p.Out(gpio.Low); err != nil {
t.Fatal(err)
}
p.direction = dIn
p.edge = gpio.RisingEdge
p.fEdge = &fakeGPIOFile{}
if p.Out(gpio.High) == nil {
t.Fatal("failed to write to edge")
}
p.edge = gpio.RisingEdge
p.fEdge = &fakeGPIOFile{data: []byte("dummy")}
if err := p.Out(gpio.Low); err != nil {
t.Fatal(err)
}
p.direction = dOut
p.edge = gpio.NoEdge
p.fValue = &fakeGPIOFile{}
if p.Out(gpio.Low) == nil {
t.Fatal("write to value failed")
}
p.fValue = &fakeGPIOFile{data: []byte("dummy")}
if err := p.Out(gpio.Low); err != nil {
t.Fatal(err)
}
if err := p.Out(gpio.High); err != nil {
t.Fatal(err)
}
}
func TestPin_PWM(t *testing.T) {
p := Pin{number: 42, name: "foo", root: "/tmp/gpio/priv/"}
if p.PWM(gpio.DutyHalf, physic.KiloHertz) == nil {
t.Fatal("sysfs-gpio doesn't support PWM")
}
}
func TestPin_readInt(t *testing.T) {
if _, err := readInt("/tmp/gpio/priv/invalid_file"); err == nil {
t.Fatal("file is not expected to exist")
}
}
func TestGPIODriver(t *testing.T) {
if len((&driverGPIO{}).Prerequisites()) != 1 {
t.Fatal("unexpected GPIO prerequisites")
}
}
//
type fakeGPIOFile struct {
data []byte
}
func (f *fakeGPIOFile) Close() error {
return nil
}
func (f *fakeGPIOFile) Fd() uintptr {
return 0
}
func (f *fakeGPIOFile) Ioctl(op uint, data uintptr) error {
return errors.New("injected")
}
func (f *fakeGPIOFile) Read(b []byte) (int, error) {
if f.data == nil {
return 0, errors.New("injected")
}
copy(b, f.data)
return len(f.data), nil
}
func (f *fakeGPIOFile) Write(b []byte) (int, error) {
if f.data == nil {
return 0, errors.New("injected")
}
copy(f.data, b)
return len(f.data), nil
}
func (f *fakeGPIOFile) Seek(offset int64, whence int) (int64, error) {
return 0, nil
}