Skip to content

Commit

Permalink
Add conversion between input/output pins.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Feb 16, 2024
1 parent 6f39de6 commit 2021ba5
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/cdev_pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ impl CdevPin<Input> {
mode: PhantomData,
})
}

/// Converts this input pin into an output pin with the given `initial_state`.
pub fn into_output<P>(self, initial_state: PinState) -> Result<CdevPin<Output>, CdevPinError> {
let new_value = self.state_to_value(initial_state);

let req = self.req;
let mut new_config = req.as_ref().config();
new_config.as_output(new_value);
req.as_ref().reconfigure(&new_config)?;

let line = self.line;
let line_config = new_config.line_config(line).unwrap().clone();

Ok(CdevPin {
req,
line,
line_config,
mode: PhantomData,
})
}
}

impl CdevPin<Output> {
Expand Down Expand Up @@ -118,6 +138,24 @@ impl CdevPin<Output> {
mode: PhantomData,
})
}

/// Converts this output pin into an input pin.
pub fn into_input<P>(self) -> Result<CdevPin<Output>, CdevPinError> {
let req = self.req;
let mut new_config = req.as_ref().config();
new_config.as_input();
req.as_ref().reconfigure(&new_config)?;

let line = self.line;
let line_config = new_config.line_config(line).unwrap().clone();

Ok(CdevPin {
req,
line,
line_config,
mode: PhantomData,
})
}
}

impl<MODE> CdevPin<MODE> {
Expand Down Expand Up @@ -242,9 +280,10 @@ impl embedded_hal_async::digital::Wait for CdevPin<Input> {
self.line_config.edge_detection,
Some(EdgeDetection::RisingEdge | EdgeDetection::BothEdges)
) {
let mut new_config = self.req.as_ref().config();
let req = self.req.as_ref();
let mut new_config = req.config();
new_config.with_edge_detection(EdgeDetection::RisingEdge);
self.req.as_ref().reconfigure(&new_config)?;
req.reconfigure(&new_config)?;
self.line_config.edge_detection = Some(EdgeDetection::RisingEdge);
}

Expand Down

0 comments on commit 2021ba5

Please sign in to comment.