diff --git a/dream_cheeky/__init__.py b/dream_cheeky/__init__.py index b017b6d..11e5952 100644 --- a/dream_cheeky/__init__.py +++ b/dream_cheeky/__init__.py @@ -17,9 +17,9 @@ class BigRedButton(object): """Encapsulates the big red button.""" POLL_SIGNAL = b"\x08\x00\x00\x00\x00\x00\x00\x02" - LID_CLOSED = b"\x15\x00\x00\x00\x00\x00\x00\x00" - BUTTON_DOWN = b"\x16\x00\x00\x00\x00\x00\x00\x00" - LID_OPEN = b"\x17\x00\x00\x00\x00\x00\x00\x00" + LID_CLOSED = b"\x15\x00\x00\x00\x00\x00\x00\x03" + BUTTON_DOWN = b"\x16\x00\x00\x00\x00\x00\x00\x03" + LID_OPEN = b"\x17\x00\x00\x00\x00\x00\x00\x03" def __init__(self, device_fd): """Initializes the BigRedButton. @@ -58,20 +58,23 @@ class BigRedButton(object): bytes_written = os.write(self.device_fd, self.POLL_SIGNAL) if not bytes_written: raise RuntimeError("Could not write to device file.") - bytes_read = os.read(self.device_fd, 8) - if not bytes_read: - raise RuntimeError("Could not read from device file.") - if bytes_read == self.LID_CLOSED and self._prior != self.LID_CLOSED: - _run_callbacks(self._lid_close_callbacks) - if bytes_read == self.BUTTON_DOWN and self._prior != self.BUTTON_DOWN: - _run_callbacks(self._button_press_callbacks) - if bytes_read == self.LID_OPEN and self._prior == self.LID_CLOSED: - _run_callbacks(self._lid_open_callbacks) - if bytes_read != self.POLL_SIGNAL: - self._prior = bytes_read + try: + bytes_read = os.read(self.device_fd, 8) + if not bytes_read: + raise RuntimeError("Could not read from device file.") + if bytes_read == self.LID_CLOSED and self._prior != self.LID_CLOSED: + _run_callbacks(self._lid_close_callbacks) + if bytes_read == self.BUTTON_DOWN and self._prior != self.BUTTON_DOWN: + _run_callbacks(self._button_press_callbacks) + if bytes_read == self.LID_OPEN and self._prior == self.LID_CLOSED: + _run_callbacks(self._lid_open_callbacks) + if bytes_read != self.POLL_SIGNAL: + self._prior = bytes_read + except BlockingIOError: + pass def run(self): """Executes the main event loop for the big red button.""" while True: self._step() - time.sleep(0.01) + time.sleep(0.001) diff --git a/examples/print_on_button_press.py b/examples/print_on_button_press.py index 174e813..c966a37 100755 --- a/examples/print_on_button_press.py +++ b/examples/print_on_button_press.py @@ -13,7 +13,7 @@ def handle_button_press(): def main(): """Main function for the sync button.""" - device_file = os.open("/dev/big_red_button", os.O_RDWR) + device_file = os.open("/dev/hidraw0", os.O_RDWR|os.O_NONBLOCK) button = BigRedButton(device_file) button.on_button_press(handle_button_press) button.run()