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
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()
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()
def test_backend_write_digital_state_requires_digital_pin() -> None: """Check that pins 14-19 are not supported by write digital state.""" backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial) with pytest.raises(NotSupportedByHardwareError): backend.write_gpio_pin_digital_state(14, True)
def test_backend_write_digital_state_requires_pin_mode() -> None: """Check that pin must be in DIGITAL_OUTPUT mode for write digital state to work.""" backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial) assert backend.get_gpio_pin_mode(2) is not GPIOPinMode.DIGITAL_OUTPUT with pytest.raises(ValueError): backend.write_gpio_pin_digital_state(2, True)