|
| 1 | +use core::convert::Infallible; |
| 2 | + |
| 3 | +use super::{Dynamic, ErasedPin, Input, OpenDrain, Output, PartiallyErasedPin, Pin, PinModeError}; |
| 4 | + |
| 5 | +pub use embedded_hal_one::digital::PinState; |
| 6 | +use embedded_hal_one::digital::{ |
| 7 | + blocking::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin}, |
| 8 | + ErrorType, |
| 9 | +}; |
| 10 | + |
| 11 | +fn into_state(state: PinState) -> super::PinState { |
| 12 | + match state { |
| 13 | + PinState::Low => super::PinState::Low, |
| 14 | + PinState::High => super::PinState::High, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Implementations for `Pin` |
| 19 | +impl<const P: char, const N: u8, const H: bool, MODE> ErrorType for Pin<P, N, H, Output<MODE>> { |
| 20 | + type Error = Infallible; |
| 21 | +} |
| 22 | +impl<const P: char, const N: u8, const H: bool, MODE> ErrorType for Pin<P, N, H, Input<MODE>> { |
| 23 | + type Error = Infallible; |
| 24 | +} |
| 25 | + |
| 26 | +impl<const P: char, const N: u8, const H: bool> ErrorType for Pin<P, N, H, Dynamic> { |
| 27 | + type Error = PinModeError; |
| 28 | +} |
| 29 | + |
| 30 | +impl<const P: char, const N: u8, const H: bool> OutputPin for Pin<P, N, H, Dynamic> { |
| 31 | + fn set_high(&mut self) -> Result<(), Self::Error> { |
| 32 | + if self.mode.is_output() { |
| 33 | + self._set_state(into_state(PinState::High)); |
| 34 | + Ok(()) |
| 35 | + } else { |
| 36 | + Err(PinModeError::IncorrectMode) |
| 37 | + } |
| 38 | + } |
| 39 | + fn set_low(&mut self) -> Result<(), Self::Error> { |
| 40 | + if self.mode.is_output() { |
| 41 | + self._set_state(into_state(PinState::Low)); |
| 42 | + Ok(()) |
| 43 | + } else { |
| 44 | + Err(PinModeError::IncorrectMode) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<const P: char, const N: u8, const H: bool> InputPin for Pin<P, N, H, Dynamic> { |
| 50 | + fn is_high(&self) -> Result<bool, Self::Error> { |
| 51 | + self.is_low().map(|b| !b) |
| 52 | + } |
| 53 | + fn is_low(&self) -> Result<bool, Self::Error> { |
| 54 | + if self.mode.is_input() { |
| 55 | + Ok(self._is_low()) |
| 56 | + } else { |
| 57 | + Err(PinModeError::IncorrectMode) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<const P: char, const N: u8, const H: bool, MODE> OutputPin for Pin<P, N, H, Output<MODE>> { |
| 63 | + #[inline] |
| 64 | + fn set_high(&mut self) -> Result<(), Self::Error> { |
| 65 | + self.set_high(); |
| 66 | + Ok(()) |
| 67 | + } |
| 68 | + #[inline] |
| 69 | + fn set_low(&mut self) -> Result<(), Self::Error> { |
| 70 | + self.set_low(); |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl<const P: char, const N: u8, const H: bool, MODE> StatefulOutputPin |
| 76 | + for Pin<P, N, H, Output<MODE>> |
| 77 | +{ |
| 78 | + #[inline] |
| 79 | + fn is_set_high(&self) -> Result<bool, Self::Error> { |
| 80 | + Ok(self.is_set_high()) |
| 81 | + } |
| 82 | + #[inline] |
| 83 | + fn is_set_low(&self) -> Result<bool, Self::Error> { |
| 84 | + Ok(self.is_set_low()) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<const P: char, const N: u8, const H: bool, MODE> ToggleableOutputPin |
| 89 | + for Pin<P, N, H, Output<MODE>> |
| 90 | +{ |
| 91 | + #[inline(always)] |
| 92 | + fn toggle(&mut self) -> Result<(), Self::Error> { |
| 93 | + self.toggle(); |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<const P: char, const N: u8, const H: bool, MODE> InputPin for Pin<P, N, H, Input<MODE>> { |
| 99 | + #[inline] |
| 100 | + fn is_high(&self) -> Result<bool, Self::Error> { |
| 101 | + Ok(self.is_high()) |
| 102 | + } |
| 103 | + |
| 104 | + #[inline] |
| 105 | + fn is_low(&self) -> Result<bool, Self::Error> { |
| 106 | + Ok(self.is_low()) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +impl<const P: char, const N: u8, const H: bool> InputPin for Pin<P, N, H, Output<OpenDrain>> { |
| 111 | + #[inline] |
| 112 | + fn is_high(&self) -> Result<bool, Self::Error> { |
| 113 | + Ok(self.is_high()) |
| 114 | + } |
| 115 | + |
| 116 | + #[inline] |
| 117 | + fn is_low(&self) -> Result<bool, Self::Error> { |
| 118 | + Ok(self.is_low()) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// PartiallyErasedPin |
| 123 | + |
| 124 | +impl<const P: char, MODE> ErrorType for PartiallyErasedPin<P, MODE> { |
| 125 | + type Error = Infallible; |
| 126 | +} |
| 127 | + |
| 128 | +impl<const P: char, MODE> OutputPin for PartiallyErasedPin<P, Output<MODE>> { |
| 129 | + #[inline(always)] |
| 130 | + fn set_high(&mut self) -> Result<(), Self::Error> { |
| 131 | + self.set_high(); |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | + |
| 135 | + #[inline(always)] |
| 136 | + fn set_low(&mut self) -> Result<(), Self::Error> { |
| 137 | + self.set_low(); |
| 138 | + Ok(()) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl<const P: char, MODE> StatefulOutputPin for PartiallyErasedPin<P, Output<MODE>> { |
| 143 | + #[inline(always)] |
| 144 | + fn is_set_high(&self) -> Result<bool, Self::Error> { |
| 145 | + Ok(self.is_set_high()) |
| 146 | + } |
| 147 | + |
| 148 | + #[inline(always)] |
| 149 | + fn is_set_low(&self) -> Result<bool, Self::Error> { |
| 150 | + Ok(self.is_set_low()) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl<const P: char, MODE> ToggleableOutputPin for PartiallyErasedPin<P, Output<MODE>> { |
| 155 | + #[inline(always)] |
| 156 | + fn toggle(&mut self) -> Result<(), Self::Error> { |
| 157 | + self.toggle(); |
| 158 | + Ok(()) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<const P: char> InputPin for PartiallyErasedPin<P, Output<OpenDrain>> { |
| 163 | + #[inline(always)] |
| 164 | + fn is_high(&self) -> Result<bool, Self::Error> { |
| 165 | + Ok(self.is_high()) |
| 166 | + } |
| 167 | + |
| 168 | + #[inline(always)] |
| 169 | + fn is_low(&self) -> Result<bool, Self::Error> { |
| 170 | + Ok(self.is_low()) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +impl<const P: char, MODE> InputPin for PartiallyErasedPin<P, Input<MODE>> { |
| 175 | + #[inline(always)] |
| 176 | + fn is_high(&self) -> Result<bool, Self::Error> { |
| 177 | + Ok(self.is_high()) |
| 178 | + } |
| 179 | + |
| 180 | + #[inline(always)] |
| 181 | + fn is_low(&self) -> Result<bool, Self::Error> { |
| 182 | + Ok(self.is_low()) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// ErasedPin |
| 187 | + |
| 188 | +impl<MODE> ErrorType for ErasedPin<MODE> { |
| 189 | + type Error = core::convert::Infallible; |
| 190 | +} |
| 191 | + |
| 192 | +impl<MODE> OutputPin for ErasedPin<Output<MODE>> { |
| 193 | + fn set_high(&mut self) -> Result<(), Infallible> { |
| 194 | + self.set_high(); |
| 195 | + Ok(()) |
| 196 | + } |
| 197 | + |
| 198 | + fn set_low(&mut self) -> Result<(), Infallible> { |
| 199 | + self.set_low(); |
| 200 | + Ok(()) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +impl<MODE> StatefulOutputPin for ErasedPin<Output<MODE>> { |
| 205 | + fn is_set_high(&self) -> Result<bool, Self::Error> { |
| 206 | + Ok(self.is_set_high()) |
| 207 | + } |
| 208 | + |
| 209 | + fn is_set_low(&self) -> Result<bool, Self::Error> { |
| 210 | + Ok(self.is_set_low()) |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +impl<MODE> InputPin for ErasedPin<Input<MODE>> { |
| 215 | + fn is_high(&self) -> Result<bool, Infallible> { |
| 216 | + Ok(self.is_high()) |
| 217 | + } |
| 218 | + |
| 219 | + fn is_low(&self) -> Result<bool, Infallible> { |
| 220 | + Ok(self.is_low()) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +impl InputPin for ErasedPin<Output<OpenDrain>> { |
| 225 | + fn is_high(&self) -> Result<bool, Infallible> { |
| 226 | + Ok(self.is_high()) |
| 227 | + } |
| 228 | + |
| 229 | + fn is_low(&self) -> Result<bool, Infallible> { |
| 230 | + Ok(self.is_low()) |
| 231 | + } |
| 232 | +} |
0 commit comments