-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
executable file
·44 lines (30 loc) · 1.01 KB
/
detect.py
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
#!/usr/bin/env python3
"""
A handy util script that assumes there are buttons on all GPIO pins
and logs whenever they are opened or closed. This is handy because
you can connect a wire directly from any ground pin to each GPIO pin
in turn to confirm that the pin is working by watching for the output
to show a change in the corresponding button.
"""
from gpiozero import Button
from signal import pause
from datetime import datetime
def main():
pin_numbers = ["GPIO{}".format(id) for id in range(0, 27+1)]
for pin_number in pin_numbers:
button = Button(pin_number)
add_handlers(button)
pause()
def add_handlers(button):
button.when_pressed = on_closed
button.when_released = on_opened
if button.is_pressed:
on_closed(button)
else:
on_opened(button)
def on_closed(button):
print("✅ {} {}".format(button.pin.number, datetime.now()))
def on_opened(button):
print("❌ {} {}".format(button.pin.number, datetime.now()))
if __name__ == '__main__':
main()