@@ -35,6 +35,47 @@ impl CdevPin {
3535 }
3636 return flags;
3737 }
38+
39+ /// Set this pin to input mode
40+ pub fn into_input_pin ( self ) -> Result < CdevPin , gpio_cdev:: errors:: Error > {
41+ if self . 1 . direction ( ) == gpio_cdev:: LineDirection :: In {
42+ return Ok ( self ) ;
43+ }
44+ let line = self . 0 . line ( ) . clone ( ) ;
45+ let input_flags = self . get_input_flags ( ) ;
46+ let consumer = self . 1 . consumer ( ) . unwrap_or ( "" ) . to_owned ( ) ;
47+
48+ // Drop self to free the line before re-requesting it in a new mode.
49+ std:: mem:: drop ( self ) ;
50+
51+ CdevPin :: new ( line. request ( input_flags, 0 , & consumer) ?)
52+ }
53+
54+ /// Set this pin to output mode
55+ pub fn into_output_pin (
56+ self ,
57+ state : embedded_hal:: digital:: PinState ,
58+ ) -> Result < CdevPin , gpio_cdev:: errors:: Error > {
59+ if self . 1 . direction ( ) == gpio_cdev:: LineDirection :: Out {
60+ return Ok ( self ) ;
61+ }
62+
63+ let line = self . 0 . line ( ) . clone ( ) ;
64+ let output_flags = self . get_output_flags ( ) ;
65+ let consumer = self . 1 . consumer ( ) . unwrap_or ( "" ) . to_owned ( ) ;
66+
67+ // Drop self to free the line before re-requesting it in a new mode.
68+ std:: mem:: drop ( self ) ;
69+
70+ CdevPin :: new ( line. request (
71+ output_flags,
72+ state_to_value (
73+ state,
74+ output_flags. intersects ( gpio_cdev:: LineRequestFlags :: ACTIVE_LOW ) ,
75+ ) ,
76+ & consumer,
77+ ) ?)
78+ }
3879}
3980
4081/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
@@ -57,7 +98,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
5798 type Error = gpio_cdev:: errors:: Error ;
5899}
59100
60- impl embedded_hal:: digital:: blocking :: OutputPin for CdevPin {
101+ impl embedded_hal:: digital:: OutputPin for CdevPin {
61102 fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
62103 self . 0 . set_value ( state_to_value (
63104 embedded_hal:: digital:: PinState :: Low ,
@@ -73,7 +114,7 @@ impl embedded_hal::digital::blocking::OutputPin for CdevPin {
73114 }
74115}
75116
76- impl embedded_hal:: digital:: blocking :: InputPin for CdevPin {
117+ impl embedded_hal:: digital:: InputPin for CdevPin {
77118 fn is_high ( & self ) -> Result < bool , Self :: Error > {
78119 self . 0 . get_value ( ) . map ( |val| {
79120 val == state_to_value (
@@ -88,49 +129,6 @@ impl embedded_hal::digital::blocking::InputPin for CdevPin {
88129 }
89130}
90131
91- impl embedded_hal:: digital:: blocking:: IoPin < CdevPin , CdevPin > for CdevPin {
92- type Error = gpio_cdev:: errors:: Error ;
93-
94- fn into_input_pin ( self ) -> Result < CdevPin , Self :: Error > {
95- if self . 1 . direction ( ) == gpio_cdev:: LineDirection :: In {
96- return Ok ( self ) ;
97- }
98- let line = self . 0 . line ( ) . clone ( ) ;
99- let input_flags = self . get_input_flags ( ) ;
100- let consumer = self . 1 . consumer ( ) . unwrap_or ( "" ) . to_owned ( ) ;
101-
102- // Drop self to free the line before re-requesting it in a new mode.
103- std:: mem:: drop ( self ) ;
104-
105- CdevPin :: new ( line. request ( input_flags, 0 , & consumer) ?)
106- }
107-
108- fn into_output_pin (
109- self ,
110- state : embedded_hal:: digital:: PinState ,
111- ) -> Result < CdevPin , Self :: Error > {
112- if self . 1 . direction ( ) == gpio_cdev:: LineDirection :: Out {
113- return Ok ( self ) ;
114- }
115-
116- let line = self . 0 . line ( ) . clone ( ) ;
117- let output_flags = self . get_output_flags ( ) ;
118- let consumer = self . 1 . consumer ( ) . unwrap_or ( "" ) . to_owned ( ) ;
119-
120- // Drop self to free the line before re-requesting it in a new mode.
121- std:: mem:: drop ( self ) ;
122-
123- CdevPin :: new ( line. request (
124- output_flags,
125- state_to_value (
126- state,
127- output_flags. intersects ( gpio_cdev:: LineRequestFlags :: ACTIVE_LOW ) ,
128- ) ,
129- & consumer,
130- ) ?)
131- }
132- }
133-
134132impl core:: ops:: Deref for CdevPin {
135133 type Target = gpio_cdev:: LineHandle ;
136134
0 commit comments