Skip to content

Commit da2f57a

Browse files
committed
Move SysfsPin to its own module
The SysfsPin export can be enabled with feature flag "gpio_sysfs".
1 parent 7aec9f6 commit da2f57a

File tree

3 files changed

+81
-67
lines changed

3 files changed

+81
-67
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ 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_sysfs = ["sysfs_gpio"]
14+
1115
[dependencies]
1216
embedded-hal = { version = "0.2.3", features = ["unproven"] }
17+
sysfs_gpio = { version = "0.5", optional = true }
18+
1319
i2cdev = "0.4.3"
1420
spidev = "0.4"
15-
sysfs_gpio = "0.5"
1621
serial-unix = "0.4.0"
1722
serial-core = "0.4.0"
1823
nb = "0.1.1"

src/lib.rs

Lines changed: 7 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub extern crate nb;
1919
pub extern crate serial_core;
2020
pub extern crate serial_unix;
2121
pub extern crate spidev;
22+
#[cfg(feature = "gpio_sysfs")]
2223
pub extern crate sysfs_gpio;
2324

2425
use std::io::{self, Write};
@@ -33,7 +34,13 @@ use spidev::SpidevTransfer;
3334

3435
mod serial;
3536

37+
#[cfg(feature = "gpio_sysfs")]
38+
/// Sysfs Pin wrapper module
39+
mod sysfs_pin;
40+
3641
pub use serial::Serial;
42+
#[cfg(feature = "gpio_sysfs")]
43+
pub use sysfs_pin::SysfsPin;
3744

3845
/// Empty struct that provides delay functionality on top of `thread::sleep`
3946
pub struct Delay;
@@ -92,72 +99,6 @@ impl hal::blocking::delay::DelayMs<u64> for Delay {
9299
}
93100
}
94101

95-
/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
96-
///
97-
/// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html
98-
pub struct Pin(pub sysfs_gpio::Pin);
99-
100-
impl Pin {
101-
/// See [`sysfs_gpio::Pin::new`][0] for details.
102-
///
103-
/// [0]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html#method.new
104-
pub fn new(pin_num: u64) -> Pin {
105-
Pin(sysfs_gpio::Pin::new(pin_num))
106-
}
107-
108-
/// See [`sysfs_gpio::Pin::from_path`][0] for details.
109-
///
110-
/// [0]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html#method.from_path
111-
pub fn from_path<P>(path: P) -> sysfs_gpio::Result<Pin>
112-
where
113-
P: AsRef<Path>,
114-
{
115-
sysfs_gpio::Pin::from_path(path).map(Pin)
116-
}
117-
}
118-
119-
impl hal::digital::v2::OutputPin for Pin {
120-
type Error = sysfs_gpio::Error;
121-
122-
fn set_low(&mut self) -> Result<(), Self::Error> {
123-
self.0.set_value(0)
124-
}
125-
126-
fn set_high(&mut self) -> Result<(), Self::Error> {
127-
self.0.set_value(1)
128-
}
129-
}
130-
131-
impl hal::digital::v2::InputPin for Pin {
132-
type Error = sysfs_gpio::Error;
133-
134-
fn is_high(&self) -> Result<bool, Self::Error> {
135-
if !self.0.get_active_low()? {
136-
self.0.get_value().map(|val| val != 0)
137-
} else {
138-
self.0.get_value().map(|val| val == 0)
139-
}
140-
}
141-
142-
fn is_low(&self) -> Result<bool, Self::Error> {
143-
self.is_high().map(|val| !val)
144-
}
145-
}
146-
147-
impl ops::Deref for Pin {
148-
type Target = sysfs_gpio::Pin;
149-
150-
fn deref(&self) -> &Self::Target {
151-
&self.0
152-
}
153-
}
154-
155-
impl ops::DerefMut for Pin {
156-
fn deref_mut(&mut self) -> &mut Self::Target {
157-
&mut self.0
158-
}
159-
}
160-
161102
/// Newtype around [`i2cdev::linux::LinuxI2CDevice`] that implements the `embedded-hal` traits
162103
///
163104
/// [`i2cdev::linux::LinuxI2CDevice`]: https://docs.rs/i2cdev/0.3.1/i2cdev/linux/struct.LinuxI2CDevice.html

src/sysfs_pin.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::ops;
2+
use std::path::Path;
3+
4+
/// Newtype around [`sysfs_gpio::Pin`] that implements the `embedded-hal` traits
5+
///
6+
/// [`sysfs_gpio::Pin`]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html
7+
pub struct SysfsPin(pub sysfs_gpio::Pin);
8+
9+
impl SysfsPin {
10+
/// See [`sysfs_gpio::Pin::new`][0] for details.
11+
///
12+
/// [0]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html#method.new
13+
pub fn new(pin_num: u64) -> Self {
14+
SysfsPin(sysfs_gpio::Pin::new(pin_num))
15+
}
16+
17+
/// See [`sysfs_gpio::Pin::from_path`][0] for details.
18+
///
19+
/// [0]: https://docs.rs/sysfs_gpio/0.5.1/sysfs_gpio/struct.Pin.html#method.from_path
20+
pub fn from_path<P>(path: P) -> sysfs_gpio::Result<Self>
21+
where
22+
P: AsRef<Path>,
23+
{
24+
sysfs_gpio::Pin::from_path(path).map(SysfsPin)
25+
}
26+
}
27+
28+
impl hal::digital::v2::OutputPin for SysfsPin {
29+
type Error = sysfs_gpio::Error;
30+
31+
fn set_low(&mut self) -> Result<(), Self::Error> {
32+
self.0.set_value(0)
33+
}
34+
35+
fn set_high(&mut self) -> Result<(), Self::Error> {
36+
self.0.set_value(1)
37+
}
38+
}
39+
40+
impl hal::digital::v2::InputPin for SysfsPin {
41+
type Error = sysfs_gpio::Error;
42+
43+
fn is_high(&self) -> Result<bool, Self::Error> {
44+
if !self.0.get_active_low()? {
45+
self.0.get_value().map(|val| val != 0)
46+
} else {
47+
self.0.get_value().map(|val| val == 0)
48+
}
49+
}
50+
51+
fn is_low(&self) -> Result<bool, Self::Error> {
52+
self.is_high().map(|val| !val)
53+
}
54+
}
55+
56+
impl ops::Deref for SysfsPin {
57+
type Target = sysfs_gpio::Pin;
58+
59+
fn deref(&self) -> &Self::Target {
60+
&self.0
61+
}
62+
}
63+
64+
impl ops::DerefMut for SysfsPin {
65+
fn deref_mut(&mut self) -> &mut Self::Target {
66+
&mut self.0
67+
}
68+
}

0 commit comments

Comments
 (0)