Skip to content

Commit 6f17287

Browse files
bors[bot]eldruin
andauthored
Merge #239
239: Add try_set_state method for OutputPin r=therealprof a=eldruin This is an implementation of #200 to gather some opinions and so we can either accept it or close the issue. This was earlier discussed at #44. I added a conversion from `bool` following the usual convention as well as an `ops::Not` implementation as suggested in #200, which seemed appropriate. I also added a default implementation for the `try_set_state` method. This bears the question whether a default implementation for `try_set_high()` / `try_set_low()` by using `try_set_state()` would be useful, so that potential implementors can choose to implement less methods. It should be noted that adding a default implementation for all 3 methods has the somewhat amusing property of generating an endless loop if none is overwritten. Closes #200 Co-authored-by: Diego Barrios Romero <[email protected]>
2 parents 6167156 + f90d9d1 commit 6f17287

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Added
1212
- 10-bit addressing mode for I2C traits.
13+
- `try_set_state` method for `OutputPin` using an input `PinState` value.
1314

1415
### Changed
1516

src/digital.rs

+51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
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
444
pub 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

Comments
 (0)