Fix driver

I finally got the button in the mail today and I've finally done it.
This commit is contained in:
Ian Adam Naval 2015-09-15 23:06:16 -04:00
parent 7e43c80020
commit 65faeba785
2 changed files with 19 additions and 16 deletions

View File

@ -17,9 +17,9 @@ class BigRedButton(object):
"""Encapsulates the big red button.""" """Encapsulates the big red button."""
POLL_SIGNAL = b"\x08\x00\x00\x00\x00\x00\x00\x02" POLL_SIGNAL = b"\x08\x00\x00\x00\x00\x00\x00\x02"
LID_CLOSED = b"\x15\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\x00" BUTTON_DOWN = b"\x16\x00\x00\x00\x00\x00\x00\x03"
LID_OPEN = b"\x17\x00\x00\x00\x00\x00\x00\x00" LID_OPEN = b"\x17\x00\x00\x00\x00\x00\x00\x03"
def __init__(self, device_fd): def __init__(self, device_fd):
"""Initializes the BigRedButton. """Initializes the BigRedButton.
@ -58,20 +58,23 @@ class BigRedButton(object):
bytes_written = os.write(self.device_fd, self.POLL_SIGNAL) bytes_written = os.write(self.device_fd, self.POLL_SIGNAL)
if not bytes_written: if not bytes_written:
raise RuntimeError("Could not write to device file.") raise RuntimeError("Could not write to device file.")
bytes_read = os.read(self.device_fd, 8) try:
if not bytes_read: bytes_read = os.read(self.device_fd, 8)
raise RuntimeError("Could not read from device file.") if not bytes_read:
if bytes_read == self.LID_CLOSED and self._prior != self.LID_CLOSED: raise RuntimeError("Could not read from device file.")
_run_callbacks(self._lid_close_callbacks) if bytes_read == self.LID_CLOSED and self._prior != self.LID_CLOSED:
if bytes_read == self.BUTTON_DOWN and self._prior != self.BUTTON_DOWN: _run_callbacks(self._lid_close_callbacks)
_run_callbacks(self._button_press_callbacks) if bytes_read == self.BUTTON_DOWN and self._prior != self.BUTTON_DOWN:
if bytes_read == self.LID_OPEN and self._prior == self.LID_CLOSED: _run_callbacks(self._button_press_callbacks)
_run_callbacks(self._lid_open_callbacks) if bytes_read == self.LID_OPEN and self._prior == self.LID_CLOSED:
if bytes_read != self.POLL_SIGNAL: _run_callbacks(self._lid_open_callbacks)
self._prior = bytes_read if bytes_read != self.POLL_SIGNAL:
self._prior = bytes_read
except BlockingIOError:
pass
def run(self): def run(self):
"""Executes the main event loop for the big red button.""" """Executes the main event loop for the big red button."""
while True: while True:
self._step() self._step()
time.sleep(0.01) time.sleep(0.001)

View File

@ -13,7 +13,7 @@ def handle_button_press():
def main(): def main():
"""Main function for the sync button.""" """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 = BigRedButton(device_file)
button.on_button_press(handle_button_press) button.on_button_press(handle_button_press)
button.run() button.run()