|
| 1 | +/// Digital I/O |
| 2 | +
|
| 3 | +/// Single digital push-pull output pin |
| 4 | +pub trait OutputPin { |
| 5 | + /// Error type |
| 6 | + type Error; |
| 7 | + |
| 8 | + /// Drives the pin low |
| 9 | + /// |
| 10 | + /// *NOTE* the actual electrical state of the pin may not actually be low, e.g. due to external |
| 11 | + /// electrical sources |
| 12 | + fn set_low(&mut self) -> Result<(), Self::Error>; |
| 13 | + |
| 14 | + /// Drives the pin high |
| 15 | + /// |
| 16 | + /// *NOTE* the actual electrical state of the pin may not actually be high, e.g. due to external |
| 17 | + /// electrical sources |
| 18 | + fn set_high(&mut self) -> Result<(), Self::Error>; |
| 19 | +} |
| 20 | + |
| 21 | +/// Push-pull output pin that can read its output state |
| 22 | +/// |
| 23 | +/// *This trait is available if embedded-hal is built with the `"unproven"` feature.* |
| 24 | +#[cfg(feature = "unproven")] |
| 25 | +pub trait StatefulOutputPin : OutputPin { |
| 26 | + /// Is the pin in drive high mode? |
| 27 | + /// |
| 28 | + /// *NOTE* this does *not* read the electrical state of the pin |
| 29 | + fn is_set_high(&self) -> Result<bool, Self::Error>; |
| 30 | + |
| 31 | + /// Is the pin in drive low mode? |
| 32 | + /// |
| 33 | + /// *NOTE* this does *not* read the electrical state of the pin |
| 34 | + fn is_set_low(&self) -> Result<bool, Self::Error>; |
| 35 | +} |
| 36 | + |
| 37 | +/// Output pin that can be toggled |
| 38 | +/// |
| 39 | +/// *This trait is available if embedded-hal is built with the `"unproven"` feature.* |
| 40 | +/// |
| 41 | +/// See [toggleable](toggleable) to use a software implementation if |
| 42 | +/// both [OutputPin](trait.OutputPin.html) and |
| 43 | +/// [StatefulOutputPin](trait.StatefulOutputPin.html) are |
| 44 | +/// implemented. Otherwise, implement this using hardware mechanisms. |
| 45 | +#[cfg(feature = "unproven")] |
| 46 | +pub trait ToggleableOutputPin { |
| 47 | + /// Error type |
| 48 | + type Error; |
| 49 | + |
| 50 | + /// Toggle pin output. |
| 51 | + fn toggle(&mut self) -> Result<(), Self::Error>; |
| 52 | +} |
| 53 | + |
| 54 | +/// If you can read **and** write the output state, a pin is |
| 55 | +/// toggleable by software. |
| 56 | +/// |
| 57 | +/// ``` |
| 58 | +/// use embedded_hal::digital::v2::{OutputPin, StatefulOutputPin, ToggleableOutputPin}; |
| 59 | +/// use embedded_hal::digital::v2::toggleable; |
| 60 | +/// |
| 61 | +/// /// A virtual output pin that exists purely in software |
| 62 | +/// struct MyPin { |
| 63 | +/// state: bool |
| 64 | +/// } |
| 65 | +/// |
| 66 | +/// impl OutputPin for MyPin { |
| 67 | +/// type Error = void::Void; |
| 68 | +/// |
| 69 | +/// fn set_low(&mut self) -> Result<(), Self::Error> { |
| 70 | +/// self.state = false; |
| 71 | +/// Ok(()) |
| 72 | +/// } |
| 73 | +/// fn set_high(&mut self) -> Result<(), Self::Error> { |
| 74 | +/// self.state = true; |
| 75 | +/// Ok(()) |
| 76 | +/// } |
| 77 | +/// } |
| 78 | +/// |
| 79 | +/// impl StatefulOutputPin for MyPin { |
| 80 | +/// fn is_set_low(&self) -> Result<bool, Self::Error> { |
| 81 | +/// Ok(!self.state) |
| 82 | +/// } |
| 83 | +/// fn is_set_high(&self) -> Result<bool, Self::Error> { |
| 84 | +/// Ok(self.state) |
| 85 | +/// } |
| 86 | +/// } |
| 87 | +/// |
| 88 | +/// /// Opt-in to the software implementation. |
| 89 | +/// impl toggleable::Default for MyPin {} |
| 90 | +/// |
| 91 | +/// let mut pin = MyPin { state: false }; |
| 92 | +/// pin.toggle().unwrap(); |
| 93 | +/// assert!(pin.is_set_high().unwrap()); |
| 94 | +/// pin.toggle().unwrap(); |
| 95 | +/// assert!(pin.is_set_low().unwrap()); |
| 96 | +/// ``` |
| 97 | +#[cfg(feature = "unproven")] |
| 98 | +pub mod toggleable { |
| 99 | + use super::{OutputPin, StatefulOutputPin, ToggleableOutputPin}; |
| 100 | + |
| 101 | + /// Software-driven `toggle()` implementation. |
| 102 | + /// |
| 103 | + /// *This trait is available if embedded-hal is built with the `"unproven"` feature.* |
| 104 | + pub trait Default: OutputPin + StatefulOutputPin {} |
| 105 | + |
| 106 | + impl<P> ToggleableOutputPin for P |
| 107 | + where |
| 108 | + P: Default, |
| 109 | + { |
| 110 | + type Error = P::Error; |
| 111 | + |
| 112 | + /// Toggle pin output |
| 113 | + fn toggle(&mut self) -> Result<(), Self::Error> { |
| 114 | + if self.is_set_low()? { |
| 115 | + self.set_high() |
| 116 | + } else { |
| 117 | + self.set_low() |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Single digital input pin |
| 124 | +/// |
| 125 | +/// *This trait is available if embedded-hal is built with the `"unproven"` feature.* |
| 126 | +#[cfg(feature = "unproven")] |
| 127 | +pub trait InputPin { |
| 128 | + /// Error type |
| 129 | + type Error; |
| 130 | + |
| 131 | + /// Is the input pin high? |
| 132 | + fn is_high(&self) -> Result<bool, Self::Error>; |
| 133 | + |
| 134 | + /// Is the input pin low? |
| 135 | + fn is_low(&self) -> Result<bool, Self::Error>; |
| 136 | +} |
0 commit comments