Пример #1
0
def test_input_is_active():
    pin = MockPin(2)
    with InputDevice(pin, pull_up=True) as device:
        pin.drive_high()
        assert not device.is_active
        pin.drive_low()
        assert device.is_active
Пример #2
0
def test_mock_pin_edges():
    pin = MockPin(2)
    assert pin.when_changed is None
    fired = Event()
    pin.function = 'input'
    pin.edges = 'both'
    assert pin.edges == 'both'
    pin.drive_low()
    assert not pin.state
    def changed():
        fired.set()
    pin.when_changed = changed
    pin.drive_high()
    assert pin.state
    assert fired.wait(0)
    fired.clear()
    pin.edges = 'falling'
    pin.drive_low()
    assert not pin.state
    assert fired.wait(0)
    fired.clear()
    pin.drive_high()
    assert pin.state
    assert not fired.wait(0)
    assert pin.edges == 'falling'
Пример #3
0
def test_mock_pin_edges():
    pin = MockPin(2)
    assert pin.when_changed is None
    fired = Event()
    pin.function = "input"
    pin.edges = "both"
    assert pin.edges == "both"
    pin.drive_low()
    assert not pin.state

    def changed():
        fired.set()

    pin.when_changed = changed
    pin.drive_high()
    assert pin.state
    assert fired.wait(0)
    fired.clear()
    pin.edges = "falling"
    pin.drive_low()
    assert not pin.state
    assert fired.wait(0)
    fired.clear()
    pin.drive_high()
    assert pin.state
    assert not fired.wait(0)
    assert pin.edges == "falling"
Пример #4
0
def test_input_is_active():
    pin = MockPin(2)
    with InputDevice(pin, pull_up=True) as device:
        pin.drive_high()
        assert not device.is_active
        pin.drive_low()
        assert device.is_active
Пример #5
0
def test_mock_pin_edges():
    pin = MockPin(5)
    assert pin.when_changed is None
    fired = Event()
    pin.function = 'input'
    pin.edges = 'both'
    assert pin.edges == 'both'
    pin.drive_low()
    assert not pin.state
    def changed():
        fired.set()
    pin.when_changed = changed
    pin.drive_high()
    assert pin.state
    assert fired.wait(0)
    fired.clear()
    pin.edges = 'falling'
    pin.drive_low()
    assert not pin.state
    assert fired.wait(0)
    fired.clear()
    pin.drive_high()
    assert pin.state
    assert not fired.wait(0)
    assert pin.edges == 'falling'
Пример #6
0
def test_input_is_active_high():
    pin = MockPin(2)
    with InputDevice(pin, pull_up=False) as device:
        pin.drive_high()
        assert device.is_active
        assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=False, is_active=True>'
        pin.drive_low()
        assert not device.is_active
        assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=False, is_active=False>'
Пример #7
0
def test_input_motion_sensor():
    pin = MockPin(2)
    with MotionSensor(pin) as sensor:
        pin.drive_high()
        assert sensor.wait_for_motion(1)
        assert sensor.motion_detected
        pin.drive_low()
        assert sensor.wait_for_no_motion(1)
        assert not sensor.motion_detected
Пример #8
0
def test_input_line_sensor():
    pin = MockPin(2)
    with LineSensor(pin) as sensor:
        pin.drive_low() # logic is inverted for line sensor
        assert sensor.wait_for_line(1)
        assert sensor.line_detected
        pin.drive_high()
        assert sensor.wait_for_no_line(1)
        assert not sensor.line_detected
Пример #9
0
def test_input_smoothed_values():
    pin = MockPin(2)
    with SmoothedInputDevice(pin) as device:
        device._queue.start()
        assert not device.is_active
        pin.drive_high()
        assert device.wait_for_active(1)
        pin.drive_low()
        assert device.wait_for_inactive(1)
