forked from vsergeev/python-periphery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gpio.py
More file actions
237 lines (187 loc) · 6.15 KB
/
test_gpio.py
File metadata and controls
237 lines (187 loc) · 6.15 KB
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
import sys
import threading
import time
import periphery
from .asserts import AssertRaises
if sys.version_info[0] == 3:
raw_input = input
import queue
else:
import Queue as queue
pin_input = None
pin_output = None
def test_arguments():
print("Starting arguments test...")
# Invalid open types
with AssertRaises(TypeError):
periphery.GPIO("abc", "out")
with AssertRaises(TypeError):
periphery.GPIO(100, 100)
# Invalid direction
with AssertRaises(ValueError):
periphery.GPIO(100, "blah")
print("Arguments test passed.")
def test_open_close():
print("Starting open/close test...")
# Open non-existent GPIO (export should fail with EINVAL)
with AssertRaises(periphery.GPIOError):
periphery.GPIO(9999, "in")
# Open legitimate GPIO
gpio = periphery.GPIO(pin_output, "in")
assert gpio.pin == pin_output
assert gpio.fd > 0
# Set direction out, check direction out, check value low
gpio.direction = "out"
assert gpio.direction == "out"
assert gpio.read() == False
# Set direction low, check direction out, check value low
gpio.direction = "low"
assert gpio.direction == "out"
assert gpio.read() == False
# Set direction high, check direction out, check value high
gpio.direction = "high"
assert gpio.direction == "out"
assert gpio.read() == True
# Set direction in, check direction in
gpio.direction = "in"
assert gpio.direction == "in"
# Set invalid direction
with AssertRaises(ValueError):
gpio.direction = "blah"
# Set invalid edge
with AssertRaises(ValueError):
gpio.edge = "blah"
# Check interrupt edge support
assert gpio.supports_interrupts == True
# Set edge none, check edge none
gpio.edge = "none"
assert gpio.edge == "none"
# Set edge rising, check edge rising
gpio.edge = "rising"
assert gpio.edge == "rising"
# Set edge falling, check edge falling
gpio.edge = "falling"
assert gpio.edge == "falling"
# Set edge both, check edge both
gpio.edge = "both"
assert gpio.edge == "both"
# Set edge none, check edge none
gpio.edge = "none"
assert gpio.edge == "none"
gpio.close()
# Open with preserved direction
gpio = periphery.GPIO(pin_output, "preserve")
assert gpio.direction == "in"
gpio.direction = "out"
gpio.close()
# Open with preserved direction, using default argument
gpio = periphery.GPIO(pin_output)
assert gpio.direction == "out"
gpio.direction = "in"
gpio.close()
print("Open/close test passed.")
def test_loopback():
print("Starting loopback test...")
# Open in and out pins
gpio_in = periphery.GPIO(pin_input, "in")
gpio_out = periphery.GPIO(pin_output, "out")
# Drive out low, check in low
print("Drive out low, check in low")
gpio_out.write(False)
assert gpio_in.read() == False
# Drive out high, check in high
print("Drive out high, check in high")
gpio_out.write(True)
assert gpio_in.read() == True
# Wrapper for running poll() in a thread
def threaded_poll(gpio, timeout):
ret = queue.Queue()
def f():
ret.put(gpio.poll(timeout))
thread = threading.Thread(target=f)
thread.start()
return ret
# Check poll falling 1 -> 0 interrupt
print("Check poll falling 1 -> 0 interrupt")
gpio_in.edge = "falling"
poll_ret = threaded_poll(gpio_in, 5)
time.sleep(1)
gpio_out.write(False)
assert poll_ret.get() == True
assert gpio_in.read() == False
# Check poll timeout on 0 -> 0
print("Check poll falling timeout on 0 -> 0")
poll_ret = threaded_poll(gpio_in, 2)
time.sleep(1)
gpio_out.write(False)
assert poll_ret.get() == False
assert gpio_in.read() == False
# Check poll rising 0 -> 1 interrupt
print("Check poll rising 0 -> 1 interrupt")
gpio_in.edge = "rising"
poll_ret = threaded_poll(gpio_in, 5)
time.sleep(1)
gpio_out.write(True)
assert poll_ret.get() == True
assert gpio_in.read() == True
# Check poll timeout on 1 -> 1
print("Check poll rising timeout on 1 -> 1")
poll_ret = threaded_poll(gpio_in, 2)
time.sleep(1)
gpio_out.write(True)
assert poll_ret.get() == False
assert gpio_in.read() == True
# Check poll rising+falling interrupts
print("Check poll rising/falling interrupt")
gpio_in.edge = "both"
poll_ret = threaded_poll(gpio_in, 5)
time.sleep(1)
gpio_out.write(False)
assert poll_ret.get() == True
assert gpio_in.read() == False
poll_ret = threaded_poll(gpio_in, 5)
time.sleep(1)
gpio_out.write(True)
assert poll_ret.get() == True
assert gpio_in.read() == True
# Check poll timeout
print("Check poll timeout")
assert gpio_in.poll(1) == False
gpio_in.close()
gpio_out.close()
print("Loopback test passed.")
def test_interactive():
print("Starting interactive test...")
gpio = periphery.GPIO(pin_output, "out")
print("Starting interactive test. Get out your multimeter, buddy!")
raw_input("Press enter to continue...")
# Drive GPIO out low
gpio.write(False)
assert raw_input("GPIO out is low? y/n ") == "y"
# Drive GPIO out high
gpio.write(True)
assert raw_input("GPIO out is high? y/n ") == "y"
# Drive GPIO out low
gpio.write(False)
assert raw_input("GPIO out is low? y/n ") == "y"
gpio.close()
print("Interactive test passed.")
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python -m tests.test_gpio <gpio1> <gpio2>")
print("")
print(" gpio1 gpio #1 (connected in to gpio #2)")
print(" gpio2 gpio #2 (connected in to gpio #1)")
print("")
print("Hint: for BeagleBone Black, connect a wire between")
print("GPIO 66 (P8.7) and GPIO 67 (P8.8), and then run this test:")
print(" python -m tests.test_gpio 66 67")
sys.exit(1)
pin_input = int(sys.argv[1])
pin_output = int(sys.argv[2])
print("Starting GPIO tests...")
test_arguments()
test_open_close()
test_loopback()
test_interactive()
print("All GPIO tests passed.")