@@ -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,16 @@ 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 ( ) ;
97
+ let line_config = config. line_config ( self . line ) . unwrap ( ) ;
103
98
104
99
if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
105
100
return Ok ( self ) ;
106
101
}
107
102
108
- let mut new_config = self . config ( ) ;
103
+ let mut new_config = config;
109
104
new_config. as_input ( ) ;
110
105
self . request ( ) . reconfigure ( & new_config) ?;
111
106
@@ -117,30 +112,19 @@ impl CdevPin {
117
112
self ,
118
113
state : embedded_hal:: digital:: PinState ,
119
114
) -> 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 ( ) ;
122
117
if line_config. direction == Some ( gpiocdev:: line:: Direction :: Output ) {
123
118
return Ok ( self ) ;
124
119
}
120
+ let is_active_low = line_config. active_low ;
125
121
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 ) ) ;
128
124
self . request ( ) . reconfigure ( & new_config) ?;
129
125
130
126
Ok ( self )
131
127
}
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
128
}
145
129
146
130
/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
@@ -197,7 +181,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
197
181
impl embedded_hal:: digital:: OutputPin for CdevPin {
198
182
fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
199
183
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 ;
201
185
self . request ( )
202
186
. set_value (
203
187
line,
@@ -209,7 +193,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
209
193
210
194
fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
211
195
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 ;
213
197
self . request ( )
214
198
. set_value (
215
199
line,
@@ -223,11 +207,10 @@ impl embedded_hal::digital::OutputPin for CdevPin {
223
207
impl InputPin for CdevPin {
224
208
fn is_high ( & mut self ) -> Result < bool , Self :: Error > {
225
209
let line = self . line ;
210
+ let is_active_low = self . config ( ) . line_config ( line) . unwrap ( ) . active_low ;
226
211
self . request ( )
227
212
. 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) )
231
214
. map_err ( CdevPinError :: from)
232
215
}
233
216
@@ -255,14 +238,55 @@ impl embedded_hal_async::digital::Wait for CdevPin {
255
238
}
256
239
257
240
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
+ }
259
258
}
260
259
261
260
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
+ }
263
278
}
264
279
265
280
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 ( ( ) )
267
291
}
268
292
}
0 commit comments