gpiocdev_embedded_hal/async/
tokio.rs1use std::path::Path;
6
7use crate::{state_to_value, Error};
8use embedded_hal::digital::PinState;
9use gpiocdev::line::{Config, EdgeDetection, EdgeKind, Offset, Value};
10use gpiocdev::tokio::AsyncRequest;
11use gpiocdev::Request;
12
13pub struct InputPin {
23 req: AsyncRequest,
24 offset: Offset,
25 config: Config,
26}
27
28impl InputPin {
29 pub fn new<P>(chip: P, offset: u32) -> Result<Self, Error>
46 where
47 P: AsRef<Path>,
48 {
49 Ok(crate::InputPin::new(chip, offset)?.into())
50 }
51
52 #[inline]
53 fn is_high(&mut self) -> Result<bool, Error> {
54 Ok(self.req.as_ref().value(self.offset)?
55 == state_to_value(PinState::High, self.config.active_low))
56 }
57
58 #[inline]
59 fn is_low(&mut self) -> Result<bool, Error> {
60 Ok(!self.is_high()?)
61 }
62
63 pub fn into_output_pin(self, state: PinState) -> Result<crate::OutputPin, Error> {
65 let pin: crate::InputPin = self.into();
66 pin.into_output_pin(state)
67 }
68
69 fn last_value(&mut self) -> Result<Value, Error> {
71 if self.config.value.is_none() {
72 self.config.value = Some(self.req.as_ref().value(self.offset)?);
73 }
74 Ok(self.config.value.unwrap())
75 }
76
77 async fn wait_for_edge(&mut self, edge: EdgeDetection) -> Result<(), Error> {
78 self.enable_edge_detection(edge)?;
79 loop {
80 let event = self.req.read_edge_event().await?;
81
82 self.config.value.replace(match event.kind {
83 EdgeKind::Rising => Value::Active,
84 EdgeKind::Falling => Value::Inactive,
85 });
86
87 if match edge {
88 EdgeDetection::BothEdges => true,
89 EdgeDetection::RisingEdge => event.kind == EdgeKind::Rising,
90 EdgeDetection::FallingEdge => event.kind == EdgeKind::Falling,
91 } {
92 return Ok(());
93 }
94 }
95 }
96
97 async fn wait_for_level(&mut self, value: Value) -> Result<(), Error> {
98 let edge = match value {
99 Value::Active => EdgeDetection::RisingEdge,
100 Value::Inactive => EdgeDetection::FallingEdge,
101 };
102 self.enable_edge_detection(edge)?;
103 if self.last_value()? == value {
104 return Ok(());
105 }
106 loop {
107 let event = self.req.read_edge_event().await?;
108
109 let nv = match event.kind {
110 EdgeKind::Rising => Value::Active,
111 EdgeKind::Falling => Value::Inactive,
112 };
113 self.config.value = Some(nv);
114 if value == nv {
115 return Ok(());
116 }
117 }
118 }
119
120 fn enable_edge_detection(&mut self, edge: EdgeDetection) -> Result<(), Error> {
122 let new_detection = match self.config.edge_detection {
123 Some(EdgeDetection::BothEdges) => return Ok(()),
124 Some(x) => {
125 if x == edge {
126 return Ok(());
127 };
128 EdgeDetection::BothEdges
129 }
130 None => edge,
131 };
132 let req = self.req.as_ref();
133 req.reconfigure(req.config().with_edge_detection(new_detection))?;
134 self.config.edge_detection = Some(new_detection);
135 self.config.value = None;
137 Ok(())
138 }
139}
140
141impl From<InputPin> for Request {
142 fn from(pin: InputPin) -> Self {
144 pin.req.into()
145 }
146}
147
148impl From<crate::InputPin> for InputPin {
149 fn from(pin: crate::InputPin) -> Self {
151 InputPin {
152 req: pin.0.req.into(),
153 offset: pin.0.offset,
154 config: pin.0.config,
155 }
156 }
157}
158
159impl From<InputPin> for crate::InputPin {
160 fn from(pin: InputPin) -> Self {
162 crate::InputPin(crate::Pin {
163 req: pin.req.into(),
164 offset: pin.offset,
165 config: pin.config,
166 })
167 }
168}
169
170impl embedded_hal::digital::ErrorType for InputPin {
171 type Error = Error;
173}
174
175impl embedded_hal::digital::InputPin for InputPin {
176 #[inline]
177 fn is_high(&mut self) -> Result<bool, Self::Error> {
178 self.is_high()
179 }
180
181 #[inline]
182 fn is_low(&mut self) -> Result<bool, Self::Error> {
183 self.is_low()
184 }
185}
186
187impl embedded_hal_async::digital::Wait for InputPin {
188 #[inline]
196 async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
197 self.wait_for_level(Value::Active).await
198 }
199
200 #[inline]
208 async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
209 self.wait_for_level(Value::Inactive).await
210 }
211
212 #[inline]
213 async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
214 self.wait_for_edge(EdgeDetection::RisingEdge).await
215 }
216
217 #[inline]
218 async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
219 self.wait_for_edge(EdgeDetection::FallingEdge).await
220 }
221
222 #[inline]
223 async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
224 self.wait_for_edge(EdgeDetection::BothEdges).await
225 }
226}