Browse Source

Fix driver

I finally got the button in the mail today and I've finally done it.
Ian Adam Naval 4 years ago
parent
commit
65faeba785
2 changed files with 19 additions and 16 deletions
  1. 18
    15
      dream_cheeky/__init__.py
  2. 1
    1
      examples/print_on_button_press.py

+ 18
- 15
dream_cheeky/__init__.py View File

@@ -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)

+ 1
- 1
examples/print_on_button_press.py View File

@@ -13,7 +13,7 @@ def handle_button_press():
13 13
 
14 14
 def main():
15 15
     """Main function for the sync button."""
16
-    device_file = os.open("/dev/big_red_button", os.O_RDWR)
16
+    device_file = os.open("/dev/hidraw0", os.O_RDWR|os.O_NONBLOCK)
17 17
     button = BigRedButton(device_file)
18 18
     button.on_button_press(handle_button_press)
19 19
     button.run()

Loading…
Cancel
Save