Skip to content

Commit b6f76e3

Browse files
committed
Don't get config multiple times.
1 parent 371b6c2 commit b6f76e3

File tree

1 file changed

+66
-38
lines changed

1 file changed

+66
-38
lines changed

src/cdev_pin.rs

+66-38
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use std::path::Path;
77

88
use 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+
};
1114
use gpiocdev::{
1215
line::{Offset, Value},
1316
request::{Config, Request},
@@ -88,24 +91,18 @@ 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();
10397

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+
}
106103
}
107104

108-
let mut new_config = self.config();
105+
let mut new_config = config;
109106
new_config.as_input();
110107
self.request().reconfigure(&new_config)?;
111108

@@ -117,30 +114,21 @@ impl CdevPin {
117114
self,
118115
state: embedded_hal::digital::PinState,
119116
) -> Result<CdevPin, CdevPinError> {
120-
let line_config = self.line_config();
117+
let config = self.config();
121118

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+
}
124124
}
125125

126-
let mut new_config = self.config();
126+
let mut new_config = config;
127127
new_config.as_output(state_to_value(state, line_config.active_low));
128128
self.request().reconfigure(&new_config)?;
129129

130130
Ok(self)
131131
}
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-
}
144132
}
145133

146134
/// Converts a pin state to the gpio_cdev compatible numeric value, accounting
@@ -197,7 +185,7 @@ impl embedded_hal::digital::ErrorType for CdevPin {
197185
impl embedded_hal::digital::OutputPin for CdevPin {
198186
fn set_low(&mut self) -> Result<(), Self::Error> {
199187
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;
201189
self.request()
202190
.set_value(
203191
line,
@@ -209,7 +197,7 @@ impl embedded_hal::digital::OutputPin for CdevPin {
209197

210198
fn set_high(&mut self) -> Result<(), Self::Error> {
211199
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;
213201
self.request()
214202
.set_value(
215203
line,
@@ -223,11 +211,10 @@ impl embedded_hal::digital::OutputPin for CdevPin {
223211
impl InputPin for CdevPin {
224212
fn is_high(&mut self) -> Result<bool, Self::Error> {
225213
let line = self.line;
214+
let is_active_low = self.config().line_config(line).unwrap().active_low;
226215
self.request()
227216
.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))
231218
.map_err(CdevPinError::from)
232219
}
233220

@@ -255,14 +242,55 @@ impl embedded_hal_async::digital::Wait for CdevPin {
255242
}
256243

257244
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+
}
259262
}
260263

261264
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+
}
263282
}
264283

265284
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(())
267295
}
268296
}

0 commit comments

Comments
 (0)