Skip to content

Commit 1ef9f26

Browse files
committed
backport digital
1 parent 77b262d commit 1ef9f26

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/digital/v2.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,46 @@
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
646
pub 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+
}

src/digital/v2_compat.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ mod tests {
116116
}
117117

118118
#[allow(deprecated)]
119+
#[cfg(feature = "unproven")]
119120
impl v1::StatefulOutputPin for OldOutputPinImpl {
120121
fn is_set_low(&self) -> bool {
121122
self.state == false
@@ -127,6 +128,7 @@ mod tests {
127128
}
128129

129130
#[allow(deprecated)]
131+
#[cfg(feature = "unproven")]
130132
impl v1::toggleable::Default for OldOutputPinImpl {}
131133

132134
struct NewOutputPinConsumer<T: v2::OutputPin> {
@@ -142,10 +144,12 @@ mod tests {
142144
}
143145
}
144146

147+
#[cfg(feature = "unproven")]
145148
struct NewToggleablePinConsumer<T: v2::ToggleableOutputPin> {
146149
_pin: T,
147150
}
148151

152+
#[cfg(feature = "unproven")]
149153
impl<T> NewToggleablePinConsumer<T>
150154
where
151155
T: v2::ToggleableOutputPin,
@@ -156,6 +160,7 @@ mod tests {
156160
}
157161

158162
#[test]
163+
#[cfg(feature = "unproven")]
159164
fn v2_v1_toggleable_implicit() {
160165
let i = OldOutputPinImpl { state: false };
161166
let _c = NewToggleablePinConsumer::new(i);
@@ -212,6 +217,7 @@ mod tests {
212217

213218
#[cfg(feature = "unproven")]
214219
#[test]
220+
#[cfg(feature = "unproven")]
215221
fn v2_v1_input_implicit() {
216222
let i = OldInputPinImpl { state: false };
217223
let _c = NewInputPinConsumer::new(i);

0 commit comments

Comments
 (0)