Browse Source

Add basic implementation

Ian Adam Naval 4 years ago
parent
commit
7e43c80020
3 changed files with 118 additions and 0 deletions
  1. 77
    0
      dream_cheeky/__init__.py
  2. 23
    0
      examples/print_on_button_press.py
  3. 18
    0
      setup.py

+ 77
- 0
dream_cheeky/__init__.py View File

@@ -0,0 +1,77 @@
1
+"""Simple interface for the Dream Cheeky Big Red Button."""
2
+
3
+import os
4
+import time
5
+
6
+
7
+def _run_callbacks(callbacks):
8
+    """Runs all the callbacks.
9
+
10
+    :param callbacks: List of callable functions.
11
+    """
12
+    for callback in callbacks:
13
+        callback()
14
+
15
+
16
+class BigRedButton(object):
17
+    """Encapsulates the big red button."""
18
+
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"
23
+
24
+    def __init__(self, device_fd):
25
+        """Initializes the BigRedButton.
26
+
27
+        :param device_fd: File descriptor for the device
28
+        """
29
+        self.device_fd = device_fd
30
+        self._prior = self.LID_CLOSED
31
+        self._button_press_callbacks = []
32
+        self._lid_open_callbacks = []
33
+        self._lid_close_callbacks = []
34
+
35
+    def on_button_press(self, callback):
36
+        """Registers callback for button press event.
37
+
38
+        :param callback: Callback function to run on button press
39
+        """
40
+        self._button_press_callbacks.append(callback)
41
+
42
+    def on_lid_open(self, callback):
43
+        """Registers callback for lid open event.
44
+
45
+        :param callback: Callback function to run on lid open
46
+        """
47
+        self._lid_open_callbacks.append(callback)
48
+
49
+    def on_lid_close(self, callback):
50
+        """Registers callback for lid close event.
51
+
52
+        :param callback: Callback function to run on lid close
53
+        """
54
+        self._lid_close_callbacks.append(callback)
55
+
56
+    def _step(self):
57
+        """Executes one step of the event loop."""
58
+        bytes_written = os.write(self.device_fd, self.POLL_SIGNAL)
59
+        if not bytes_written:
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
72
+
73
+    def run(self):
74
+        """Executes the main event loop for the big red button."""
75
+        while True:
76
+            self._step()
77
+            time.sleep(0.01)

+ 23
- 0
examples/print_on_button_press.py View File

@@ -0,0 +1,23 @@
1
+#!/usr/bin/env python
2
+"""Sample script for python-dream-cheeky."""
3
+
4
+import os
5
+
6
+from dream_cheeky import BigRedButton
7
+
8
+
9
+def handle_button_press():
10
+    """Handles a button press."""
11
+    print("Button pressed!")
12
+
13
+
14
+def main():
15
+    """Main function for the sync button."""
16
+    device_file = os.open("/dev/big_red_button", os.O_RDWR)
17
+    button = BigRedButton(device_file)
18
+    button.on_button_press(handle_button_press)
19
+    button.run()
20
+
21
+
22
+if __name__ == '__main__':
23
+    main()

+ 18
- 0
setup.py View File

@@ -0,0 +1,18 @@
1
+from distutils.core import setup
2
+
3
+description = "Event-driven Python library for interacting with the Dream Cheeky big red button."
4
+
5
+setup(
6
+        name='python-dream-cheeky',
7
+        version='0.1.0',
8
+        author='Ian Adam Naval',
9
+        author_email='ianonavy@gmail.com',
10
+        packages=['dream_cheeky'],
11
+        scripts=[],
12
+        url='https://git.ianonavy.com/ianonavy/python-dream-cheeky/',
13
+        license='MIT',
14
+        description=description,
15
+        long_description=description,
16
+        install_requires=[],
17
+
18
+)

Loading…
Cancel
Save