Skip to content

Commit c98d82e

Browse files
committed
Add GPIO cdev feature
This feature adds possibility to use newer gpio cdev instead of deprecated gpio sysfs. At the same time maintains backward compatibility as default feature set is still using sysfs.
1 parent 5e8ec37 commit c98d82e

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ name = "linux-embedded-hal"
88
repository = "https://github.com/japaric/linux-embedded-hal"
99
version = "0.3.0"
1010

11+
[features]
12+
default = ["sysfs_gpio"]
13+
14+
gpio_cdev = ["gpio-cdev"]
15+
1116
[dependencies]
1217
embedded-hal = { version = "0.2.3", features = ["unproven"] }
1318
i2cdev = "0.4.3"
1419
spidev = "0.4"
15-
sysfs_gpio = "0.5"
1620
serial-unix = "0.4.0"
1721
serial-core = "0.4.0"
1822
nb = "0.1.1"
23+
sysfs_gpio = { version = "0.5", optional = true }
24+
gpio-cdev = { version="0.2", optional = true }
1925

2026
[dev-dependencies]
2127
openpty = "0.1.0"

src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515
extern crate cast;
1616
extern crate embedded_hal as hal;
17+
#[cfg(feature = "gpio_cdev")]
18+
pub extern crate gpio_cdev;
1719
pub extern crate i2cdev;
1820
pub extern crate nb;
1921
pub extern crate serial_core;
2022
pub extern crate serial_unix;
2123
pub extern crate spidev;
24+
#[cfg(not(feature = "gpio_cdev"))]
2225
pub extern crate sysfs_gpio;
2326

2427
use std::io::{self, Write};
@@ -92,11 +95,30 @@ impl hal::blocking::delay::DelayMs<u64> for Delay {
9295
}
9396
}
9497

98+
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
99+
///
100+
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.LineHandle.html
101+
#[cfg(feature = "gpio_cdev")]
102+
pub struct Pin(pub gpio_cdev::LineHandle, pub bool);
103+
104+
#[cfg(feature = "gpio_cdev")]
105+
impl Pin {
106+
/// See [`gpio_cdev::Line::request`][0] for details.
107+
///
108+
/// [0]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.Line.html#method.request
109+
pub fn new(handle: gpio_cdev::LineHandle) -> Result<Pin, gpio_cdev::errors::Error> {
110+
let info = handle.line().info()?;
111+
Ok(Pin(handle, info.is_active_low()))
112+
}
113+
}
114+
95115
/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
96116
///
97117
/// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html
118+
#[cfg(not(feature = "gpio_cdev"))]
98119
pub struct Pin(pub sysfs_gpio::Pin);
99120

121+
#[cfg(not(feature = "gpio_cdev"))]
100122
impl Pin {
101123
/// See [`sysfs_gpio::Pin::new`][0] for details.
102124
///
@@ -117,6 +139,9 @@ impl Pin {
117139
}
118140

119141
impl hal::digital::v2::OutputPin for Pin {
142+
#[cfg(feature = "gpio_cdev")]
143+
type Error = gpio_cdev::errors::Error;
144+
#[cfg(not(feature = "gpio_cdev"))]
120145
type Error = sysfs_gpio::Error;
121146

122147
fn set_low(&mut self) -> Result<(), Self::Error> {
@@ -129,8 +154,20 @@ impl hal::digital::v2::OutputPin for Pin {
129154
}
130155

131156
impl hal::digital::v2::InputPin for Pin {
157+
#[cfg(feature = "gpio_cdev")]
158+
type Error = gpio_cdev::errors::Error;
159+
#[cfg(not(feature = "gpio_cdev"))]
132160
type Error = sysfs_gpio::Error;
133161

162+
#[cfg(feature = "gpio_cdev")]
163+
fn is_high(&self) -> Result<bool, Self::Error> {
164+
if !self.1 {
165+
self.0.get_value().map(|val| val != 0)
166+
} else {
167+
self.0.get_value().map(|val| val == 0)
168+
}
169+
}
170+
#[cfg(not(feature = "gpio_cdev"))]
134171
fn is_high(&self) -> Result<bool, Self::Error> {
135172
if !self.0.get_active_low()? {
136173
self.0.get_value().map(|val| val != 0)
@@ -145,6 +182,9 @@ impl hal::digital::v2::InputPin for Pin {
145182
}
146183

147184
impl ops::Deref for Pin {
185+
#[cfg(feature = "gpio_cdev")]
186+
type Target = gpio_cdev::LineHandle;
187+
#[cfg(not(feature = "gpio_cdev"))]
148188
type Target = sysfs_gpio::Pin;
149189

150190
fn deref(&self) -> &Self::Target {

0 commit comments

Comments
 (0)