liveBook · Manning

Outputpin

As explained in Embedded Software with Rust

OutputPin is an embedded-hal trait defining operations for driving a digital output pin high or low.

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

How Embedded Software with Rust covers it

How deep it goes
Explains the methods, mutable receiver, and error type, then demonstrates HAL delegation without detailing GPIO register operations.
Examples
Demonstrates LED pin implementations and a generic blinking function using high and low output operations.
What you can do after
Apply it via embedded_hal::digital::OutputPin to adapt GPIO pins for reusable driver code.
How it connects
Connects embedded-hal traits with hardware abstraction layers, GPIO pins, and generic drivers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.