Skip to content

Commit 3239f28

Browse files
committed
Add chardev Pin module
The Pin export depends on feature flag with default being sysfs, "gpio_cdev" will export cdev Pin.
1 parent 491cc4f commit 3239f28

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ name = "linux-embedded-hal"
88
repository = "https://github.com/japaric/linux-embedded-hal"
99
version = "0.3.0"
1010

11+
[features]
12+
default = []
13+
gpio_cdev = []
14+
1115
[dependencies]
1216
embedded-hal = { version = "0.2.3", features = ["unproven"] }
1317
i2cdev = "0.4.3"
@@ -16,6 +20,7 @@ sysfs_gpio = "0.5"
1620
serial-unix = "0.4.0"
1721
serial-core = "0.4.0"
1822
nb = "0.1.1"
23+
gpio-cdev = "0.2"
1924

2025
[dev-dependencies]
2126
openpty = "0.1.0"

src/cdev_pin.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use core::ops;
2+
3+
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
4+
///
5+
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.LineHandle.html
6+
pub struct Pin(pub gpio_cdev::LineHandle, bool);
7+
8+
impl Pin {
9+
/// See [`gpio_cdev::Line::request`][0] for details.
10+
///
11+
/// [0]: https://docs.rs/gpio-cdev/0.2.0/gpio_cdev/struct.Line.html#method.request
12+
pub fn new(handle: gpio_cdev::LineHandle) -> Result<Pin, gpio_cdev::errors::Error> {
13+
let info = handle.line().info()?;
14+
Ok(Pin(handle, info.is_active_low()))
15+
}
16+
}
17+
18+
impl hal::digital::v2::OutputPin for Pin {
19+
type Error = gpio_cdev::errors::Error;
20+
21+
fn set_low(&mut self) -> Result<(), Self::Error> {
22+
self.0.set_value(0)
23+
}
24+
25+
fn set_high(&mut self) -> Result<(), Self::Error> {
26+
self.0.set_value(1)
27+
}
28+
}
29+
30+
impl hal::digital::v2::InputPin for Pin {
31+
type Error = gpio_cdev::errors::Error;
32+
33+
fn is_high(&self) -> Result<bool, Self::Error> {
34+
if !self.1 {
35+
self.0.get_value().map(|val| val != 0)
36+
} else {
37+
self.0.get_value().map(|val| val == 0)
38+
}
39+
}
40+
41+
fn is_low(&self) -> Result<bool, Self::Error> {
42+
self.is_high().map(|val| !val)
43+
}
44+
}
45+
46+
impl ops::Deref for Pin {
47+
type Target = gpio_cdev::LineHandle;
48+
49+
fn deref(&self) -> &Self::Target {
50+
&self.0
51+
}
52+
}
53+
54+
impl ops::DerefMut for Pin {
55+
fn deref_mut(&mut self) -> &mut Self::Target {
56+
&mut self.0
57+
}
58+
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#![deny(missing_docs)]
1414

1515
extern crate cast;
16+
extern crate core;
1617
extern crate embedded_hal as hal;
18+
pub extern crate gpio_cdev;
1719
pub extern crate i2cdev;
1820
pub extern crate nb;
1921
pub extern crate serial_core;
@@ -33,10 +35,15 @@ use spidev::SpidevTransfer;
3335

3436
mod serial;
3537

38+
/// Cdev Pin wrapper module
39+
pub mod cdev_pin;
3640
/// Sysfs Pin wrapper module
3741
pub mod sysfs_pin;
3842

43+
#[cfg(feature = "gpio_cdev")]
44+
pub use cdev_pin::Pin;
3945
pub use serial::Serial;
46+
#[cfg(not(feature = "gpio_cdev"))]
4047
pub use sysfs_pin::Pin;
4148

4249
/// Empty struct that provides delay functionality on top of `thread::sleep`

0 commit comments

Comments
 (0)