Skip to content

Commit 0c1a0bb

Browse files
bors[bot]Morgan Roff
and
Morgan Roff
authored
Merge #61
61: Fix active low behavior of GPIO output pins r=eldruin a=MorganR When an output pin is in active-low mode, the try_set_low and try_set_high functions currently incorrect behavior. In keeping with the input behavior for `try_is_high` and `try_is_low`, "high" and "low" should refer to the actual output voltage. This works as expected for pins that are not in active-low mode, but if a pin is in active-low mode, then the opposite happens: * calling `try_set_low` sets the voltage high * calling `try_set_high` sets the voltage low This bug and the fix have been verified using [this test program](https://github.com/MorganR/active-low-test). Co-authored-by: Morgan Roff <[email protected]>
2 parents b930d32 + 1637458 commit 0c1a0bb

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

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

1010
### Changed
1111

12+
- Modified `OutputPin` behavior for active-low pins to match `InputPin` behavior.
1213
- Set default features to build both sysfs and cdev pin types.
1314
- Removed `Pin` export, use `CdevPin` or `SysfsPin`.
1415
- Increased the Minimum Supported Rust Version to `1.36.0` due to an update of `gpio_cdev`.

src/cdev_pin.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ impl embedded_hal::digital::OutputPin for CdevPin {
1919
type Error = gpio_cdev::errors::Error;
2020

2121
fn try_set_low(&mut self) -> Result<(), Self::Error> {
22-
self.0.set_value(0)
22+
if self.1 {
23+
self.0.set_value(1)
24+
} else {
25+
self.0.set_value(0)
26+
}
2327
}
2428

2529
fn try_set_high(&mut self) -> Result<(), Self::Error> {
26-
self.0.set_value(1)
30+
if self.1 {
31+
self.0.set_value(0)
32+
} else {
33+
self.0.set_value(1)
34+
}
2735
}
2836
}
2937

src/sysfs_pin.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ impl embedded_hal::digital::OutputPin for SysfsPin {
3030
type Error = sysfs_gpio::Error;
3131

3232
fn try_set_low(&mut self) -> Result<(), Self::Error> {
33-
self.0.set_value(0)
33+
if self.0.get_active_low()? {
34+
self.0.set_value(1)
35+
} else {
36+
self.0.set_value(0)
37+
}
3438
}
3539

3640
fn try_set_high(&mut self) -> Result<(), Self::Error> {
37-
self.0.set_value(1)
41+
if self.0.get_active_low()? {
42+
self.0.set_value(0)
43+
} else {
44+
self.0.set_value(1)
45+
}
3846
}
3947
}
4048

0 commit comments

Comments
 (0)