|
2 | 2 |
|
3 | 3 | /// Single digital output pin
|
4 | 4 | pub trait OutputPin {
|
5 |
| - /// Is the output pin high? |
6 |
| - fn is_high(&self) -> bool; |
7 |
| - |
8 |
| - /// Is the output pin low? |
9 |
| - fn is_low(&self) -> bool; |
10 |
| - |
11 | 5 | /// Sets the pin low
|
12 | 6 | fn set_low(&mut self);
|
13 | 7 |
|
14 | 8 | /// Sets the pin high
|
15 | 9 | fn set_high(&mut self);
|
16 | 10 | }
|
17 | 11 |
|
| 12 | +/// Output pin that can read its output state |
| 13 | +#[cfg(feature = "unproven")] |
| 14 | +pub trait StatefulOutputPin { |
| 15 | + /// Is the pin set to high? |
| 16 | + fn is_set_high(&self) -> bool; |
| 17 | + |
| 18 | + /// Is the pin set to low? |
| 19 | + fn is_set_low(&self) -> bool; |
| 20 | +} |
| 21 | + |
| 22 | +/// Output pin that can be toggled |
| 23 | +/// |
| 24 | +/// See [toggleable](toggleable) to use a software implementation if |
| 25 | +/// both [OutputPin](trait.OutputPin.html) and |
| 26 | +/// [StatefulOutputPin](trait.StatefulOutputPin.html) are |
| 27 | +/// implemented. Otherwise, implement this using hardware mechanisms. |
| 28 | +#[cfg(feature = "unproven")] |
| 29 | +pub trait ToggleableOutputPin { |
| 30 | + /// Toggle pin output. |
| 31 | + fn toggle(&mut self); |
| 32 | +} |
| 33 | + |
| 34 | +/// If you can read **and** write the output state, a pin is |
| 35 | +/// toggleable by software. |
| 36 | +/// |
| 37 | +/// ``` |
| 38 | +/// use embedded_hal::digital::{OutputPin, StatefulOutputPin, ToggleableOutputPin}; |
| 39 | +/// use embedded_hal::digital::toggleable; |
| 40 | +/// |
| 41 | +/// /// A virtual output pin that exists purely in software |
| 42 | +/// struct MyPin { |
| 43 | +/// state: bool |
| 44 | +/// } |
| 45 | +/// |
| 46 | +/// impl OutputPin for MyPin { |
| 47 | +/// fn set_low(&mut self) { |
| 48 | +/// self.state = false; |
| 49 | +/// } |
| 50 | +/// fn set_high(&mut self) { |
| 51 | +/// self.state = true; |
| 52 | +/// } |
| 53 | +/// } |
| 54 | +/// |
| 55 | +/// impl StatefulOutputPin for MyPin { |
| 56 | +/// fn is_set_low(&self) -> bool { |
| 57 | +/// !self.state |
| 58 | +/// } |
| 59 | +/// fn is_set_high(&self) -> bool { |
| 60 | +/// self.state |
| 61 | +/// } |
| 62 | +/// } |
| 63 | +/// |
| 64 | +/// /// Opt-in to the software implementation. |
| 65 | +/// impl toggleable::Default for MyPin {} |
| 66 | +/// |
| 67 | +/// let mut pin = MyPin { state: false }; |
| 68 | +/// pin.toggle(); |
| 69 | +/// assert!(pin.is_set_high()); |
| 70 | +/// pin.toggle(); |
| 71 | +/// assert!(pin.is_set_low()); |
| 72 | +/// ``` |
| 73 | +#[cfg(feature = "unproven")] |
| 74 | +pub mod toggleable { |
| 75 | + use super::{OutputPin, StatefulOutputPin, ToggleableOutputPin}; |
| 76 | + |
| 77 | + /// Software-driven `toggle()` implementation. |
| 78 | + pub trait Default: OutputPin + StatefulOutputPin {} |
| 79 | + |
| 80 | + impl<P> ToggleableOutputPin for P |
| 81 | + where |
| 82 | + P: Default, |
| 83 | + { |
| 84 | + /// Toggle pin output |
| 85 | + fn toggle(&mut self) { |
| 86 | + if self.is_set_low() { |
| 87 | + self.set_high(); |
| 88 | + } else { |
| 89 | + self.set_low(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
18 | 95 | /// Single digital input pin
|
19 | 96 | #[cfg(feature = "unproven")]
|
20 | 97 | pub trait InputPin {
|
|
0 commit comments