Skip to content

Commit 085e253

Browse files
committed
Add conversion between input/output pins.
1 parent 6f39de6 commit 085e253

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/cdev_pin.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,26 @@ impl CdevPin<Input> {
7373
mode: PhantomData,
7474
})
7575
}
76+
77+
/// Converts this input pin into an output pin with the given `initial_state`.
78+
pub fn into_output<P>(self, initial_state: PinState) -> Result<CdevPin<Output>, CdevPinError> {
79+
let new_value = self.state_to_value(initial_state);
80+
81+
let req = self.req;
82+
let mut new_config = req.as_ref().config();
83+
new_config.as_output(new_value);
84+
req.as_ref().reconfigure(&new_config)?;
85+
86+
let line = self.line;
87+
let line_config = new_config.line_config(line).unwrap().clone();
88+
89+
Ok(CdevPin {
90+
req,
91+
line,
92+
line_config,
93+
mode: PhantomData,
94+
})
95+
}
7696
}
7797

7898
impl CdevPin<Output> {
@@ -118,6 +138,24 @@ impl CdevPin<Output> {
118138
mode: PhantomData,
119139
})
120140
}
141+
142+
/// Converts this output pin into an input pin.
143+
pub fn into_input<P>(self) -> Result<CdevPin<Output>, CdevPinError> {
144+
let req = self.req;
145+
let mut new_config = req.as_ref().config();
146+
new_config.as_input();
147+
req.as_ref().reconfigure(&new_config)?;
148+
149+
let line = self.line;
150+
let line_config = new_config.line_config(line).unwrap().clone();
151+
152+
Ok(CdevPin {
153+
req,
154+
line,
155+
line_config,
156+
mode: PhantomData,
157+
})
158+
}
121159
}
122160

123161
impl<MODE> CdevPin<MODE> {
@@ -242,10 +280,11 @@ impl embedded_hal_async::digital::Wait for CdevPin<Input> {
242280
self.line_config.edge_detection,
243281
Some(EdgeDetection::RisingEdge | EdgeDetection::BothEdges)
244282
) {
245-
let mut new_config = self.req.as_ref().config();
283+
let req = self.req.as_ref();
284+
let mut new_config = req.config();
246285
new_config.with_edge_detection(EdgeDetection::RisingEdge);
247-
self.req.as_ref().reconfigure(&new_config)?;
248-
self.line_config.edge_detection = Some(EdgeDetection::RisingEdge);
286+
req.reconfigure(&new_config)?;
287+
self.line_config = Some(EdgeDetection::RisingEdge);
249288
}
250289

251290
loop {

0 commit comments

Comments
 (0)