1
1
//! Digital I/O
2
2
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
+
3
43
/// Single digital push-pull output pin
4
44
pub trait OutputPin {
5
45
/// Error type
@@ -16,6 +56,17 @@ pub trait OutputPin {
16
56
/// *NOTE* the actual electrical state of the pin may not actually be high, e.g. due to external
17
57
/// electrical sources
18
58
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
+ }
19
70
}
20
71
21
72
/// Push-pull output pin that can read its output state
0 commit comments