3
3
//! [`embedded-hal`]: https://docs.rs/embedded-hal
4
4
5
5
use std:: fmt;
6
+ use std:: path:: Path ;
6
7
7
8
use embedded_hal:: digital:: InputPin ;
8
9
#[ cfg( feature = "async-tokio" ) ]
@@ -12,9 +13,7 @@ use gpiocdev::{
12
13
request:: { Config , Request } ,
13
14
} ;
14
15
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.
18
17
#[ derive( Debug ) ]
19
18
pub struct CdevPin {
20
19
req : Option < Request > ,
@@ -23,12 +22,41 @@ pub struct CdevPin {
23
22
}
24
23
25
24
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).
27
55
///
28
56
/// # Panics
29
57
///
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 > {
32
60
let config = req. config ( ) ;
33
61
let lines = config. lines ( ) ;
34
62
@@ -79,7 +107,7 @@ impl CdevPin {
79
107
80
108
drop ( self . req . take ( ) ) ;
81
109
82
- CdevPin :: new ( Request :: from_config ( self . config ) . as_input ( ) . request ( ) ?)
110
+ CdevPin :: from_request ( Request :: from_config ( self . config ) . as_input ( ) . request ( ) ?)
83
111
}
84
112
85
113
/// Set this pin to output mode
@@ -96,7 +124,7 @@ impl CdevPin {
96
124
97
125
drop ( self . req . take ( ) ) ;
98
126
99
- CdevPin :: new (
127
+ CdevPin :: from_request (
100
128
Request :: from_config ( self . config )
101
129
. as_output ( state_to_value ( state, is_active_low) )
102
130
. request ( ) ?,
0 commit comments