def test_pin_mode_setter() -> None: """Test the setter for the pin mode.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, driver, initial_mode=GPIOPinMode.DIGITAL_INPUT, hardware_modes={GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_OUTPUT}, ) assert driver._mode[0] is GPIOPinMode.DIGITAL_INPUT pin.mode = GPIOPinMode.DIGITAL_OUTPUT assert driver._mode[0] is GPIOPinMode.DIGITAL_OUTPUT with pytest.raises(NotSupportedByComponentError): pin.mode = GPIOPinMode.ANALOGUE_INPUT
def test_analogue_value_getter() -> None: """Test that we can get a scaled analogue value.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, driver, initial_mode=GPIOPinMode.DIGITAL_INPUT, hardware_modes={ GPIOPinMode.DIGITAL_OUTPUT, GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.ANALOGUE_INPUT, }, ) pin.mode = GPIOPinMode.ANALOGUE_INPUT assert pin.analogue_value == 0.6 with pytest.raises(BadGPIOPinModeError): pin.mode = GPIOPinMode.DIGITAL_OUTPUT _ = pin.analogue_value
def test_analogue_value_getter(): """Test that we can get a scaled analogue value.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, MockGPIOPinBoard(), driver, supported_modes=[ GPIOPinMode.DIGITAL_OUTPUT, GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.ANALOGUE_INPUT, ], ) pin.mode = GPIOPinMode.ANALOGUE_INPUT assert pin.analogue_value == 0.6 with pytest.raises(BadGPIOPinModeError): pin.mode = GPIOPinMode.DIGITAL_OUTPUT _ = pin.analogue_value
def test_pin_mode_setter(): """Test the setter for the pin mode.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, MockGPIOPinBoard(), driver, initial_mode=GPIOPinMode.DIGITAL_INPUT, supported_modes=[ GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_OUTPUT ], ) assert driver._mode[0] is GPIOPinMode.DIGITAL_INPUT pin.mode = GPIOPinMode.DIGITAL_OUTPUT assert driver._mode[0] is GPIOPinMode.DIGITAL_OUTPUT with pytest.raises(NotSupportedByHardwareError): pin.mode = GPIOPinMode.ANALOGUE_INPUT
def test_analogue_value_setter() -> None: """Test that we can set a scaled analogue value.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, driver, initial_mode=GPIOPinMode.ANALOGUE_OUTPUT, hardware_modes={ GPIOPinMode.ANALOGUE_OUTPUT, GPIOPinMode.PWM_OUTPUT, }, ) pin.mode = GPIOPinMode.ANALOGUE_OUTPUT pin.analogue_value = 0.6 pin.mode = GPIOPinMode.PWM_OUTPUT pin.analogue_value = 0.7 with pytest.raises(ValueError): pin.analogue_value = -1
def test_analogue_value_setter(): """Test that we can set a scaled analogue value.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, MockGPIOPinBoard(), driver, supported_modes=[ GPIOPinMode.ANALOGUE_OUTPUT, GPIOPinMode.PWM_OUTPUT, ], ) pin.mode = GPIOPinMode.ANALOGUE_OUTPUT pin.analogue_value = 0.6 pin.mode = GPIOPinMode.PWM_OUTPUT pin.analogue_value = 0.7 with pytest.raises(ValueError): pin.analogue_value = -1
def test_digital_state_getter() -> None: """Test that we can get the digital state correctly.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, driver, initial_mode=GPIOPinMode.DIGITAL_INPUT, hardware_modes={ GPIOPinMode.DIGITAL_OUTPUT, GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.DIGITAL_INPUT_PULLDOWN, GPIOPinMode.ANALOGUE_INPUT, }, ) # Digital Output pin.mode = GPIOPinMode.DIGITAL_OUTPUT assert pin.last_digital_write is driver._written_digital_state[0] driver._written_digital_state[0] = not driver._written_digital_state[0] assert pin.last_digital_write is driver._written_digital_state[0] with pytest.raises(BadGPIOPinModeError): _ = pin.digital_read() # Digital Input for mode in [ GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.DIGITAL_INPUT_PULLDOWN, ]: pin.mode = mode assert pin.digital_read() is driver._digital_state[0] driver._digital_state[0] = not driver._digital_state[0] assert pin.digital_read() is driver._digital_state[0] # Analogue pin.mode = GPIOPinMode.ANALOGUE_INPUT with pytest.raises(BadGPIOPinModeError): _ = pin.last_digital_write
def test_digital_state_getter(): """Test that we can get the digital state correctly.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, MockGPIOPinBoard(), driver, supported_modes=[ GPIOPinMode.DIGITAL_OUTPUT, GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.DIGITAL_INPUT_PULLDOWN, GPIOPinMode.ANALOGUE_INPUT, ], ) # Digital Output pin.mode = GPIOPinMode.DIGITAL_OUTPUT assert pin.digital_state is driver._written_digital_state[0] driver._written_digital_state[0] = not driver._written_digital_state[0] assert pin.digital_state is driver._written_digital_state[0] # Digital Input for mode in [ GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.DIGITAL_INPUT_PULLDOWN, ]: pin.mode = mode assert pin.digital_state is driver._digital_state[0] driver._digital_state[0] = not driver._digital_state[0] assert pin.digital_state is driver._digital_state[0] # Analogue pin.mode = GPIOPinMode.ANALOGUE_INPUT with pytest.raises(BadGPIOPinModeError): _ = pin.digital_state
def test_pwm_value_setter() -> None: """Test that we can set a scaled PWM value.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, driver, initial_mode=GPIOPinMode.PWM_OUTPUT, hardware_modes={ GPIOPinMode.PWM_OUTPUT, }, ) pin.mode = GPIOPinMode.PWM_OUTPUT pin.pwm_write(0.7) with pytest.raises(ValueError): pin.pwm_write(-1)
def test_digital_state_setter() -> None: """Test that we can set the digital state.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, driver, initial_mode=GPIOPinMode.DIGITAL_INPUT, hardware_modes={ GPIOPinMode.DIGITAL_OUTPUT, GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.ANALOGUE_INPUT, }, ) pin.mode = GPIOPinMode.DIGITAL_OUTPUT pin.digital_state = True assert driver._written_digital_state[0] pin.digital_state = False assert not driver._written_digital_state[0]
def test_digital_state_setter(): """Test that we can set the digital state.""" driver = MockGPIOPinDriver() pin = GPIOPin( 0, MockGPIOPinBoard(), driver, supported_modes=[ GPIOPinMode.DIGITAL_OUTPUT, GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_INPUT_PULLUP, GPIOPinMode.ANALOGUE_INPUT, ], ) pin.mode = GPIOPinMode.DIGITAL_OUTPUT pin.digital_state = True assert driver._written_digital_state[0] pin.digital_state = False assert not driver._written_digital_state[0]