@@ -7,7 +7,10 @@ use std::path::Path;
7
7
8
8
use embedded_hal:: digital:: InputPin ;
9
9
#[ cfg( feature = "async-tokio" ) ]
10
- use gpiocdev:: { line:: EdgeDetection , tokio:: AsyncRequest } ;
10
+ use gpiocdev:: {
11
+ line:: { EdgeDetection , EdgeKind } ,
12
+ tokio:: AsyncRequest ,
13
+ } ;
11
14
use gpiocdev:: {
12
15
line:: { Offset , Value } ,
13
16
request:: { Config , Request } ,
@@ -88,24 +91,18 @@ impl CdevPin {
88
91
self . request ( ) . config ( )
89
92
}
90
93
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
-
100
94
/// Set this pin to input mode
101
95
pub fn into_input_pin ( self ) -> Result < CdevPin , CdevPinError > {
102
- let line_config = self . line_config ( ) ;
96
+ let config = self . config ( ) ;
103
97
104
- if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
105
- return Ok ( self ) ;
98
+ {
99
+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
100
+ if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
101
+ return Ok ( self ) ;
102
+ }
106
103
}
107
104
108
- let mut new_config = self . config ( ) ;
105
+ let mut new_config = config;
109
106
new_config. as_input ( ) ;
110
107
self . request ( ) . reconfigure ( & new_config) ?;
111
108
@@ -117,30 +114,21 @@ impl CdevPin {
117
114
self ,
118
115
state : embedded_hal:: digital:: PinState ,
119
116
) -> Result < CdevPin , CdevPinError > {
120
- let line_config = self . line_config ( ) ;
117
+ let config = self . config ( ) ;
121
118
122
- if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
123
- return Ok ( self ) ;
119
+ {
120
+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
121
+ if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
122
+ return Ok ( self ) ;
123
+ }
124
124
}
125
125
126
- let mut new_config = self . config ( ) ;
126
+ let mut new_config = config;
127
127
new_config. as_output ( state_to_value ( state, line_config. active_low ) ) ;
128
128
self . request ( ) . reconfigure ( & new_config) ?;
129
129
130
130
Ok ( self )
131
131
}
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
- }
144
132
}
145
133
146
134
/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
@@ -197,7 +185,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
197
185
impl embedded_hal:: digital:: OutputPin for CdevPin {
198
186
fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
199
187
let line = self . line ;
200
- let is_active_low = self . is_active_low ( ) ;
188
+ let is_active_low = self . config ( ) . line_config ( line ) . unwrap ( ) . active_low ;
201
189
self . request ( )
202
190
. set_value (
203
191
line,
@@ -209,7 +197,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
209
197
210
198
fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
211
199
let line = self . line ;
212
- let is_active_low = self . is_active_low ( ) ;
200
+ let is_active_low = self . config ( ) . line_config ( line ) . unwrap ( ) . active_low ;
213
201
self . request ( )
214
202
. set_value (
215
203
line,
@@ -223,11 +211,10 @@ impl embedded_hal::digital::OutputPin for CdevPin {
223
211
impl InputPin for CdevPin {
224
212
fn is_high ( & mut self ) -> Result < bool , Self :: Error > {
225
213
let line = self . line ;
214
+ let is_active_low = self . config ( ) . line_config ( line) . unwrap ( ) . active_low ;
226
215
self . request ( )
227
216
. value ( line)
228
- . map ( |val| {
229
- val == state_to_value ( embedded_hal:: digital:: PinState :: High , self . is_active_low ( ) )
230
- } )
217
+ . map ( |val| val == state_to_value ( embedded_hal:: digital:: PinState :: High , is_active_low) )
231
218
. map_err ( CdevPinError :: from)
232
219
}
233
220
@@ -255,14 +242,55 @@ impl embedded_hal_async::digital::Wait for CdevPin {
255
242
}
256
243
257
244
async fn wait_for_rising_edge ( & mut self ) -> Result < ( ) , Self :: Error > {
258
- self . wait_for_edge ( EdgeDetection :: RisingEdge ) . await
245
+ let config = self . config ( ) ;
246
+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
247
+ if !matches ! (
248
+ line_config. edge_detection,
249
+ Some ( EdgeDetection :: RisingEdge | EdgeDetection :: BothEdges )
250
+ ) {
251
+ let mut new_config = config;
252
+ new_config. with_edge_detection ( EdgeDetection :: RisingEdge ) ;
253
+ self . request ( ) . reconfigure ( & new_config) ?;
254
+ }
255
+
256
+ loop {
257
+ let event = self . req . read_edge_event ( ) . await ?;
258
+ if event. kind == EdgeKind :: Rising {
259
+ return Ok ( ( ) ) ;
260
+ }
261
+ }
259
262
}
260
263
261
264
async fn wait_for_falling_edge ( & mut self ) -> Result < ( ) , Self :: Error > {
262
- self . wait_for_edge ( EdgeDetection :: FallingEdge ) . await
265
+ let config = self . config ( ) ;
266
+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
267
+ if !matches ! (
268
+ line_config. edge_detection,
269
+ Some ( EdgeDetection :: FallingEdge | EdgeDetection :: BothEdges )
270
+ ) {
271
+ let mut new_config = config;
272
+ new_config. with_edge_detection ( EdgeDetection :: FallingEdge ) ;
273
+ self . request ( ) . reconfigure ( & new_config) ?;
274
+ }
275
+
276
+ loop {
277
+ let event = self . req . read_edge_event ( ) . await ?;
278
+ if event. kind == EdgeKind :: Falling {
279
+ return Ok ( ( ) ) ;
280
+ }
281
+ }
263
282
}
264
283
265
284
async fn wait_for_any_edge ( & mut self ) -> Result < ( ) , Self :: Error > {
266
- self . wait_for_edge ( EdgeDetection :: BothEdges ) . await
285
+ let config = self . config ( ) ;
286
+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
287
+ if line_config. edge_detection != Some ( EdgeDetection :: BothEdges ) {
288
+ let mut new_config = config;
289
+ new_config. with_edge_detection ( EdgeDetection :: BothEdges ) ;
290
+ self . request ( ) . reconfigure ( & new_config) ?;
291
+ }
292
+
293
+ self . req . read_edge_event ( ) . await ?;
294
+ Ok ( ( ) )
267
295
}
268
296
}
0 commit comments