Skip to content

Commit 47964ca

Browse files
committed
PinState
1 parent 84c881d commit 47964ca

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- `PinState` and `get/set_state`.
13+
- Inherent methods for infallible digital operations.
1214
- Generic `into_alternate` and `into_alternate_open_drain`. Non-generic ones are deprecated
1315
- Internal implementation of GPIO Pin API changed to use Const Generics
1416
- `PinExt` trait. Make `ExtiPin` implementation generic

src/gpio.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ pub struct PushPull;
8181
/// Analog mode (type state)
8282
pub struct Analog;
8383

84+
/// Digital output pin state
85+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
86+
pub enum PinState {
87+
/// Low pin state
88+
Low,
89+
/// High pin state
90+
High,
91+
}
92+
8493
/// GPIO Pin speed selection
8594
pub enum Speed {
8695
Low = 0,
@@ -240,6 +249,23 @@ impl<MODE, const P: char> PXx<Output<MODE>, P> {
240249
}
241250
}
242251

252+
#[inline(always)]
253+
pub fn get_state(&self) -> PinState {
254+
if self.is_set_low() {
255+
PinState::Low
256+
} else {
257+
PinState::High
258+
}
259+
}
260+
261+
#[inline(always)]
262+
pub fn set_state(&mut self, state: PinState) {
263+
match state {
264+
PinState::Low => self.set_low(),
265+
PinState::High => self.set_high(),
266+
}
267+
}
268+
243269
#[inline(always)]
244270
pub fn is_set_high(&self) -> bool {
245271
!self.is_set_low()
@@ -846,6 +872,23 @@ impl<MODE, const P: char, const N: u8> PX<Output<MODE>, P, N> {
846872
}
847873
}
848874

875+
#[inline(always)]
876+
pub fn get_state(&self) -> PinState {
877+
if self.is_set_low() {
878+
PinState::Low
879+
} else {
880+
PinState::High
881+
}
882+
}
883+
884+
#[inline(always)]
885+
pub fn set_state(&mut self, state: PinState) {
886+
match state {
887+
PinState::Low => self.set_low(),
888+
PinState::High => self.set_high(),
889+
}
890+
}
891+
849892
#[inline(always)]
850893
pub fn is_set_high(&self) -> bool {
851894
!self.is_set_low()
@@ -1286,6 +1329,23 @@ impl<MODE> Pin<Output<MODE>> {
12861329
};
12871330
}
12881331

1332+
#[inline(always)]
1333+
pub fn get_state(&self) -> PinState {
1334+
if self.is_set_low() {
1335+
PinState::Low
1336+
} else {
1337+
PinState::High
1338+
}
1339+
}
1340+
1341+
#[inline(always)]
1342+
pub fn set_state(&mut self, state: PinState) {
1343+
match state {
1344+
PinState::Low => self.set_low(),
1345+
PinState::High => self.set_high(),
1346+
}
1347+
}
1348+
12891349
#[inline(always)]
12901350
pub fn is_set_high(&self) -> bool {
12911351
!self.is_set_low()

0 commit comments

Comments
 (0)