[][src]Module embedded_hal::digital::toggleable

If you can read and write the output state, a pin is toggleable by software.

use embedded_hal::digital::{OutputPin, StatefulOutputPin, ToggleableOutputPin};
use embedded_hal::digital::toggleable;
use core::convert::Infallible;

/// A virtual output pin that exists purely in software
struct MyPin {
    state: bool
}

impl OutputPin for MyPin {
   type Error = Infallible;

   fn try_set_low(&mut self) -> Result<(), Self::Error> {
       self.state = false;
       Ok(())
   }
   fn try_set_high(&mut self) -> Result<(), Self::Error> {
       self.state = true;
       Ok(())
   }
}

impl StatefulOutputPin for MyPin {
   fn try_is_set_low(&self) -> Result<bool, Self::Error> {
       Ok(!self.state)
   }
   fn try_is_set_high(&self) -> Result<bool, Self::Error> {
       Ok(self.state)
   }
}

/// Opt-in to the software implementation.
impl toggleable::Default for MyPin {}

let mut pin = MyPin { state: false };
pin.try_toggle().unwrap();
assert!(pin.try_is_set_high().unwrap());
pin.try_toggle().unwrap();
assert!(pin.try_is_set_low().unwrap());

Traits

Default

Software-driven toggle() implementation.