Skip to content

Commit 9162206

Browse files
committed
Refactor CdevPin::new.
1 parent e2c7ef4 commit 9162206

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

examples/gpio-wait.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::time::Duration;
33

44
use embedded_hal::digital::{InputPin, OutputPin, PinState};
55
use embedded_hal_async::digital::Wait;
6-
use gpiocdev::Request;
76
use linux_embedded_hal::CdevPin;
87
use tokio::time::{sleep, timeout};
98

@@ -14,17 +13,8 @@ const OUTPUT_LINE: u32 = 17;
1413

1514
#[tokio::main]
1615
async fn main() -> Result<(), Box<dyn Error>> {
17-
let input = Request::builder()
18-
.on_chip(CHIP)
19-
.with_line(INPUT_LINE)
20-
.request()?;
21-
let output = Request::builder()
22-
.on_chip(CHIP)
23-
.with_line(OUTPUT_LINE)
24-
.request()?;
25-
26-
let mut input_pin = CdevPin::new(input)?.into_input_pin()?;
27-
let mut output_pin = CdevPin::new(output)?.into_output_pin(PinState::Low)?;
16+
let mut input_pin = CdevPin::new(CHIP, INPUT_LINE)?.into_input_pin()?;
17+
let mut output_pin = CdevPin::new(CHIP, OUTPUT_LINE)?.into_output_pin(PinState::Low)?;
2818

2919
timeout(Duration::from_secs(10), async move {
3020
let set_output = tokio::spawn(async move {

src/cdev_pin.rs

+36-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
55
use std::fmt;
6+
use std::path::Path;
67

78
use embedded_hal::digital::InputPin;
89
#[cfg(feature = "async-tokio")]
@@ -12,9 +13,7 @@ use gpiocdev::{
1213
request::{Config, Request},
1314
};
1415

15-
/// Newtype around [`gpio_cdev::LineHandle`] that implements the `embedded-hal` traits
16-
///
17-
/// [`gpio_cdev::LineHandle`]: https://docs.rs/gpio-cdev/0.5.0/gpio_cdev/struct.LineHandle.html
16+
/// Newtype around [`gpiocdev::request::Request`] that implements the `embedded-hal` traits.
1817
#[derive(Debug)]
1918
pub struct CdevPin {
2019
req: Option<Request>,
@@ -23,12 +22,41 @@ pub struct CdevPin {
2322
}
2423

2524
impl CdevPin {
26-
/// See [`gpiocdev::request::Request`] for details.
25+
/// Creates a new pin for the given `line` on the given `chip`.
26+
///
27+
/// ```
28+
/// use linux_embedded_hal::CdevPin;
29+
/// # use linux_embedded_hal::CdevPinError;
30+
///
31+
/// # fn main() -> Result<(), CdevPinError> {
32+
/// let mut pin = CdevPin::new("/dev/gpiochip0", 4)?.into_output_pin()?;
33+
/// pin.set_high()?;
34+
/// # }
35+
/// ```
36+
pub fn new<P>(chip: P, line: u32) -> Result<Self, CdevPinError>
37+
where
38+
P: AsRef<Path>,
39+
{
40+
let req = Request::builder()
41+
.on_chip(chip.as_ref())
42+
.with_line(line)
43+
.request()?;
44+
45+
let config = req.config();
46+
47+
Ok(Self {
48+
req: Some(req),
49+
config,
50+
line,
51+
})
52+
}
53+
54+
/// Creates a new pin from a [`Request`](gpiocdev::request::Request).
2755
///
2856
/// # Panics
2957
///
30-
/// Panics if the `Request` does not contain exactly one line.
31-
pub fn new(req: Request) -> Result<Self, CdevPinError> {
58+
/// Panics if the [`Request`](gpiocdev::request::Request) does not contain exactly one line.
59+
pub fn from_request(req: Request) -> Result<Self, CdevPinError> {
3260
let config = req.config();
3361
let lines = config.lines();
3462

@@ -79,7 +107,7 @@ impl CdevPin {
79107

80108
drop(self.req.take());
81109

82-
CdevPin::new(Request::from_config(self.config).as_input().request()?)
110+
CdevPin::from_request(Request::from_config(self.config).as_input().request()?)
83111
}
84112

85113
/// Set this pin to output mode
@@ -96,7 +124,7 @@ impl CdevPin {
96124

97125
drop(self.req.take());
98126

99-
CdevPin::new(
127+
CdevPin::from_request(
100128
Request::from_config(self.config)
101129
.as_output(state_to_value(state, is_active_low))
102130
.request()?,

src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ pub use sysfs_gpio;
2727
mod sysfs_pin;
2828

2929
#[cfg(feature = "gpio_cdev")]
30-
/// Cdev Pin wrapper module
3130
mod cdev_pin;
32-
3331
#[cfg(feature = "gpio_cdev")]
34-
/// Cdev pin re-export
3532
pub use cdev_pin::{CdevPin, CdevPinError};
3633

3734
#[cfg(feature = "gpio_sysfs")]

0 commit comments

Comments
 (0)