X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/a79e37b2729f77faf00545062fc4a8ded1a6eb97..cf51c6e39aebacced739e8fe16b71389a47d236e:/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py diff --git a/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py b/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py index 6b86614..700d8de 100644 --- a/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py +++ b/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py @@ -4,6 +4,7 @@ import subprocess import os import atexit import random +import struct import sysv_ipc DEBUG = False @@ -45,11 +46,19 @@ class PulseIn: print("Message Queue Key: ", self._mq.key) queues.append(self._mq) except sysv_ipc.ExistentialError: - raise RuntimeError("Message queue creation failed") + raise RuntimeError( + "Message queue creation failed" + ) from sysv_ipc.ExistentialError + + # Check if OS is 64-bit + if struct.calcsize("P") * 8 == 64: + libgpiod_filename = "libgpiod_pulsein64" + else: + libgpiod_filename = "libgpiod_pulsein" dir_path = os.path.dirname(os.path.realpath(__file__)) cmd = [ - dir_path + "/libgpiod_pulsein", + dir_path + "/" + libgpiod_filename, "--pulses", str(maxlen), "--queue", @@ -68,24 +77,29 @@ class PulseIn: # wait for it to start up if DEBUG: print("Waiting for startup success message from subprocess") - message = self._wait_receive_msg() + message = self._wait_receive_msg(timeout=0.25) if message[0] != b"!": raise RuntimeError("Could not establish message queue with subprocess") self._paused = False # pylint: disable=redefined-builtin - def _wait_receive_msg(self, timeout=0.25, type=2): + def _wait_receive_msg(self, timeout=0, type=2): """Internal helper that will wait for new messages of a given type, and throw an exception on timeout""" - stamp = time.monotonic() - while (time.monotonic() - stamp) < timeout: - try: - message = self._mq.receive(block=False, type=type) - return message - except sysv_ipc.BusyError: - time.sleep(0.001) # wait a bit then retry! - # uh-oh timed out - raise RuntimeError("Timed out waiting for PulseIn message") + if timeout > 0: + stamp = time.monotonic() + while (time.monotonic() - stamp) < timeout: + try: + message = self._mq.receive(block=False, type=type) + return message + except sysv_ipc.BusyError: + time.sleep(0.001) # wait a bit then retry! + # uh-oh timed out + raise RuntimeError( + "Timed out waiting for PulseIn message. Make sure libgpiod is installed." + ) + message = self._mq.receive(block=True, type=type) + return message # pylint: enable=redefined-builtin