Пример #10
0
def test_input_is_active_high():
    pin = MockPin(2)
    with InputDevice(pin, pull_up=False) as device:
        pin.drive_high()
        assert device.is_active
        assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=False, is_active=True>'
        pin.drive_low()
        assert not device.is_active
        assert repr(device) == '<gpiozero.InputDevice object on pin MOCK2, pull_up=False, is_active=False>'
Пример #11
0
def test_input_motion_sensor():
    pin = MockPin(2)
    with MotionSensor(pin) as sensor:
        pin.drive_high()
        assert sensor.wait_for_motion(1)
        assert sensor.motion_detected
        pin.drive_low()
        assert sensor.wait_for_no_motion(1)
        assert not sensor.motion_detected
Пример #12
0
def test_input_line_sensor():
    pin = MockPin(2)
    with LineSensor(pin) as sensor:
        pin.drive_low() # logic is inverted for line sensor
        assert sensor.wait_for_line(1)
        assert sensor.line_detected
        pin.drive_high()
        assert sensor.wait_for_no_line(1)
        assert not sensor.line_detected
Пример #13
0
def test_input_smoothed_values():
    pin = MockPin(2)
    with SmoothedInputDevice(pin) as device:
        device._queue.start()
        assert not device.is_active
        pin.drive_high()
        assert device.wait_for_active(1)
        pin.drive_low()
        assert device.wait_for_inactive(1)
Пример #14
0
def test_input_event_deactivated():
    event = Event()
    pin = MockPin(2)
    with DigitalInputDevice(pin) as device:
        device.when_deactivated = lambda: event.set()
        assert not event.is_set()
        pin.drive_high()
        assert not event.is_set()
        pin.drive_low()
        assert event.is_set()
Пример #15
0
def test_input_event_deactivated():
    event = Event()
    pin = MockPin(2)
    with DigitalInputDevice(pin) as device:
        device.when_deactivated = lambda: event.set()
        assert not event.is_set()
        pin.drive_high()
        assert not event.is_set()
        pin.drive_low()
        assert event.is_set()
Пример #16
0
def test_input_button():
    pin = MockPin(2)
    with Button(pin) as button:
        assert pin.pull == 'up'
        assert not button.is_pressed
        pin.drive_low()
        assert button.is_pressed
        assert button.wait_for_press(1)
        pin.drive_high()
        assert not button.is_pressed
        assert button.wait_for_release(1)
Пример #17
0
def test_input_button():
    pin = MockPin(2)
    with Button(pin) as button:
        assert pin.pull == 'up'
        assert not button.is_pressed
        pin.drive_low()
        assert button.is_pressed
        assert button.wait_for_press(1)
        pin.drive_high()
        assert not button.is_pressed
        assert button.wait_for_release(1)
def test_traffic_lights_buzzer():
    red_pin = MockPin(2)
    amber_pin = MockPin(3)
    green_pin = MockPin(4)
    buzzer_pin = MockPin(5)
    button_pin = MockPin(6)
    with TrafficLightsBuzzer(TrafficLights(red_pin, amber_pin, green_pin),
                             Buzzer(buzzer_pin), Button(button_pin)) as board:
        board.lights.red.on()
        board.buzzer.on()
        assert red_pin.state
        assert not amber_pin.state
        assert not green_pin.state
        assert buzzer_pin.state
        button_pin.drive_low()
        assert board.button.is_active
Пример #19
0
def test_traffic_lights_buzzer():
    red_pin = MockPin(2)
    amber_pin = MockPin(3)
    green_pin = MockPin(4)
    buzzer_pin = MockPin(5)
    button_pin = MockPin(6)
    with TrafficLightsBuzzer(
            TrafficLights(red_pin, amber_pin, green_pin),
            Buzzer(buzzer_pin),
            Button(button_pin)) as board:
        board.lights.red.on()
        board.buzzer.on()
        assert red_pin.state
        assert not amber_pin.state
        assert not green_pin.state
        assert buzzer_pin.state
        button_pin.drive_low()
        assert board.button.is_active