예제 #1
0
def event_detected(channel):
    """
    This function is designed to be used in a loop with other things, but unlike
    polling it is not going to miss the change in state of an input while the
    CPU is busy working on other things. This could be useful when using
    something like Pygame or PyQt where there is a main loop listening and
    responding to GUI events in a timely basis.

    .. code:: python

       GPIO.add_event_detect(channel, GPIO.RISING)  # add rising edge detection on a channel
       do_something()
       if GPIO.event_detected(channel):
           print('Button pressed')

    Note that you can detect events for :py:attr:`GPIO.RISING`,
    :py:attr:`GPIO.FALLING` or :py:attr:`GPIO.BOTH`.

    :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`).
    :returns: :py:attr:`True` if an edge event was detected, else :py:attr:`False`.
    """
    _check_configured(channel, direction=IN)
    pin = get_gpio_pin(_mode, channel)
    return event.edge_detected(pin)
예제 #2
0
def test_edge_detected(fs):
    pin = 23
    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:
        try:
            assert pin not in event._threads
            event.add_edge_detect(pin, RISING)
            assert not event.edge_detected(pin)
            mock.return_value.poll.return_value = [(pin, 4)]
            time.sleep(2)
            event._threads[pin].cancel()
            assert event.edge_detected(pin)
            assert not event.edge_detected(pin)

        finally:
            event.cleanup()
            assert pin not in event._threads
예제 #3
0
파일: GPIO.py 프로젝트: jamesdhead/OPi.GPIO
def event_detected(channel):
    """
    TODO

    :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`).
    :returns: :py:attr:`True` if an edge event was detected, else :py:attr:`False`.
    """
    _check_configured(channel, direction=IN)
    pin = get_gpio_pin(_mode, channel)
    return event.edge_detected(pin)
예제 #4
0
def test_event_detected_not_configured():
    pin = 54
    assert pin not in event._threads
    assert not event.edge_detected(pin)