From 2021ba5878cea59a34e55596c4bc058407fd2f46 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 16 Feb 2024 07:24:04 +0100 Subject: [PATCH] Add conversion between input/output pins. --- src/cdev_pin.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/cdev_pin.rs b/src/cdev_pin.rs index a74e44b..77a3df8 100644 --- a/src/cdev_pin.rs +++ b/src/cdev_pin.rs @@ -73,6 +73,26 @@ impl CdevPin { mode: PhantomData, }) } + + /// Converts this input pin into an output pin with the given `initial_state`. + pub fn into_output

(self, initial_state: PinState) -> Result, 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 { @@ -118,6 +138,24 @@ impl CdevPin { mode: PhantomData, }) } + + /// Converts this output pin into an input pin. + pub fn into_input

(self) -> Result, 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 CdevPin { @@ -242,9 +280,10 @@ impl embedded_hal_async::digital::Wait for CdevPin { 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); }