예제 #1
0
def test_add_edge_detect_called_twice_throws_error(fs):
    pin = 43
    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:
            event.add_edge_detect(pin, RISING)
            assert pin in event._threads
            with pytest.raises(RuntimeError) as ex:
                event.add_edge_detect(pin, RISING)
            assert str(ex.value) == "Conflicting edge detection already enabled for this GPIO channel"

        finally:
            event.cleanup()
            assert pin not in event._threads
예제 #2
0
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()
예제 #3
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
예제 #4
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()
예제 #5
0
def test_callback_raises_error(fs):
    pin = 194
    fs.CreateFile("/sys/class/gpio/gpio{0}/edge".format(pin))
    fs.CreateFile("/sys/class/gpio/gpio{0}/value".format(pin))

    def cb(p):
        raise RuntimeError("test exception")

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

            with pytest.raises(RuntimeError) as ex:
                event.cleanup(pin)

            assert str(ex.value) == "test exception"

        finally:
            event.cleanup()
예제 #6
0
파일: GPIO.py 프로젝트: jamesdhead/OPi.GPIO
def cleanup(channel=None):
    """
    At the end any program, it is good practice to clean up any resources you
    might have used. This is no different with OPi.GPIO. By returning all
    channels you have used back to inputs with no pull up/down, you can avoid
    accidental damage to your Orange Pi by shorting out the pins. Note that
    this will only clean up GPIO channels that your script has used. Note that
    GPIO.cleanup() also clears the pin numbering system in use.

    To clean up at the end of your script:

    .. code:: python

       GPIO.cleanup()

    It is possible that don't want to clean up every channel leaving some set
    up when your program exits. You can clean up individual channels, a list or
    a tuple of channels:

    .. code:: python

       GPIO.cleanup(channel)
       GPIO.cleanup( (channel1, channel2) )
       GPIO.cleanup( [channel1, channel2] )
    """
    if channel is None:
        cleanup(list(_exports.keys()))
        setwarnings(True)
        global _mode
        _mode = None
    elif isinstance(channel, list):
        for ch in channel:
            cleanup(ch)
    else:
        _check_configured(channel)
        pin = get_gpio_pin(_mode, channel)
        event.cleanup(pin)
        sysfs.unexport(pin)
        del _exports[channel]