embedded_hal/digital/
v1.rs

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