11//! Digital I/O
22
3+ use core:: { convert:: From , ops:: Not } ;
4+
5+ /// Digital output pin state
6+ ///
7+ /// Conversion from `bool` and logical negation are also implemented
8+ /// for this type.
9+ /// ```rust
10+ /// # use embedded_hal::digital::PinState;
11+ /// let state = PinState::from(false);
12+ /// assert_eq!(state, PinState::Low);
13+ /// assert_eq!(!state, PinState::High);
14+ /// ```
15+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
16+ pub enum PinState {
17+ /// Low pin state
18+ Low ,
19+ /// High pin state
20+ High ,
21+ }
22+
23+ impl From < bool > for PinState {
24+ fn from ( value : bool ) -> Self {
25+ match value {
26+ false => PinState :: Low ,
27+ true => PinState :: High ,
28+ }
29+ }
30+ }
31+
32+ impl Not for PinState {
33+ type Output = PinState ;
34+
35+ fn not ( self ) -> Self :: Output {
36+ match self {
37+ PinState :: High => PinState :: Low ,
38+ PinState :: Low => PinState :: High ,
39+ }
40+ }
41+ }
42+
343/// Single digital push-pull output pin
444pub trait OutputPin {
545 /// Error type
@@ -16,6 +56,17 @@ pub trait OutputPin {
1656 /// *NOTE* the actual electrical state of the pin may not actually be high, e.g. due to external
1757 /// electrical sources
1858 fn try_set_high ( & mut self ) -> Result < ( ) , Self :: Error > ;
59+
60+ /// Drives the pin high or low depending on the provided value
61+ ///
62+ /// *NOTE* the actual electrical state of the pin may not actually be high or low, e.g. due to external
63+ /// electrical sources
64+ fn try_set_state ( & mut self , state : PinState ) -> Result < ( ) , Self :: Error > {
65+ match state {
66+ PinState :: Low => self . try_set_low ( ) ,
67+ PinState :: High => self . try_set_high ( ) ,
68+ }
69+ }
1970}
2071
2172/// Push-pull output pin that can read its output state
0 commit comments