Example #1
0
def test_required_pin_modes():
    """Test the runtime check for required pin modes."""
    driver = MockGPIOPinDriver()
    pin = GPIOPin(
        0,
        MockGPIOPinBoard(),
        driver,
        supported_modes=[
            GPIOPinMode.DIGITAL_OUTPUT,
            GPIOPinMode.DIGITAL_INPUT,
        ],
    )

    # 0
    pin._require_pin_modes([])

    # 1
    pin._require_pin_modes([GPIOPinMode.DIGITAL_OUTPUT])

    with pytest.raises(BadGPIOPinModeError):
        pin._require_pin_modes([GPIOPinMode.DIGITAL_INPUT_PULLUP])

    # 2
    pin._require_pin_modes([
        GPIOPinMode.DIGITAL_OUTPUT,
        GPIOPinMode.DIGITAL_INPUT,
    ])
Example #2
0
def test_required_pin_modes() -> None:
    """Test the runtime check for required pin modes."""
    driver = MockGPIOPinDriver()
    pin = GPIOPin(
        0,
        driver,
        initial_mode=GPIOPinMode.DIGITAL_OUTPUT,
        hardware_modes={
            GPIOPinMode.DIGITAL_OUTPUT,
            GPIOPinMode.DIGITAL_INPUT,
        },
    )

    # 0
    pin._require_pin_modes([])

    # 1
    pin._require_pin_modes([GPIOPinMode.DIGITAL_OUTPUT])

    with pytest.raises(BadGPIOPinModeError):
        pin._require_pin_modes([GPIOPinMode.DIGITAL_INPUT_PULLUP])

    # 2
    pin._require_pin_modes([
        GPIOPinMode.DIGITAL_OUTPUT,
        GPIOPinMode.DIGITAL_INPUT,
    ])
def test_ultrasound_sensor() -> None:
    """Test that we can instantiate an ultrasound sensor."""
    trigger = GPIOPin(
        0,
        MockGPIOPinDriver(),
        initial_mode=UltrasoundSensor,
        firmware_modes={UltrasoundSensor},
    )
    echo = GPIOPin(
        1,
        MockGPIOPinDriver(),
        initial_mode=UltrasoundSensor,
        firmware_modes={UltrasoundSensor},
    )

    u = UltrasoundSensor(trigger, echo, MockUltrasoundSensorDriver())

    time = u.pulse()

    assert time is not None
    assert type(time) is timedelta
    assert time.microseconds == 20000

    dist = u.distance()
    assert dist is not None
    assert type(dist) is float
    assert round(dist) == 3430
Example #4
0
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
Example #5
0
def test_firmware_mode_setter() -> None:
    """Test that the firmware mode of a pin can be set after instantation."""
    driver = MockGPIOPinDriver()
    pin = GPIOPin(
        0,
        driver,
        initial_mode=GPIOPinMode.ANALOGUE_OUTPUT,
        hardware_modes={
            GPIOPinMode.ANALOGUE_OUTPUT,
            GPIOPinMode.PWM_OUTPUT,
        },
    )

    assert Peripheral not in pin.firmware_modes

    pin.firmware_modes = {Peripheral}

    assert Peripheral in pin.firmware_modes
Example #6
0
def test_supported_modes_length() -> None:
    """Test that a pin cannot be created with zero supported modes."""
    driver = MockGPIOPinDriver()

    with pytest.raises(ValueError):
        GPIOPin(
            0,
            driver,
            initial_mode=GPIOPinMode.DIGITAL_INPUT,
            hardware_modes=set(),
        )
Example #7
0
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]
Example #8
0
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
Example #9
0
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
Example #10
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]
Example #11
0
def test_supported_modes_length():
    """Test that a pin cannot be created with zero supported modes."""
    driver = MockGPIOPinDriver()

    with pytest.raises(ValueError):
        GPIOPin(
            0,
            MockGPIOPinBoard(),
            driver,
            supported_modes=[],
        )
Example #12
0
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_ultrasound_no_support() -> None:
    """Test that we throw an error on an unsupported pin."""
    trigger = GPIOPin(
        0,
        MockGPIOPinDriver(),
        initial_mode=GPIOPinMode.DIGITAL_OUTPUT,
    )
    echo = GPIOPin(
        1,
        MockGPIOPinDriver(),
        initial_mode=UltrasoundSensor,
        firmware_modes={UltrasoundSensor},
    )

    with pytest.raises(NotSupportedByComponentError):
        UltrasoundSensor(
            trigger,
            echo,
            MockUltrasoundSensorDriver(),
            distance_mode=False,
        )
Example #14
0
def test_initial_mode() -> None:
    """Test that the initial mode of the pin is set correctly."""
    driver = MockGPIOPinDriver()

    # Implicit initial mode with default supported modes
    GPIOPin(0, driver, initial_mode=GPIOPinMode.DIGITAL_OUTPUT)
    assert driver._mode[0] is GPIOPinMode.DIGITAL_OUTPUT

    # Explicit initial mode with default supported modes
    GPIOPin(
        2,
        driver,
        initial_mode=GPIOPinMode.DIGITAL_OUTPUT,
    )
    assert driver._mode[2] is GPIOPinMode.DIGITAL_OUTPUT

    # Explicit initial mode with specified supported modes
    GPIOPin(
        2,
        driver,
        initial_mode=GPIOPinMode.DIGITAL_INPUT,
        hardware_modes={GPIOPinMode.DIGITAL_INPUT},
    )
    assert driver._mode[2] is GPIOPinMode.DIGITAL_INPUT

    # Unsupported explicit initial mode with default supported modes
    with pytest.raises(NotSupportedByComponentError):
        GPIOPin(
            2,
            driver,
            initial_mode=GPIOPinMode.DIGITAL_INPUT,
        )
    # Unsupported explicit initial mode with specified supported modes
    with pytest.raises(NotSupportedByComponentError):
        GPIOPin(
            2,
            driver,
            initial_mode=GPIOPinMode.DIGITAL_INPUT,
            hardware_modes={GPIOPinMode.DIGITAL_OUTPUT},
        )
