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_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()
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()
def test_backend_get_digital_state_requires_digital_pin() -> None: """Check that pins 14-19 are not supported by get digital state.""" backend = SBArduinoHardwareBackend("COM0", SBArduinoSerial) with pytest.raises(NotSupportedByHardwareError): backend.get_gpio_pin_digital_state(14)
def test_backend_get_digital_state_requires_pin_mode() -> None: """Check that pin must be in DIGITAL_OUTPUT mode for get 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.get_gpio_pin_digital_state(2)