Skip to content

Commit 1d0f368

Browse files
committed
Add CdevPin module
The CdevPin export can be enabled with feature flag "gpio_cdev".
1 parent da2f57a commit 1d0f368

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ version = "0.3.0"
1111
[features]
1212
default = []
1313
gpio_sysfs = ["sysfs_gpio"]
14+
gpio_cdev = ["gpio-cdev"]
1415

1516
[dependencies]
1617
embedded-hal = { version = "0.2.3", features = ["unproven"] }
18+
gpio-cdev = { version = "0.2", optional = true }
1719
sysfs_gpio = { version = "0.5", optional = true }
1820

1921
i2cdev = "0.4.3"

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 CdevPin(pub gpio_cdev::LineHandle, bool);
7+
8+
impl CdevPin {
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<Self, gpio_cdev::errors::Error> {
13+
let info = handle.line().info()?;
14+
Ok(CdevPin(handle, info.is_active_low()))
15+
}
16+
}
17+
18+
impl hal::digital::v2::OutputPin for CdevPin {
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 CdevPin {
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 CdevPin {
47+
type Target = gpio_cdev::LineHandle;
48+
49+
fn deref(&self) -> &Self::Target {
50+
&self.0
51+
}
52+
}
53+
54+
impl ops::DerefMut for CdevPin {
55+
fn deref_mut(&mut self) -> &mut Self::Target {
56+
&mut self.0
57+
}
58+
}

src/lib.rs

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

1515
extern crate cast;
16+
extern crate core;
1617
extern crate embedded_hal as hal;
18+
#[cfg(feature = "gpio_cdev")]
19+
pub extern crate gpio_cdev;
1720
pub extern crate i2cdev;
1821
pub extern crate nb;
1922
pub extern crate serial_core;
@@ -34,10 +37,15 @@ use spidev::SpidevTransfer;
3437

3538
mod serial;
3639

40+
#[cfg(feature = "gpio_cdev")]
41+
/// Cdev Pin wrapper module
42+
mod cdev_pin;
3743
#[cfg(feature = "gpio_sysfs")]
3844
/// Sysfs Pin wrapper module
3945
mod sysfs_pin;
4046

47+
#[cfg(feature = "gpio_cdev")]
48+
pub use cdev_pin::CdevPin;
4149
pub use serial::Serial;
4250
#[cfg(feature = "gpio_sysfs")]
4351
pub use sysfs_pin::SysfsPin;

0 commit comments

Comments
 (0)