|
@@ -17,9 +17,9 @@ class BigRedButton(object):
|
17
|
17
|
"""Encapsulates the big red button."""
|
18
|
18
|
|
19
|
19
|
POLL_SIGNAL = b"\x08\x00\x00\x00\x00\x00\x00\x02"
|
20
|
|
- LID_CLOSED = b"\x15\x00\x00\x00\x00\x00\x00\x00"
|
21
|
|
- BUTTON_DOWN = b"\x16\x00\x00\x00\x00\x00\x00\x00"
|
22
|
|
- LID_OPEN = b"\x17\x00\x00\x00\x00\x00\x00\x00"
|
|
20
|
+ LID_CLOSED = b"\x15\x00\x00\x00\x00\x00\x00\x03"
|
|
21
|
+ BUTTON_DOWN = b"\x16\x00\x00\x00\x00\x00\x00\x03"
|
|
22
|
+ LID_OPEN = b"\x17\x00\x00\x00\x00\x00\x00\x03"
|
23
|
23
|
|
24
|
24
|
def __init__(self, device_fd):
|
25
|
25
|
"""Initializes the BigRedButton.
|
|
@@ -58,20 +58,23 @@ class BigRedButton(object):
|
58
|
58
|
bytes_written = os.write(self.device_fd, self.POLL_SIGNAL)
|
59
|
59
|
if not bytes_written:
|
60
|
60
|
raise RuntimeError("Could not write to device file.")
|
61
|
|
- bytes_read = os.read(self.device_fd, 8)
|
62
|
|
- if not bytes_read:
|
63
|
|
- raise RuntimeError("Could not read from device file.")
|
64
|
|
- if bytes_read == self.LID_CLOSED and self._prior != self.LID_CLOSED:
|
65
|
|
- _run_callbacks(self._lid_close_callbacks)
|
66
|
|
- if bytes_read == self.BUTTON_DOWN and self._prior != self.BUTTON_DOWN:
|
67
|
|
- _run_callbacks(self._button_press_callbacks)
|
68
|
|
- if bytes_read == self.LID_OPEN and self._prior == self.LID_CLOSED:
|
69
|
|
- _run_callbacks(self._lid_open_callbacks)
|
70
|
|
- if bytes_read != self.POLL_SIGNAL:
|
71
|
|
- self._prior = bytes_read
|
|
61
|
+ try:
|
|
62
|
+ bytes_read = os.read(self.device_fd, 8)
|
|
63
|
+ if not bytes_read:
|
|
64
|
+ raise RuntimeError("Could not read from device file.")
|
|
65
|
+ if bytes_read == self.LID_CLOSED and self._prior != self.LID_CLOSED:
|
|
66
|
+ _run_callbacks(self._lid_close_callbacks)
|
|
67
|
+ if bytes_read == self.BUTTON_DOWN and self._prior != self.BUTTON_DOWN:
|
|
68
|
+ _run_callbacks(self._button_press_callbacks)
|
|
69
|
+ if bytes_read == self.LID_OPEN and self._prior == self.LID_CLOSED:
|
|
70
|
+ _run_callbacks(self._lid_open_callbacks)
|
|
71
|
+ if bytes_read != self.POLL_SIGNAL:
|
|
72
|
+ self._prior = bytes_read
|
|
73
|
+ except BlockingIOError:
|
|
74
|
+ pass
|
72
|
75
|
|
73
|
76
|
def run(self):
|
74
|
77
|
"""Executes the main event loop for the big red button."""
|
75
|
78
|
while True:
|
76
|
79
|
self._step()
|
77
|
|
- time.sleep(0.01)
|
|
80
|
+ time.sleep(0.001)
|