pub trait IoPin<TInput, TOutput> where
TInput: InputPin + IoPin<TInput, TOutput>,
TOutput: OutputPin + IoPin<TInput, TOutput>, {
type Error: Debug;
fn into_input_pin(self) -> Result<TInput, Self::Error>;
fn into_output_pin(self, state: PinState) -> Result<TOutput, Self::Error>;
}
Expand description
Single pin that can switch from input to output mode, and vice-versa.
Example use (assumes the Error
type is the same for the IoPin
,
InputPin
, and OutputPin
):
use core::time::Duration;
use embedded_hal::digital::blocking::{IoPin, InputPin, OutputPin};
pub fn ping_and_read<TInputPin, TOutputPin, TError: core::fmt::Debug>(
mut pin: TOutputPin, delay_fn: &dyn Fn(Duration) -> ()) -> Result<bool, TError>
where
TInputPin : InputPin<Error = TError> + IoPin<TInputPin, TOutputPin, Error = TError>,
TOutputPin : OutputPin<Error = TError> + IoPin<TInputPin, TOutputPin, Error = TError>,
{
// Ping
pin.set_low()?;
delay_fn(Duration::from_millis(10));
pin.set_high()?;
// Read
let pin = pin.into_input_pin()?;
delay_fn(Duration::from_millis(10));
pin.is_high()
}
Required Associated Types
Required Methods
fn into_input_pin(self) -> Result<TInput, Self::Error>
fn into_input_pin(self) -> Result<TInput, Self::Error>
Tries to convert this pin to input mode.
If the pin is already in input mode, this method should succeed.
fn into_output_pin(self, state: PinState) -> Result<TOutput, Self::Error>
fn into_output_pin(self, state: PinState) -> Result<TOutput, Self::Error>
Tries to convert this pin to output mode with the given initial state.
If the pin is already in the requested state, this method should succeed.