33//! [`embedded-hal`]: https://docs.rs/embedded-hal
44
55use std:: fmt;
6+ use std:: path:: Path ;
67
78use 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 ) ]
1918pub struct CdevPin {
2019 req : Option < Request > ,
@@ -23,12 +22,41 @@ pub struct CdevPin {
2322}
2423
2524impl 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 ( ) ?,
0 commit comments