#pin #gpio-pin #gpio #hal #inverted #io

no-std inverted-pin

Embedded-hal input/output pin trait implementations with inverted level logic

3 releases (1 stable)

1.0.0 Jan 19, 2024
0.2.0 Feb 18, 2022
0.1.0 Feb 17, 2022

#1996 in Embedded development

Download history 20/week @ 2024-07-29 6/week @ 2024-09-23 2/week @ 2024-10-07 48/week @ 2024-10-14 40/week @ 2024-10-21 14/week @ 2024-10-28 18/week @ 2024-11-04

120 downloads per month
Used in quectel-bg77

MIT/Apache

12KB
60 lines

Inverted Input/Output Pin Implementations

crates.io Docs MSRV Build Status Coverage Status

This provides implementations of the input/output pin embedded-hal traits with inverted logic.

For example, an InvertedPin can wrap an OutputPin and when setting it low, it will set the wrapped OutputPin high. It works similarly for an InputPin as well.

This is useful when dealing with pins that use a logic that is inverted with respect to what the rest of the system expects.

Since an InvertedPin implements the OutputPin and InputPin traits as well, it can be used just like any other OutputPin or InputPin and serves as a drop-in replacement of the wrapped pin.

Usage

This example demonstrates how the same driver can operate with either a normal or an inverted output pin.

use embedded_hal::digital::OutputPin;
use inverted_pin::InvertedPin;
use linux_embedded_hal::SysfsPin;

struct Driver<P> {
    output: P,
}

impl<P, E> Driver<P>
where
    P: OutputPin<Error = E>,
{
    fn new(pin: P) -> Self {
        Driver { output: pin }
    }

    fn do_something(&mut self) -> Result<(), E> {
        // ...
        self.output.set_high()
    }

    fn destroy(self) -> P {
        self.output
    }
}

fn main() {
    // The same driver can operate with either a normal or an inverted pin.
    let real_pin = SysfsPin::new(25);
    let mut driver_with_real_pin = Driver::new(real_pin);
    driver_with_real_pin.do_something().unwrap();
    let real_pin = driver_with_real_pin.destroy();

    let inverted_pin = InvertedPin::new(real_pin);
    let mut driver_with_inverted_pin = Driver::new(inverted_pin);
    driver_with_inverted_pin.do_something().unwrap();
}

Support

For questions, issues, feature requests, and other changes, please file an issue in the github project.

License

Licensed under either of

at your option.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~56KB