1use core::convert::Infallible;
2
3use super::{
4 dynamic::PinModeError, DynamicPin, ErasedPin, Floating, Input, OpenDrain, Output,
5 PartiallyErasedPin, Pin, PinState, PullDown, PullUp, PushPull,
6};
7
8use embedded_hal::digital::v2::{
9 InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin,
10};
11
12impl<const P: char, const N: u8, MODE> OutputPin for Pin<P, N, Output<MODE>> {
15 type Error = Infallible;
16
17 #[inline(always)]
18 fn set_high(&mut self) -> Result<(), Self::Error> {
19 self.set_high();
20 Ok(())
21 }
22
23 #[inline(always)]
24 fn set_low(&mut self) -> Result<(), Self::Error> {
25 self.set_low();
26 Ok(())
27 }
28}
29
30impl<const P: char, const N: u8, MODE> StatefulOutputPin for Pin<P, N, Output<MODE>> {
31 #[inline(always)]
32 fn is_set_high(&self) -> Result<bool, Self::Error> {
33 Ok(self.is_set_high())
34 }
35
36 #[inline(always)]
37 fn is_set_low(&self) -> Result<bool, Self::Error> {
38 Ok(self.is_set_low())
39 }
40}
41
42impl<const P: char, const N: u8, MODE> ToggleableOutputPin for Pin<P, N, Output<MODE>> {
43 type Error = Infallible;
44
45 #[inline(always)]
46 fn toggle(&mut self) -> Result<(), Self::Error> {
47 self.toggle();
48 Ok(())
49 }
50}
51
52impl<const P: char, const N: u8> InputPin for Pin<P, N, Output<OpenDrain>> {
53 type Error = Infallible;
54
55 #[inline(always)]
56 fn is_high(&self) -> Result<bool, Self::Error> {
57 Ok(self.is_high())
58 }
59
60 #[inline(always)]
61 fn is_low(&self) -> Result<bool, Self::Error> {
62 Ok(self.is_low())
63 }
64}
65
66impl<const P: char, const N: u8, MODE> InputPin for Pin<P, N, Input<MODE>> {
67 type Error = Infallible;
68
69 #[inline(always)]
70 fn is_high(&self) -> Result<bool, Self::Error> {
71 Ok(self.is_high())
72 }
73
74 #[inline(always)]
75 fn is_low(&self) -> Result<bool, Self::Error> {
76 Ok(self.is_low())
77 }
78}
79
80impl<const P: char, const N: u8> IoPin<Self, Self> for Pin<P, N, Output<OpenDrain>> {
81 type Error = Infallible;
82 fn into_input_pin(self) -> Result<Self, Self::Error> {
83 Ok(self)
84 }
85 fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
86 self.set_state(state);
87 Ok(self)
88 }
89}
90
91impl<const P: char, const N: u8> IoPin<Pin<P, N, Input<Floating>>, Self>
92 for Pin<P, N, Output<OpenDrain>>
93{
94 type Error = Infallible;
95 fn into_input_pin(self) -> Result<Pin<P, N, Input<Floating>>, Self::Error> {
96 Ok(self.into_floating_input())
97 }
98 fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
99 self.set_state(state);
100 Ok(self)
101 }
102}
103
104impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<OpenDrain>>>
105 for Pin<P, N, Input<Floating>>
106{
107 type Error = Infallible;
108 fn into_input_pin(self) -> Result<Self, Self::Error> {
109 Ok(self)
110 }
111 fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<OpenDrain>>, Self::Error> {
112 Ok(self.into_open_drain_output_in_state(state))
113 }
114}
115
116impl<const P: char, const N: u8> IoPin<Pin<P, N, Input<Floating>>, Self>
117 for Pin<P, N, Output<PushPull>>
118{
119 type Error = Infallible;
120 fn into_input_pin(self) -> Result<Pin<P, N, Input<Floating>>, Self::Error> {
121 Ok(self.into_floating_input())
122 }
123 fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
124 self.set_state(state);
125 Ok(self)
126 }
127}
128
129impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<PushPull>>>
130 for Pin<P, N, Input<Floating>>
131{
132 type Error = Infallible;
133 fn into_input_pin(self) -> Result<Self, Self::Error> {
134 Ok(self)
135 }
136 fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<PushPull>>, Self::Error> {
137 Ok(self.into_push_pull_output_in_state(state))
138 }
139}
140
141impl<const P: char, const N: u8> IoPin<Pin<P, N, Input<PullUp>>, Self>
142 for Pin<P, N, Output<PushPull>>
143{
144 type Error = Infallible;
145 fn into_input_pin(self) -> Result<Pin<P, N, Input<PullUp>>, Self::Error> {
146 Ok(self.into_pull_up_input())
147 }
148 fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
149 self.set_state(state);
150 Ok(self)
151 }
152}
153
154impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<PushPull>>>
155 for Pin<P, N, Input<PullUp>>
156{
157 type Error = Infallible;
158 fn into_input_pin(self) -> Result<Self, Self::Error> {
159 Ok(self)
160 }
161 fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<PushPull>>, Self::Error> {
162 Ok(self.into_push_pull_output_in_state(state))
163 }
164}
165
166impl<const P: char, const N: u8> IoPin<Pin<P, N, Input<PullDown>>, Self>
167 for Pin<P, N, Output<PushPull>>
168{
169 type Error = Infallible;
170 fn into_input_pin(self) -> Result<Pin<P, N, Input<PullDown>>, Self::Error> {
171 Ok(self.into_pull_down_input())
172 }
173 fn into_output_pin(mut self, state: PinState) -> Result<Self, Self::Error> {
174 self.set_state(state);
175 Ok(self)
176 }
177}
178
179impl<const P: char, const N: u8> IoPin<Self, Pin<P, N, Output<PushPull>>>
180 for Pin<P, N, Input<PullDown>>
181{
182 type Error = Infallible;
183 fn into_input_pin(self) -> Result<Self, Self::Error> {
184 Ok(self)
185 }
186 fn into_output_pin(self, state: PinState) -> Result<Pin<P, N, Output<PushPull>>, Self::Error> {
187 Ok(self.into_push_pull_output_in_state(state))
188 }
189}
190
191impl<MODE> OutputPin for ErasedPin<Output<MODE>> {
194 type Error = core::convert::Infallible;
195
196 #[inline(always)]
197 fn set_high(&mut self) -> Result<(), Self::Error> {
198 self.set_high();
199 Ok(())
200 }
201
202 #[inline(always)]
203 fn set_low(&mut self) -> Result<(), Self::Error> {
204 self.set_low();
205 Ok(())
206 }
207}
208
209impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> {
210 #[inline(always)]
211 fn is_set_high(&self) -> Result<bool, Self::Error> {
212 Ok(self.is_set_high())
213 }
214
215 #[inline(always)]
216 fn is_set_low(&self) -> Result<bool, Self::Error> {
217 Ok(self.is_set_low())
218 }
219}
220
221impl<MODE> ToggleableOutputPin for ErasedPin<Output<MODE>> {
222 type Error = Infallible;
223
224 #[inline(always)]
225 fn toggle(&mut self) -> Result<(), Self::Error> {
226 self.toggle();
227 Ok(())
228 }
229}
230
231impl InputPin for ErasedPin<Output<OpenDrain>> {
232 type Error = core::convert::Infallible;
233
234 #[inline(always)]
235 fn is_high(&self) -> Result<bool, Self::Error> {
236 Ok(self.is_high())
237 }
238
239 #[inline(always)]
240 fn is_low(&self) -> Result<bool, Self::Error> {
241 Ok(self.is_low())
242 }
243}
244
245impl<MODE> InputPin for ErasedPin<Input<MODE>> {
246 type Error = core::convert::Infallible;
247
248 #[inline(always)]
249 fn is_high(&self) -> Result<bool, Self::Error> {
250 Ok(self.is_high())
251 }
252
253 #[inline(always)]
254 fn is_low(&self) -> Result<bool, Self::Error> {
255 Ok(self.is_low())
256 }
257}
258
259impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> {
262 type Error = Infallible;
263
264 #[inline(always)]
265 fn set_high(&mut self) -> Result<(), Self::Error> {
266 self.set_high();
267 Ok(())
268 }
269
270 #[inline(always)]
271 fn set_low(&mut self) -> Result<(), Self::Error> {
272 self.set_low();
273 Ok(())
274 }
275}
276
277impl<const P: char, MODE> StatefulOutputPin for PartiallyErasedPin<P, Output<MODE>> {
278 #[inline(always)]
279 fn is_set_high(&self) -> Result<bool, Self::Error> {
280 Ok(self.is_set_high())
281 }
282
283 #[inline(always)]
284 fn is_set_low(&self) -> Result<bool, Self::Error> {
285 Ok(self.is_set_low())
286 }
287}
288
289impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<MODE>> {
290 type Error = Infallible;
291
292 #[inline(always)]
293 fn toggle(&mut self) -> Result<(), Self::Error> {
294 self.toggle();
295 Ok(())
296 }
297}
298
299impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> {
300 type Error = Infallible;
301
302 #[inline(always)]
303 fn is_high(&self) -> Result<bool, Self::Error> {
304 Ok(self.is_high())
305 }
306
307 #[inline(always)]
308 fn is_low(&self) -> Result<bool, Self::Error> {
309 Ok(self.is_low())
310 }
311}
312
313impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, Input<MODE>> {
314 type Error = Infallible;
315
316 #[inline(always)]
317 fn is_high(&self) -> Result<bool, Self::Error> {
318 Ok(self.is_high())
319 }
320
321 #[inline(always)]
322 fn is_low(&self) -> Result<bool, Self::Error> {
323 Ok(self.is_low())
324 }
325}
326
327impl<const P: char, const N: u8> OutputPin for DynamicPin<P, N> {
330 type Error = PinModeError;
331 fn set_high(&mut self) -> Result<(), Self::Error> {
332 self.set_high()
333 }
334 fn set_low(&mut self) -> Result<(), Self::Error> {
335 self.set_low()
336 }
337}
338
339impl<const P: char, const N: u8> InputPin for DynamicPin<P, N> {
340 type Error = PinModeError;
341 fn is_high(&self) -> Result<bool, Self::Error> {
342 self.is_high()
343 }
344 fn is_low(&self) -> Result<bool, Self::Error> {
345 self.is_low()
346 }
347}