OutputPin defines mutable operations for setting a digital pin high or low. Each operation returns a Result, allowing implementations to report errors; simple GPIO implementations can use Infallible. section 3.5.4
pub trait OutputPin {
/// The error type returned by the methods.
type Error;
/// Set the pin to high logic level.
fn set_high(&mut self) -> Result<(), Self::Error>;
/// Set the pin to low logic level.
fn set_low(&mut self) -> Result<(), Self::Error>;
}
The trait’s two methods provide a shared interface for driving digital outputs. section 3.5.4
A hardware abstraction layer (HAL) implements the trait for its pin types, forwarding calls to platform-specific GPIO operations. Generic driver code can then use any pin that implements OutputPin, such as in an LED-blinking function. section 3.5.4
impl embedded_hal::digital::OutputPin for LedPin {
type Error = core::convert::Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
// Delegates to the HAL’s method for setting the pin high.
self.pin.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
// Delegates to the HAL’s method for clearing the pin.
self.pin.set_low();
Ok(())
}
}
This implementation delegates pin changes to the HAL and uses Infallible as its error type. section 3.5.4
