示例#1
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)
示例#2
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
示例#3
0
def test_backend_get_digital_state() -> None:
    """Test that we can read back the digital state of a pin."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    # This should put the pin into the most recent (or default) output state.
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_OUTPUT)
    assert backend.get_gpio_pin_digital_state(2) is False
    backend.write_gpio_pin_digital_state(2, True)
    assert backend.get_gpio_pin_digital_state(2) is True
    backend.write_gpio_pin_digital_state(2, False)
    assert backend.get_gpio_pin_digital_state(2) is False
示例#4
0
def test_backend_write_digital_state() -> None:
    """Test that we can write the digital state of a pin."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()
    # This should put the pin into the most recent (or default) output state.
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_OUTPUT)
    serial.check_sent_data(b"W 2 L\n")
    backend.write_gpio_pin_digital_state(2, True)
    serial.check_sent_data(b"W 2 H\n")
    backend.write_gpio_pin_digital_state(2, False)
    serial.check_sent_data(b"W 2 L\n")
    serial.check_all_received_data_consumed()
示例#5
0
def test_backend_digital_state_persists() -> None:
    """Test switching to a different mode and then back to output."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_OUTPUT)
    serial.check_sent_data(b"W 2 L\n")
    backend.write_gpio_pin_digital_state(2, True)
    serial.check_sent_data(b"W 2 H\n")
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT)
    serial.check_sent_data(b"W 2 Z\n")
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_OUTPUT)
    serial.check_sent_data(b"W 2 H\n")
    backend.write_gpio_pin_digital_state(2, False)
    serial.check_sent_data(b"W 2 L\n")
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT)
    serial.check_sent_data(b"W 2 Z\n")
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_OUTPUT)
    serial.check_sent_data(b"W 2 L\n")
    serial.check_all_received_data_consumed()
示例#6
0
def test_backend_read_digital_state() -> None:
    """Test that we can read the digital state of a pin."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()

    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT)
    serial.check_sent_data(b"W 2 Z\n")

    serial.append_received_data(b"> H", newline=True)
    assert backend.read_gpio_pin_digital_state(2) is True
    serial.check_sent_data(b"R 2\n")

    serial.append_received_data(b"> L", newline=True)
    assert backend.read_gpio_pin_digital_state(2) is False
    serial.check_sent_data(b"R 2\n")

    serial.append_received_data(b"> X", newline=True)  # invalid
    with pytest.raises(CommunicationError):
        backend.read_gpio_pin_digital_state(2)
    serial.check_sent_data(b"R 2\n")

    serial.check_all_received_data_consumed()
示例#7
0
def test_backend_input_modes() -> None:
    """Check that the correct commands are send when setting pins to input modes."""
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    serial = cast(SBArduinoSerial, backend._serial)
    serial.check_data_sent_by_constructor()
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT)
    serial.check_sent_data(b"W 2 Z\n")
    backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT_PULLUP)
    serial.check_sent_data(b"W 2 P\n")
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT_PULLDOWN)
    serial.check_all_received_data_consumed()
示例#8
0
def check_for_communication_error(backend: SBArduinoHardwareBackend) -> None:
    """Check that performing an operation on the backend raises a CommunicationError."""
    with pytest.raises(CommunicationError):
        backend.set_gpio_pin_mode(2, GPIOPinMode.DIGITAL_INPUT)
示例#9
0
def test_backend_analogue_pin_modes() -> None:
    """Test that only certain modes are valid on digital pins."""
    pin = 14
    backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial)
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(pin, GPIOPinMode.DIGITAL_INPUT)
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(pin, GPIOPinMode.DIGITAL_INPUT_PULLUP)
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(pin, GPIOPinMode.DIGITAL_INPUT_PULLDOWN)
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(pin, GPIOPinMode.DIGITAL_OUTPUT)
    backend.set_gpio_pin_mode(pin, GPIOPinMode.ANALOGUE_INPUT)
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(pin, GPIOPinMode.ANALOGUE_OUTPUT)
    with pytest.raises(NotSupportedByHardwareError):
        backend.set_gpio_pin_mode(pin, GPIOPinMode.PWM_OUTPUT)