def blocking_wait_for_edge(pin, trigger, timeout=-1): assert trigger in [RISING, FALLING, BOTH] if pin in _threads: raise RuntimeError("Conflicting edge detection events already exist for this GPIO channel") try: sysfs.edge(pin, trigger) finished = False initial_edge = True with sysfs.value_descriptor(pin) as fd: e = select.epoll() e.register(fd, EPOLLIN | EPOLLET | EPOLLPRI) try: while not finished: # TODO: implement bouncetime events = e.poll(timeout / 1000.0, maxevents=1) if initial_edge: initial_edge = False else: finished = True n = len(events) if n == 0: return None else: return pin finally: e.unregister(fd) e.close() finally: sysfs.edge(pin, NONE)
def run(self): self.exc = None try: sysfs.edge(self._pin, self._trigger) initial_edge = True with sysfs.value_descriptor(self._pin) as fd: e = select.epoll() e.register(fd, EPOLLIN | EPOLLET | EPOLLPRI) try: while not self._finished: events = e.poll(0.1, maxevents=1) if initial_edge: initial_edge = False elif len(events) > 0: with self._lock: self._event_detected = True self.notify_callbacks() finally: e.unregister(fd) e.close() except BaseException as e: self.exc = e finally: sysfs.edge(self._pin, NONE)
def test_edge(fs, test_input, expected): fs.CreateFile("/sys/class/gpio/gpio5/edge") edge(5, test_input) with open("/sys/class/gpio/gpio5/edge") as fp: assert fp.read() == expected