22//!
33//! Version 2 / fallible traits. Infallible implementations should set Error to `!`.
44
5+ use core:: { convert:: From , ops:: Not } ;
6+
7+ /// Digital output pin state
8+ ///
9+ /// Conversion from `bool` and logical negation are also implemented
10+ /// for this type.
11+ /// ```rust
12+ /// # use embedded_hal::digital::v2::PinState;
13+ /// let state = PinState::from(false);
14+ /// assert_eq!(state, PinState::Low);
15+ /// assert_eq!(!state, PinState::High);
16+ /// ```
17+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
18+ pub enum PinState {
19+ /// Low pin state
20+ Low ,
21+ /// High pin state
22+ High ,
23+ }
24+
25+ impl From < bool > for PinState {
26+ fn from ( value : bool ) -> Self {
27+ match value {
28+ false => PinState :: Low ,
29+ true => PinState :: High ,
30+ }
31+ }
32+ }
33+
34+ impl Not for PinState {
35+ type Output = PinState ;
36+
37+ fn not ( self ) -> Self :: Output {
38+ match self {
39+ PinState :: High => PinState :: Low ,
40+ PinState :: Low => PinState :: High ,
41+ }
42+ }
43+ }
44+
545/// Single digital push-pull output pin
646pub trait OutputPin {
747 /// Error type
@@ -18,6 +58,17 @@ pub trait OutputPin {
1858 /// *NOTE* the actual electrical state of the pin may not actually be high, e.g. due to external
1959 /// electrical sources
2060 fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > ;
61+
62+ /// Drives the pin high or low depending on the provided value
63+ ///
64+ /// *NOTE* the actual electrical state of the pin may not actually be high or low, e.g. due to external
65+ /// electrical sources
66+ fn set_state ( & mut self , state : PinState ) -> Result < ( ) , Self :: Error > {
67+ match state {
68+ PinState :: Low => self . set_low ( ) ,
69+ PinState :: High => self . set_high ( ) ,
70+ }
71+ }
2172}
2273
2374/// Push-pull output pin that can read its output state
@@ -136,3 +187,30 @@ pub trait InputPin {
136187 /// Is the input pin low?
137188 fn is_low ( & self ) -> Result < bool , Self :: Error > ;
138189}
190+
191+ /// Single pin that can switch from input to output mode, and vice-versa.
192+ ///
193+ /// Example use (assumes the `Error` type is the same for the `IoPin`,
194+ /// `InputPin`, and `OutputPin`):
195+ ///
196+ /// *This trait is available if embedded-hal is built with the `"unproven"` feature.*
197+ #[ cfg( feature = "unproven" ) ]
198+ pub trait IoPin < TInput , TOutput >
199+ where
200+ TInput : InputPin + IoPin < TInput , TOutput > ,
201+ TOutput : OutputPin + IoPin < TInput , TOutput > ,
202+ {
203+ /// Error type.
204+ type Error ;
205+
206+ /// Tries to convert this pin to input mode.
207+ ///
208+ /// If the pin is already in input mode, this method should succeed.
209+ fn into_input_pin ( self ) -> Result < TInput , Self :: Error > ;
210+
211+ /// Tries to convert this pin to output mode with the given initial state.
212+ ///
213+ /// If the pin is already in the requested state, this method should
214+ /// succeed.
215+ fn into_output_pin ( self , state : PinState ) -> Result < TOutput , Self :: Error > ;
216+ }
0 commit comments