def test_blocking_wait_for_edge_timeout(fs): pin = 68 fs.CreateFile("/sys/class/gpio/gpio{0}/edge".format(pin)) fs.CreateFile("/sys/class/gpio/gpio{0}/value".format(pin)) with patch("select.epoll"): assert event.blocking_wait_for_edge(pin, RISING, timeout=0.01) is None
def test_blocking_wait_raises_error_add_edge_detect_already_active(fs): pin = 66 fs.CreateFile("/sys/class/gpio/gpio{0}/edge".format(pin)) fs.CreateFile("/sys/class/gpio/gpio{0}/value".format(pin)) with patch("select.epoll"): try: assert pin not in event._threads event.add_edge_detect(pin, RISING) with pytest.raises(RuntimeError) as ex: event.blocking_wait_for_edge(pin, RISING) assert str(ex.value) == "Conflicting edge detection events already exist for this GPIO channel" finally: event.cleanup()
def test_blocking_wait_for_edge_detected(fs): pin = 198 fs.CreateFile("/sys/class/gpio/gpio{0}/edge".format(pin)) fs.CreateFile("/sys/class/gpio/gpio{0}/value".format(pin)) with patch("select.epoll") as mock: mock.return_value.poll.return_value = [(pin, 23)] assert event.blocking_wait_for_edge(pin, RISING, timeout=0.01) == pin
def wait_for_edge(channel, trigger, timeout=-1): """ This function is designed to block execution of your program until an edge is detected. :param channel: the channel based on the numbering system you have specified (:py:attr:`GPIO.BOARD`, :py:attr:`GPIO.BCM` or :py:attr:`GPIO.SUNXI`). :param trigger: The event to detect, one of: :py:attr:`GPIO.RISING`, :py:attr:`GPIO.FALLING` or :py:attr:`GPIO.BOTH`. :param timeout: TODO In other words, the polling example above that waits for a button press could be rewritten as: .. code:: python GPIO.wait_for_edge(channel, GPIO.RISING) Note that you can detect edges of type :py:attr:`GPIO.RISING`, :py:attr`GPIO.FALLING` or :py:attr:`GPIO.BOTH`. The advantage of doing it this way is that it uses a negligible amount of CPU, so there is plenty left for other tasks. If you only want to wait for a certain length of time, you can use the timeout parameter: .. code:: python # wait for up to 5 seconds for a rising edge (timeout is in milliseconds) channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000) if channel is None: print('Timeout occurred') else: print('Edge detected on channel', channel) """ _check_configured(channel, direction=IN) pin = get_gpio_pin(_mode, channel) if event.blocking_wait_for_edge(pin, trigger, timeout) is not None: return channel