Beispiel #1
0
def add_event_callback(channel, callback, bouncetime=None):
    """
    :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 callback: TODO
    :param bouncetime: (optional) TODO
    """
    _check_configured(channel, direction=IN)

    if bouncetime is not None:
        if _gpio_warnings:
            warnings.warn("bouncetime is not (yet) fully supported, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", stacklevel=2)

    pin = get_gpio_pin(_mode, channel)
    event.add_edge_callback(pin, __wrap(callback, channel))
Beispiel #2
0
def add_event_callback(channel, callback):
    """
    OPi.GPIO manages a number of secondary threads for callback functions. This
    means that callback functions can be run at the same time as your main
    program, in immediate response to an edge.

    :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 callback: TODO

    For example:

    .. code:: python

       def my_callback(channel):
           print('This is a edge event callback function!')
           print('Edge detected on channel %s'%channel)
           print('This is run in a different thread to your main program')

       GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)  # add rising edge detection on a channel
       #...the rest of your program...

    If you wanted more than one callback function:

    .. code:: python

       def my_callback_one(channel):
           print('Callback one')

       def my_callback_two(channel):
           print('Callback two')

       GPIO.add_event_detect(channel, GPIO.RISING)
       GPIO.add_event_callback(channel, my_callback_one)
       GPIO.add_event_callback(channel, my_callback_two)

    Note that in this case, the callback functions are run sequentially, not
    concurrently. This is because there is only one thread used for callbacks,
    in which every callback is run, in the order in which they have been
    defined.
    """
    _check_configured(channel, direction=IN)
    pin = get_gpio_pin(_mode, channel)
    event.add_edge_callback(pin, __wrap(callback, channel))
Beispiel #3
0
def test_add_edge_callback(fs):
    pin = 71
    fs.CreateFile("/sys/class/gpio/gpio{0}/edge".format(pin))
    fs.CreateFile("/sys/class/gpio/gpio{0}/value".format(pin))

    called = {}

    def cb(p):
        called[p] = True

    with patch("select.epoll") as mock:
        try:
            assert pin not in event._threads
            event.add_edge_detect(pin, RISING)
            event.add_edge_callback(pin, cb)
            mock.return_value.poll.return_value = [(pin, 4)]
            time.sleep(2)
            event.cleanup(pin)
            assert pin in called

        finally:
            event.cleanup()
Beispiel #4
0
def test_add_edge_callback_not_setup():
    pin = 32
    with pytest.raises(RuntimeError) as ex:
        event.add_edge_callback(pin, None)
    assert str(ex.value) == "Add event detection before adding a callback"