Example #1
0
def test_backend_get_set_pin_mode() -> None:
    """Test that we can get and set pin modes."""
    pin = 2
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    assert backend.get_gpio_pin_mode(pin) is GPIOPinMode.DIGITAL_INPUT
    backend.set_gpio_pin_mode(pin, GPIOPinMode.DIGITAL_OUTPUT)
    assert backend.get_gpio_pin_mode(pin) is GPIOPinMode.DIGITAL_OUTPUT
Example #2
0
def test_backend_read_digital_state_requires_pin_mode() -> None:
    """Check that pin must be in DIGITAL_INPUT* mode for read digital state to work."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_OUTPUT)
    assert backend.get_gpio_pin_mode(2) is not GPIOPinMode.DIGITAL_INPUT
    with pytest.raises(ValueError):
        backend.read_gpio_pin_digital_state(2)
Example #3
0
def test_ultrasound_pulse() -> None:
    """Test that we can read an ultrasound pulse time."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()

    serial.append_received_data(b"> 2345\n")
    duration = backend.get_ultrasound_pulse(3, 4)
    serial.check_sent_data(b"T 3 4\n")
    assert duration == timedelta(microseconds=2345)

    # Check backend updated its view of what modes the pins are in now.
    assert backend.get_gpio_pin_mode(3) is GPIOPinMode.DIGITAL_OUTPUT
    assert backend.get_gpio_pin_digital_state(3) is False
    assert backend.get_gpio_pin_mode(4) is GPIOPinMode.DIGITAL_INPUT

    serial.check_all_received_data_consumed()
Example #4
0
def test_ultrasound_distance() -> None:
    """Test that we can read an ultrasound distance."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()

    serial.append_received_data(b"> 1230\n")
    metres = backend.get_ultrasound_distance(3, 4)
    serial.check_sent_data(b"U 3 4\n")
    assert metres is not None
    assert isclose(metres, 1.23)

    # Check backend updated its view of what modes the pins are in now.
    assert backend.get_gpio_pin_mode(3) is GPIOPinMode.DIGITAL_OUTPUT
    assert backend.get_gpio_pin_digital_state(3) is False
    assert backend.get_gpio_pin_mode(4) is GPIOPinMode.DIGITAL_INPUT

    serial.check_all_received_data_consumed()
Example #5
0
def test_ultrasound_pulse_on_same_pin() -> None:
    """Test same pin for trigger and echo."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()

    serial.append_received_data(b"> 2345\n")
    duration = backend.get_ultrasound_pulse(3, 3)
    serial.check_sent_data(b"T 3 3\n")
    assert duration == timedelta(microseconds=2345)

    # Check backend updated its view of what modes the pins are in now.
    assert backend.get_gpio_pin_mode(3) is GPIOPinMode.DIGITAL_INPUT

    serial.check_all_received_data_consumed()
Example #6
0
def test_ultrasound_distance_on_same_pin() -> None:
    """Test same pin for trigger and echo."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()

    serial.append_received_data(b"> 1230\n")
    metres = backend.get_ultrasound_distance(3, 3)
    serial.check_sent_data(b"U 3 3\n")
    assert metres is not None
    assert isclose(metres, 1.23)

    # Check backend updated its view of what modes the pins are in now.
    assert backend.get_gpio_pin_mode(3) is GPIOPinMode.DIGITAL_INPUT

    serial.check_all_received_data_consumed()