Module digital

Source
Available on crate feature eh1 only.
Expand description

Mock digital InputPin, OutputPin, and StatefulOutputPin implementations Also mock calls to Wait, assuming the embedded-hal-async feature is enabled.

use std::io::ErrorKind;

use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin};
use embedded_hal_mock::eh1::{
    digital::{Mock as PinMock, State as PinState, Transaction as PinTransaction},
    MockError,
};

let err = MockError::Io(ErrorKind::NotConnected);

// Configure expectations
let expectations = [
    PinTransaction::get(PinState::High),
    PinTransaction::get(PinState::High),
    PinTransaction::set(PinState::Low),
    PinTransaction::set(PinState::High).with_error(err.clone()),
    PinTransaction::get_state(PinState::High),
    PinTransaction::toggle(),
    PinTransaction::get_state(PinState::Low),
];

// Create pin
let mut pin = PinMock::new(&expectations);

// Run and test
assert_eq!(pin.is_high().unwrap(), true);
assert_eq!(pin.is_low().unwrap(), false);

pin.set_low().unwrap();
pin.set_high().expect_err("expected error return");

pin.is_set_high().unwrap();
pin.toggle().unwrap();
pin.is_set_low().unwrap();

pin.done();

// Update expectations
pin.update_expectations(&[]);
// ...
pin.done();

Structs§

Transaction
MockPin transaction

Enums§

Edgeembedded-hal-async
Digital pin edge enumeration
State
Digital pin value enumeration
TransactionKind
MockPin transaction kind.

Type Aliases§

Mock
Mock Pin implementation