forked from mattvenn/machinekit-bipod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterrupt.py
75 lines (65 loc) · 1.81 KB
/
interrupt.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
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
"""
couldn't get Adafruit library working reliably
"""
import select
import time
import threading
import logging
import os
log = logging.getLogger(__name__)
# file to override button press
override = "/tmp/button"
class Interrupt(threading.Thread):
def run(self):
log.debug("button thread started")
# hard coded p9.12
f = '/sys/class/gpio/gpio60/value'
# open & read the file
fh = open(f)
fh.read()
fh.seek(0)
poller = select.poll()
poller.register(fh, select.POLLPRI | select.POLLERR)
# wait until something changes and quit
while True:
events = poller.poll(1)
if len(events):
val = fh.read().strip()
fh.seek(0)
log.debug("%s = %s" % (f,val))
if val == "1":
log.debug("button event %s" % events)
break
# check override
try:
open(override)
log.debug("button override")
os.unlink(override)
break
except IOError:
pass
# finish
fh.close()
log.debug("button thread finished")
if __name__ == '__main__':
log_format = logging.Formatter('%(asctime)s - %(levelname)-8s - %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(log_format)
log.setLevel(logging.DEBUG)
log.addHandler(ch)
thread = Interrupt()
thread.start()
log.info("blocking")
thread.join()
time.sleep(0.5)
log.info("looping")
thread = Interrupt()
thread.daemon = True
thread.start()
while True:
log.info("waiting")
time.sleep(1)
if not thread.isAlive():
log.info("interrupted")
thread.join()
exit(1)