@@ -7,7 +7,10 @@ use std::path::Path;
77
88use embedded_hal:: digital:: InputPin ;
99#[ cfg( feature = "async-tokio" ) ]
10- use gpiocdev:: { line:: EdgeDetection , tokio:: AsyncRequest } ;
10+ use gpiocdev:: {
11+ line:: { EdgeDetection , EdgeKind } ,
12+ tokio:: AsyncRequest ,
13+ } ;
1114use gpiocdev:: {
1215 line:: { Offset , Value } ,
1316 request:: { Config , Request } ,
@@ -88,24 +91,16 @@ impl CdevPin {
8891 self . request ( ) . config ( )
8992 }
9093
91- fn is_active_low ( & self ) -> bool {
92- self . line_config ( ) . active_low
93- }
94-
95- fn line_config ( & self ) -> gpiocdev:: line:: Config {
96- // Unwrapping is fine, since `self.line` comes from a `Request` and is guaranteed to exist.
97- self . config ( ) . line_config ( self . line ) . unwrap ( ) . clone ( )
98- }
99-
10094 /// Set this pin to input mode
10195 pub fn into_input_pin ( self ) -> Result < CdevPin , CdevPinError > {
102- let line_config = self . line_config ( ) ;
96+ let config = self . config ( ) ;
97+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
10398
10499 if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
105100 return Ok ( self ) ;
106101 }
107102
108- let mut new_config = self . config ( ) ;
103+ let mut new_config = config;
109104 new_config. as_input ( ) ;
110105 self . request ( ) . reconfigure ( & new_config) ?;
111106
@@ -117,30 +112,19 @@ impl CdevPin {
117112 self ,
118113 state : embedded_hal:: digital:: PinState ,
119114 ) -> Result < CdevPin , CdevPinError > {
120- let line_config = self . line_config ( ) ;
121-
115+ let config = self . config ( ) ;
116+ let line_config = config . line_config ( self . line ) . unwrap ( ) ;
122117 if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
123118 return Ok ( self ) ;
124119 }
120+ let is_active_low = line_config. active_low ;
125121
126- let mut new_config = self . config ( ) ;
127- new_config. as_output ( state_to_value ( state, line_config . active_low ) ) ;
122+ let mut new_config = config;
123+ new_config. as_output ( state_to_value ( state, is_active_low ) ) ;
128124 self . request ( ) . reconfigure ( & new_config) ?;
129125
130126 Ok ( self )
131127 }
132-
133- #[ cfg( feature = "async-tokio" ) ]
134- async fn wait_for_edge ( & mut self , edge : EdgeDetection ) -> Result < ( ) , CdevPinError > {
135- if self . line_config ( ) . edge_detection != Some ( edge) {
136- let mut new_config = self . config ( ) ;
137- new_config. with_edge_detection ( edge) ;
138- self . request ( ) . reconfigure ( & new_config) ?;
139- }
140-
141- self . req . read_edge_event ( ) . await ?;
142- Ok ( ( ) )
143- }
144128}
145129
146130/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
@@ -197,7 +181,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
197181impl embedded_hal:: digital:: OutputPin for CdevPin {
198182 fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
199183 let line = self . line ;
200- let is_active_low = self . is_active_low ( ) ;
184+ let is_active_low = self . config ( ) . line_config ( line ) . unwrap ( ) . active_low ;
201185 self . request ( )
202186 . set_value (
203187 line,
@@ -209,7 +193,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
209193
210194 fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
211195 let line = self . line ;
212- let is_active_low = self . is_active_low ( ) ;
196+ let is_active_low = self . config ( ) . line_config ( line ) . unwrap ( ) . active_low ;
213197 self . request ( )
214198 . set_value (
215199 line,
@@ -223,11 +207,10 @@ impl embedded_hal::digital::OutputPin for CdevPin {
223207impl InputPin for CdevPin {
224208 fn is_high ( & mut self ) -> Result < bool , Self :: Error > {
225209 let line = self . line ;
210+ let is_active_low = self . config ( ) . line_config ( line) . unwrap ( ) . active_low ;
226211 self . request ( )
227212 . value ( line)
228- . map ( |val| {
229- val == state_to_value ( embedded_hal:: digital:: PinState :: High , self . is_active_low ( ) )
230- } )
213+ . map ( |val| val == state_to_value ( embedded_hal:: digital:: PinState :: High , is_active_low) )
231214 . map_err ( CdevPinError :: from)
232215 }
233216
@@ -255,14 +238,55 @@ impl embedded_hal_async::digital::Wait for CdevPin {
255238 }
256239
257240 async fn wait_for_rising_edge ( & mut self ) -> Result < ( ) , Self :: Error > {
258- self . wait_for_edge ( EdgeDetection :: RisingEdge ) . await
241+ let config = self . config ( ) ;
242+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
243+ if !matches ! (
244+ line_config. edge_detection,
245+ Some ( EdgeDetection :: RisingEdge | EdgeDetection :: BothEdges )
246+ ) {
247+ let mut new_config = config;
248+ new_config. with_edge_detection ( EdgeDetection :: RisingEdge ) ;
249+ self . request ( ) . reconfigure ( & new_config) ?;
250+ }
251+
252+ loop {
253+ let event = self . req . read_edge_event ( ) . await ?;
254+ if event. kind == EdgeKind :: Rising {
255+ return Ok ( ( ) ) ;
256+ }
257+ }
259258 }
260259
261260 async fn wait_for_falling_edge ( & mut self ) -> Result < ( ) , Self :: Error > {
262- self . wait_for_edge ( EdgeDetection :: FallingEdge ) . await
261+ let config = self . config ( ) ;
262+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
263+ if !matches ! (
264+ line_config. edge_detection,
265+ Some ( EdgeDetection :: FallingEdge | EdgeDetection :: BothEdges )
266+ ) {
267+ let mut new_config = config;
268+ new_config. with_edge_detection ( EdgeDetection :: FallingEdge ) ;
269+ self . request ( ) . reconfigure ( & new_config) ?;
270+ }
271+
272+ loop {
273+ let event = self . req . read_edge_event ( ) . await ?;
274+ if event. kind == EdgeKind :: Falling {
275+ return Ok ( ( ) ) ;
276+ }
277+ }
263278 }
264279
265280 async fn wait_for_any_edge ( & mut self ) -> Result < ( ) , Self :: Error > {
266- self . wait_for_edge ( EdgeDetection :: BothEdges ) . await
281+ let config = self . config ( ) ;
282+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
283+ if line_config. edge_detection != Some ( EdgeDetection :: BothEdges ) {
284+ let mut new_config = config;
285+ new_config. with_edge_detection ( EdgeDetection :: BothEdges ) ;
286+ self . request ( ) . reconfigure ( & new_config) ?;
287+ }
288+
289+ self . req . read_edge_event ( ) . await ?;
290+ Ok ( ( ) )
267291 }
268292}
0 commit comments