Example #15
0
def test_pin_mode_getter() -> None:
    """Test the mode getter."""
    driver = MockGPIOPinDriver()

    pin = GPIOPin(
        0,
        driver,
        initial_mode=GPIOPinMode.DIGITAL_INPUT,
        hardware_modes={GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_OUTPUT},
    )

    assert pin.mode is GPIOPinMode.DIGITAL_INPUT
    driver._mode[0] = GPIOPinMode.DIGITAL_OUTPUT
    assert pin.mode is GPIOPinMode.DIGITAL_OUTPUT
Example #16
0
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.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_ultrasound_no_distance() -> None:
    """Test that we can't get the distance if it's disabled."""
    trigger = GPIOPin(
        0,
        MockGPIOPinDriver(),
        initial_mode=UltrasoundSensor,
        firmware_modes={UltrasoundSensor},
    )
    echo = GPIOPin(
        1,
        MockGPIOPinDriver(),
        initial_mode=UltrasoundSensor,
        firmware_modes={UltrasoundSensor},
    )

    u = UltrasoundSensor(
        trigger,
        echo,
        MockUltrasoundSensorDriver(),
        distance_mode=False,
    )

    with pytest.raises(Exception):
        u.distance()
Example #18
0
def test_derived_mode_is_possible() -> None:
    """Check that it is possible to support a derived component."""
    driver = MockGPIOPinDriver()
    GPIOPin(
        0,
        driver,
        initial_mode=GPIOPinMode.ANALOGUE_OUTPUT,
        hardware_modes={
            GPIOPinMode.ANALOGUE_OUTPUT,
            GPIOPinMode.PWM_OUTPUT,
        },
        firmware_modes={
            Peripheral,
        },
    )
Example #19
0
def test_pin_mode_getter():
    """Test the mode getter."""
    driver = MockGPIOPinDriver()

    pin = GPIOPin(
        0,
        MockGPIOPinBoard(),
        driver,
        initial_mode=GPIOPinMode.DIGITAL_INPUT,
        supported_modes=[
            GPIOPinMode.DIGITAL_INPUT, GPIOPinMode.DIGITAL_OUTPUT
        ],
    )

    assert pin.mode is GPIOPinMode.DIGITAL_INPUT
    driver._mode[0] = GPIOPinMode.DIGITAL_OUTPUT
    assert pin.mode is GPIOPinMode.DIGITAL_OUTPUT
Example #20
0
def test_initial_mode():
    """Test that the initial mode of the pin is set correctly."""
    driver = MockGPIOPinDriver()

    # Implicit initial mode with default supported modes
    GPIOPin(0, MockGPIOPinBoard(), driver)
    assert driver._mode[0] is GPIOPinMode.DIGITAL_OUTPUT

    # Implicit initial mode with specified supported modes
    GPIOPin(
        1,
        MockGPIOPinBoard(),
        driver,
        supported_modes=[GPIOPinMode.DIGITAL_INPUT],
    )
    assert driver._mode[1] is GPIOPinMode.DIGITAL_INPUT

    # Explicit initial mode with default supported modes
    GPIOPin(
        2,
        MockGPIOPinBoard(),
        driver,
        initial_mode=GPIOPinMode.DIGITAL_OUTPUT,
    )
    assert driver._mode[2] is GPIOPinMode.DIGITAL_OUTPUT

    # Explicit initial mode with specified supported modes
    GPIOPin(
        2,
        MockGPIOPinBoard(),
        driver,
        initial_mode=GPIOPinMode.DIGITAL_INPUT,
        supported_modes=[GPIOPinMode.DIGITAL_INPUT],
    )
    assert driver._mode[2] is GPIOPinMode.DIGITAL_INPUT

    # Unsupported explicit initial mode with default supported modes
    with pytest.raises(NotSupportedByHardwareError):
        GPIOPin(
            2,
            MockGPIOPinBoard(),
            driver,
            initial_mode=GPIOPinMode.DIGITAL_INPUT,
        )
    # Unsupported explicit initial mode with specified supported modes
    with pytest.raises(NotSupportedByHardwareError):
        GPIOPin(
            2,
            MockGPIOPinBoard(),
            driver,
            initial_mode=GPIOPinMode.DIGITAL_INPUT,
            supported_modes=[GPIOPinMode.DIGITAL_OUTPUT],
        )
Example #21
0
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)
Example #22
0
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_write(0.6)

    with pytest.raises(ValueError):
        pin.analogue_write(-1)
Example #23
0
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
Example #24
0
def test_gpio_pin_instantiation() -> None:
    """Test that we can instantiate a GPIO pin."""
    GPIOPin(0, MockGPIOPinDriver(), initial_mode=GPIOPinMode.DIGITAL_OUTPUT)
Example #25
0
def test_gpio_pin_interface_class() -> None:
    """Test that the GPIO pin Interface class is a GPIOPinInterface."""
    assert GPIOPin.interface_class() is GPIOPinInterface
Example #26
0
def test_gpio_pin_identifier() -> None:
    """Test the identifier attribute of the component."""
    component = GPIOPin(0,
                        MockGPIOPinDriver(),
                        initial_mode=GPIOPinMode.DIGITAL_OUTPUT)
    assert component.identifier == 0
Example #27
0
def test_gpio_pin_instantiation():
    """Test that we can instantiate a GPIO pin."""
    GPIOPin(0, MockGPIOPinBoard(), MockGPIOPinDriver())