diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-07-12 23:56:45 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-07-12 23:56:45 +0000 |
| commit | 7c8dffeb12d9bc9589583fa6c9fd69d8c927c5bb (patch) | |
| tree | cfd70f1d698277e98422185e3e7c73e596d6176c | |
| parent | e4cacc3bb812c8870584c513020f0747146c860a (diff) | |
| parent | 53e40860c17b2525ad4a8d25d6d0aa7fe4da789b (diff) | |
Merge #863
863: Add more convenience GPIO functions r=Dirbaio a=chemicstry
This reduces boilerplate code from:
```rs
if config.pin_high {
pin.set_high()
} else {
pin.set_low()
}
```
to
```rs
pin.set_level(config.pin_high.into());
```
Co-authored-by: chemicstry <[email protected]>
| -rw-r--r-- | embassy-nrf/src/gpio.rs | 76 | ||||
| -rw-r--r-- | embassy-rp/src/gpio.rs | 75 | ||||
| -rw-r--r-- | embassy-stm32/src/gpio.rs | 74 |
3 files changed, 224 insertions, 1 deletions
diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 0ba20b0b1..fd4ae2ec3 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs | |||
| @@ -38,6 +38,7 @@ pub struct Input<'d, T: Pin> { | |||
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | impl<'d, T: Pin> Input<'d, T> { | 40 | impl<'d, T: Pin> Input<'d, T> { |
| 41 | #[inline] | ||
| 41 | pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { | 42 | pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { |
| 42 | let mut pin = Flex::new(pin); | 43 | let mut pin = Flex::new(pin); |
| 43 | pin.set_as_input(pull); | 44 | pin.set_as_input(pull); |
| @@ -45,13 +46,21 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 45 | Self { pin } | 46 | Self { pin } |
| 46 | } | 47 | } |
| 47 | 48 | ||
| 49 | #[inline] | ||
| 48 | pub fn is_high(&self) -> bool { | 50 | pub fn is_high(&self) -> bool { |
| 49 | self.pin.is_high() | 51 | self.pin.is_high() |
| 50 | } | 52 | } |
| 51 | 53 | ||
| 54 | #[inline] | ||
| 52 | pub fn is_low(&self) -> bool { | 55 | pub fn is_low(&self) -> bool { |
| 53 | self.pin.is_low() | 56 | self.pin.is_low() |
| 54 | } | 57 | } |
| 58 | |||
| 59 | /// Returns current pin level | ||
| 60 | #[inline] | ||
| 61 | pub fn get_level(&self) -> Level { | ||
| 62 | self.pin.get_level() | ||
| 63 | } | ||
| 55 | } | 64 | } |
| 56 | 65 | ||
| 57 | /// Digital input or output level. | 66 | /// Digital input or output level. |
| @@ -62,6 +71,24 @@ pub enum Level { | |||
| 62 | High, | 71 | High, |
| 63 | } | 72 | } |
| 64 | 73 | ||
| 74 | impl From<bool> for Level { | ||
| 75 | fn from(val: bool) -> Self { | ||
| 76 | match val { | ||
| 77 | true => Self::High, | ||
| 78 | false => Self::Low, | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | impl Into<bool> for Level { | ||
| 84 | fn into(self) -> bool { | ||
| 85 | match self { | ||
| 86 | Level::Low => false, | ||
| 87 | Level::High => true, | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 65 | // These numbers match DRIVE_A exactly so hopefully the compiler will unify them. | 92 | // These numbers match DRIVE_A exactly so hopefully the compiler will unify them. |
| 66 | #[derive(Clone, Copy, Debug, PartialEq)] | 93 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 67 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 94 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -91,6 +118,7 @@ pub struct Output<'d, T: Pin> { | |||
| 91 | } | 118 | } |
| 92 | 119 | ||
| 93 | impl<'d, T: Pin> Output<'d, T> { | 120 | impl<'d, T: Pin> Output<'d, T> { |
| 121 | #[inline] | ||
| 94 | pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self { | 122 | pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level, drive: OutputDrive) -> Self { |
| 95 | let mut pin = Flex::new(pin); | 123 | let mut pin = Flex::new(pin); |
| 96 | match initial_output { | 124 | match initial_output { |
| @@ -103,24 +131,40 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 103 | } | 131 | } |
| 104 | 132 | ||
| 105 | /// Set the output as high. | 133 | /// Set the output as high. |
| 134 | #[inline] | ||
| 106 | pub fn set_high(&mut self) { | 135 | pub fn set_high(&mut self) { |
| 107 | self.pin.set_high() | 136 | self.pin.set_high() |
| 108 | } | 137 | } |
| 109 | 138 | ||
| 110 | /// Set the output as low. | 139 | /// Set the output as low. |
| 140 | #[inline] | ||
| 111 | pub fn set_low(&mut self) { | 141 | pub fn set_low(&mut self) { |
| 112 | self.pin.set_low() | 142 | self.pin.set_low() |
| 113 | } | 143 | } |
| 114 | 144 | ||
| 145 | /// Set the output level. | ||
| 146 | #[inline] | ||
| 147 | pub fn set_level(&mut self, level: Level) { | ||
| 148 | self.pin.set_level(level) | ||
| 149 | } | ||
| 150 | |||
| 115 | /// Is the output pin set as high? | 151 | /// Is the output pin set as high? |
| 152 | #[inline] | ||
| 116 | pub fn is_set_high(&self) -> bool { | 153 | pub fn is_set_high(&self) -> bool { |
| 117 | self.pin.is_set_high() | 154 | self.pin.is_set_high() |
| 118 | } | 155 | } |
| 119 | 156 | ||
| 120 | /// Is the output pin set as low? | 157 | /// Is the output pin set as low? |
| 158 | #[inline] | ||
| 121 | pub fn is_set_low(&self) -> bool { | 159 | pub fn is_set_low(&self) -> bool { |
| 122 | self.pin.is_set_low() | 160 | self.pin.is_set_low() |
| 123 | } | 161 | } |
| 162 | |||
| 163 | /// What level output is set to | ||
| 164 | #[inline] | ||
| 165 | pub fn get_output_level(&self) -> Level { | ||
| 166 | self.pin.get_output_level() | ||
| 167 | } | ||
| 124 | } | 168 | } |
| 125 | 169 | ||
| 126 | fn convert_drive(drive: OutputDrive) -> DRIVE_A { | 170 | fn convert_drive(drive: OutputDrive) -> DRIVE_A { |
| @@ -159,6 +203,7 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 159 | /// | 203 | /// |
| 160 | /// The pin remains disconnected. The initial output level is unspecified, but can be changed | 204 | /// The pin remains disconnected. The initial output level is unspecified, but can be changed |
| 161 | /// before the pin is put into output mode. | 205 | /// before the pin is put into output mode. |
| 206 | #[inline] | ||
| 162 | pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self { | 207 | pub fn new(pin: impl Unborrow<Target = T> + 'd) -> Self { |
| 163 | unborrow!(pin); | 208 | unborrow!(pin); |
| 164 | // Pin will be in disconnected state. | 209 | // Pin will be in disconnected state. |
| @@ -169,6 +214,7 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 169 | } | 214 | } |
| 170 | 215 | ||
| 171 | /// Put the pin into input mode. | 216 | /// Put the pin into input mode. |
| 217 | #[inline] | ||
| 172 | pub fn set_as_input(&mut self, pull: Pull) { | 218 | pub fn set_as_input(&mut self, pull: Pull) { |
| 173 | self.pin.conf().write(|w| { | 219 | self.pin.conf().write(|w| { |
| 174 | w.dir().input(); | 220 | w.dir().input(); |
| @@ -184,6 +230,7 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 184 | /// | 230 | /// |
| 185 | /// The pin level will be whatever was set before (or low by default). If you want it to begin | 231 | /// The pin level will be whatever was set before (or low by default). If you want it to begin |
| 186 | /// at a specific level, call `set_high`/`set_low` on the pin first. | 232 | /// at a specific level, call `set_high`/`set_low` on the pin first. |
| 233 | #[inline] | ||
| 187 | pub fn set_as_output(&mut self, drive: OutputDrive) { | 234 | pub fn set_as_output(&mut self, drive: OutputDrive) { |
| 188 | self.pin.conf().write(|w| { | 235 | self.pin.conf().write(|w| { |
| 189 | w.dir().output(); | 236 | w.dir().output(); |
| @@ -204,6 +251,7 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 204 | /// | 251 | /// |
| 205 | /// The pin level will be whatever was set before (or low by default). If you want it to begin | 252 | /// The pin level will be whatever was set before (or low by default). If you want it to begin |
| 206 | /// at a specific level, call `set_high`/`set_low` on the pin first. | 253 | /// at a specific level, call `set_high`/`set_low` on the pin first. |
| 254 | #[inline] | ||
| 207 | pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) { | 255 | pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) { |
| 208 | self.pin.conf().write(|w| { | 256 | self.pin.conf().write(|w| { |
| 209 | w.dir().output(); | 257 | w.dir().output(); |
| @@ -216,37 +264,65 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 216 | } | 264 | } |
| 217 | 265 | ||
| 218 | /// Put the pin into disconnected mode. | 266 | /// Put the pin into disconnected mode. |
| 267 | #[inline] | ||
| 219 | pub fn set_as_disconnected(&mut self) { | 268 | pub fn set_as_disconnected(&mut self) { |
| 220 | self.pin.conf().reset(); | 269 | self.pin.conf().reset(); |
| 221 | } | 270 | } |
| 222 | 271 | ||
| 272 | #[inline] | ||
| 223 | pub fn is_high(&self) -> bool { | 273 | pub fn is_high(&self) -> bool { |
| 224 | !self.is_low() | 274 | !self.is_low() |
| 225 | } | 275 | } |
| 226 | 276 | ||
| 277 | #[inline] | ||
| 227 | pub fn is_low(&self) -> bool { | 278 | pub fn is_low(&self) -> bool { |
| 228 | self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0 | 279 | self.pin.block().in_.read().bits() & (1 << self.pin.pin()) == 0 |
| 229 | } | 280 | } |
| 230 | 281 | ||
| 282 | /// Returns current pin level | ||
| 283 | #[inline] | ||
| 284 | pub fn get_level(&self) -> Level { | ||
| 285 | self.is_high().into() | ||
| 286 | } | ||
| 287 | |||
| 231 | /// Set the output as high. | 288 | /// Set the output as high. |
| 289 | #[inline] | ||
| 232 | pub fn set_high(&mut self) { | 290 | pub fn set_high(&mut self) { |
| 233 | self.pin.set_high() | 291 | self.pin.set_high() |
| 234 | } | 292 | } |
| 235 | 293 | ||
| 236 | /// Set the output as low. | 294 | /// Set the output as low. |
| 295 | #[inline] | ||
| 237 | pub fn set_low(&mut self) { | 296 | pub fn set_low(&mut self) { |
| 238 | self.pin.set_low() | 297 | self.pin.set_low() |
| 239 | } | 298 | } |
| 240 | 299 | ||
| 300 | /// Set the output level. | ||
| 301 | #[inline] | ||
| 302 | pub fn set_level(&mut self, level: Level) { | ||
| 303 | match level { | ||
| 304 | Level::Low => self.pin.set_low(), | ||
| 305 | Level::High => self.pin.set_high(), | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 241 | /// Is the output pin set as high? | 309 | /// Is the output pin set as high? |
| 310 | #[inline] | ||
| 242 | pub fn is_set_high(&self) -> bool { | 311 | pub fn is_set_high(&self) -> bool { |
| 243 | !self.is_set_low() | 312 | !self.is_set_low() |
| 244 | } | 313 | } |
| 245 | 314 | ||
| 246 | /// Is the output pin set as low? | 315 | /// Is the output pin set as low? |
| 316 | #[inline] | ||
| 247 | pub fn is_set_low(&self) -> bool { | 317 | pub fn is_set_low(&self) -> bool { |
| 248 | self.pin.block().out.read().bits() & (1 << self.pin.pin()) == 0 | 318 | self.pin.block().out.read().bits() & (1 << self.pin.pin()) == 0 |
| 249 | } | 319 | } |
| 320 | |||
| 321 | /// What level output is set to | ||
| 322 | #[inline] | ||
| 323 | pub fn get_output_level(&self) -> Level { | ||
| 324 | self.is_set_high().into() | ||
| 325 | } | ||
| 250 | } | 326 | } |
| 251 | 327 | ||
| 252 | impl<'d, T: Pin> Drop for Flex<'d, T> { | 328 | impl<'d, T: Pin> Drop for Flex<'d, T> { |
diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 131330e79..c6dbb3d48 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs | |||
| @@ -14,6 +14,24 @@ pub enum Level { | |||
| 14 | High, | 14 | High, |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | impl From<bool> for Level { | ||
| 18 | fn from(val: bool) -> Self { | ||
| 19 | match val { | ||
| 20 | true => Self::High, | ||
| 21 | false => Self::Low, | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | impl Into<bool> for Level { | ||
| 27 | fn into(self) -> bool { | ||
| 28 | match self { | ||
| 29 | Level::Low => false, | ||
| 30 | Level::High => true, | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 17 | /// Represents a pull setting for an input. | 35 | /// Represents a pull setting for an input. |
| 18 | #[derive(Debug, Eq, PartialEq)] | 36 | #[derive(Debug, Eq, PartialEq)] |
| 19 | pub enum Pull { | 37 | pub enum Pull { |
| @@ -34,6 +52,7 @@ pub struct Input<'d, T: Pin> { | |||
| 34 | } | 52 | } |
| 35 | 53 | ||
| 36 | impl<'d, T: Pin> Input<'d, T> { | 54 | impl<'d, T: Pin> Input<'d, T> { |
| 55 | #[inline] | ||
| 37 | pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { | 56 | pub fn new(pin: impl Unborrow<Target = T> + 'd, pull: Pull) -> Self { |
| 38 | let mut pin = Flex::new(pin); | 57 | let mut pin = Flex::new(pin); |
| 39 | pin.set_as_input(); | 58 | pin.set_as_input(); |
| @@ -41,13 +60,21 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 41 | Self { pin } | 60 | Self { pin } |
| 42 | } | 61 | } |
| 43 | 62 | ||
| 63 | #[inline] | ||
| 44 | pub fn is_high(&self) -> bool { | 64 | pub fn is_high(&self) -> bool { |
| 45 | self.pin.is_high() | 65 | self.pin.is_high() |
| 46 | } | 66 | } |
| 47 | 67 | ||
| 68 | #[inline] | ||
| 48 | pub fn is_low(&self) -> bool { | 69 | pub fn is_low(&self) -> bool { |
| 49 | self.pin.is_low() | 70 | self.pin.is_low() |
| 50 | } | 71 | } |
| 72 | |||
| 73 | /// Returns current pin level | ||
| 74 | #[inline] | ||
| 75 | pub fn get_level(&self) -> Level { | ||
| 76 | self.pin.get_level() | ||
| 77 | } | ||
| 51 | } | 78 | } |
| 52 | 79 | ||
| 53 | pub struct Output<'d, T: Pin> { | 80 | pub struct Output<'d, T: Pin> { |
| @@ -79,6 +106,12 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 79 | self.pin.set_low() | 106 | self.pin.set_low() |
| 80 | } | 107 | } |
| 81 | 108 | ||
| 109 | /// Set the output level. | ||
| 110 | #[inline] | ||
| 111 | pub fn set_level(&mut self, level: Level) { | ||
| 112 | self.pin.set_level(level) | ||
| 113 | } | ||
| 114 | |||
| 82 | /// Is the output pin set as high? | 115 | /// Is the output pin set as high? |
| 83 | #[inline] | 116 | #[inline] |
| 84 | pub fn is_set_high(&self) -> bool { | 117 | pub fn is_set_high(&self) -> bool { |
| @@ -91,6 +124,12 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 91 | self.pin.is_set_low() | 124 | self.pin.is_set_low() |
| 92 | } | 125 | } |
| 93 | 126 | ||
| 127 | /// What level output is set to | ||
| 128 | #[inline] | ||
| 129 | pub fn get_output_level(&self) -> Level { | ||
| 130 | self.pin.get_output_level() | ||
| 131 | } | ||
| 132 | |||
| 94 | /// Toggle pin output | 133 | /// Toggle pin output |
| 95 | #[inline] | 134 | #[inline] |
| 96 | pub fn toggle(&mut self) { | 135 | pub fn toggle(&mut self) { |
| @@ -129,6 +168,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 129 | self.pin.set_as_output() | 168 | self.pin.set_as_output() |
| 130 | } | 169 | } |
| 131 | 170 | ||
| 171 | /// Set the output level. | ||
| 172 | #[inline] | ||
| 173 | pub fn set_level(&mut self, level: Level) { | ||
| 174 | match level { | ||
| 175 | Level::Low => self.set_low(), | ||
| 176 | Level::High => self.set_high(), | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 132 | /// Is the output level high? | 180 | /// Is the output level high? |
| 133 | #[inline] | 181 | #[inline] |
| 134 | pub fn is_set_high(&self) -> bool { | 182 | pub fn is_set_high(&self) -> bool { |
| @@ -141,6 +189,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 141 | self.pin.is_set_as_output() | 189 | self.pin.is_set_as_output() |
| 142 | } | 190 | } |
| 143 | 191 | ||
| 192 | /// What level output is set to | ||
| 193 | #[inline] | ||
| 194 | pub fn get_output_level(&self) -> Level { | ||
| 195 | self.is_set_high().into() | ||
| 196 | } | ||
| 197 | |||
| 144 | /// Toggle pin output | 198 | /// Toggle pin output |
| 145 | #[inline] | 199 | #[inline] |
| 146 | pub fn toggle(&mut self) { | 200 | pub fn toggle(&mut self) { |
| @@ -236,6 +290,12 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 236 | unsafe { self.pin.sio_in().read() & self.bit() == 0 } | 290 | unsafe { self.pin.sio_in().read() & self.bit() == 0 } |
| 237 | } | 291 | } |
| 238 | 292 | ||
| 293 | /// Returns current pin level | ||
| 294 | #[inline] | ||
| 295 | pub fn get_level(&self) -> Level { | ||
| 296 | self.is_high().into() | ||
| 297 | } | ||
| 298 | |||
| 239 | /// Set the output as high. | 299 | /// Set the output as high. |
| 240 | #[inline] | 300 | #[inline] |
| 241 | pub fn set_high(&mut self) { | 301 | pub fn set_high(&mut self) { |
| @@ -248,6 +308,15 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 248 | unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } | 308 | unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } |
| 249 | } | 309 | } |
| 250 | 310 | ||
| 311 | /// Set the output level. | ||
| 312 | #[inline] | ||
| 313 | pub fn set_level(&mut self, level: Level) { | ||
| 314 | match level { | ||
| 315 | Level::Low => self.set_low(), | ||
| 316 | Level::High => self.set_high(), | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 251 | /// Is the output level high? | 320 | /// Is the output level high? |
| 252 | #[inline] | 321 | #[inline] |
| 253 | pub fn is_set_high(&self) -> bool { | 322 | pub fn is_set_high(&self) -> bool { |
| @@ -260,6 +329,12 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 260 | !self.is_set_high() | 329 | !self.is_set_high() |
| 261 | } | 330 | } |
| 262 | 331 | ||
| 332 | /// What level output is set to | ||
| 333 | #[inline] | ||
| 334 | pub fn get_output_level(&self) -> Level { | ||
| 335 | self.is_set_high().into() | ||
| 336 | } | ||
| 337 | |||
| 263 | /// Toggle pin output | 338 | /// Toggle pin output |
| 264 | #[inline] | 339 | #[inline] |
| 265 | pub fn toggle(&mut self) { | 340 | pub fn toggle(&mut self) { |
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 806b5eb68..1059ebf86 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs | |||
| @@ -142,6 +142,11 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 142 | } | 142 | } |
| 143 | 143 | ||
| 144 | #[inline] | 144 | #[inline] |
| 145 | pub fn get_level(&self) -> Level { | ||
| 146 | self.is_high().into() | ||
| 147 | } | ||
| 148 | |||
| 149 | #[inline] | ||
| 145 | pub fn is_set_high(&self) -> bool { | 150 | pub fn is_set_high(&self) -> bool { |
| 146 | !self.is_set_low() | 151 | !self.is_set_low() |
| 147 | } | 152 | } |
| @@ -153,6 +158,12 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 153 | state == vals::Odr::LOW | 158 | state == vals::Odr::LOW |
| 154 | } | 159 | } |
| 155 | 160 | ||
| 161 | /// What level output is set to | ||
| 162 | #[inline] | ||
| 163 | pub fn get_output_level(&self) -> Level { | ||
| 164 | self.is_set_high().into() | ||
| 165 | } | ||
| 166 | |||
| 156 | #[inline] | 167 | #[inline] |
| 157 | pub fn set_high(&mut self) { | 168 | pub fn set_high(&mut self) { |
| 158 | self.pin.set_high(); | 169 | self.pin.set_high(); |
| @@ -164,6 +175,14 @@ impl<'d, T: Pin> Flex<'d, T> { | |||
| 164 | self.pin.set_low(); | 175 | self.pin.set_low(); |
| 165 | } | 176 | } |
| 166 | 177 | ||
| 178 | #[inline] | ||
| 179 | pub fn set_level(&mut self, level: Level) { | ||
| 180 | match level { | ||
| 181 | Level::Low => self.pin.set_low(), | ||
| 182 | Level::High => self.pin.set_high(), | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 167 | /// Toggle pin output | 186 | /// Toggle pin output |
| 168 | #[inline] | 187 | #[inline] |
| 169 | pub fn toggle(&mut self) { | 188 | pub fn toggle(&mut self) { |
| @@ -281,6 +300,11 @@ impl<'d, T: Pin> Input<'d, T> { | |||
| 281 | pub fn is_low(&self) -> bool { | 300 | pub fn is_low(&self) -> bool { |
| 282 | self.pin.is_low() | 301 | self.pin.is_low() |
| 283 | } | 302 | } |
| 303 | |||
| 304 | #[inline] | ||
| 305 | pub fn get_level(&self) -> Level { | ||
| 306 | self.pin.get_level() | ||
| 307 | } | ||
| 284 | } | 308 | } |
| 285 | 309 | ||
| 286 | /// Digital input or output level. | 310 | /// Digital input or output level. |
| @@ -291,6 +315,24 @@ pub enum Level { | |||
| 291 | High, | 315 | High, |
| 292 | } | 316 | } |
| 293 | 317 | ||
| 318 | impl From<bool> for Level { | ||
| 319 | fn from(val: bool) -> Self { | ||
| 320 | match val { | ||
| 321 | true => Self::High, | ||
| 322 | false => Self::Low, | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | |||
| 327 | impl Into<bool> for Level { | ||
| 328 | fn into(self) -> bool { | ||
| 329 | match self { | ||
| 330 | Level::Low => false, | ||
| 331 | Level::High => true, | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 294 | /// GPIO output driver. | 336 | /// GPIO output driver. |
| 295 | pub struct Output<'d, T: Pin> { | 337 | pub struct Output<'d, T: Pin> { |
| 296 | pub(crate) pin: Flex<'d, T>, | 338 | pub(crate) pin: Flex<'d, T>, |
| @@ -320,6 +362,12 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 320 | self.pin.set_low(); | 362 | self.pin.set_low(); |
| 321 | } | 363 | } |
| 322 | 364 | ||
| 365 | /// Set the output level. | ||
| 366 | #[inline] | ||
| 367 | pub fn set_level(&mut self, level: Level) { | ||
| 368 | self.pin.set_level(level) | ||
| 369 | } | ||
| 370 | |||
| 323 | /// Is the output pin set as high? | 371 | /// Is the output pin set as high? |
| 324 | #[inline] | 372 | #[inline] |
| 325 | pub fn is_set_high(&self) -> bool { | 373 | pub fn is_set_high(&self) -> bool { |
| @@ -332,6 +380,12 @@ impl<'d, T: Pin> Output<'d, T> { | |||
| 332 | self.pin.is_set_low() | 380 | self.pin.is_set_low() |
| 333 | } | 381 | } |
| 334 | 382 | ||
| 383 | /// What level output is set to | ||
| 384 | #[inline] | ||
| 385 | pub fn get_output_level(&self) -> Level { | ||
| 386 | self.pin.get_output_level() | ||
| 387 | } | ||
| 388 | |||
| 335 | /// Toggle pin output | 389 | /// Toggle pin output |
| 336 | #[inline] | 390 | #[inline] |
| 337 | pub fn toggle(&mut self) { | 391 | pub fn toggle(&mut self) { |
| @@ -368,6 +422,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 368 | self.pin.is_low() | 422 | self.pin.is_low() |
| 369 | } | 423 | } |
| 370 | 424 | ||
| 425 | /// Returns current pin level | ||
| 426 | #[inline] | ||
| 427 | pub fn get_level(&self) -> Level { | ||
| 428 | self.pin.get_level() | ||
| 429 | } | ||
| 430 | |||
| 371 | /// Set the output as high. | 431 | /// Set the output as high. |
| 372 | #[inline] | 432 | #[inline] |
| 373 | pub fn set_high(&mut self) { | 433 | pub fn set_high(&mut self) { |
| @@ -380,10 +440,16 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 380 | self.pin.set_low(); | 440 | self.pin.set_low(); |
| 381 | } | 441 | } |
| 382 | 442 | ||
| 443 | /// Set the output level. | ||
| 444 | #[inline] | ||
| 445 | pub fn set_level(&mut self, level: Level) { | ||
| 446 | self.pin.set_level(level); | ||
| 447 | } | ||
| 448 | |||
| 383 | /// Is the output pin set as high? | 449 | /// Is the output pin set as high? |
| 384 | #[inline] | 450 | #[inline] |
| 385 | pub fn is_set_high(&self) -> bool { | 451 | pub fn is_set_high(&self) -> bool { |
| 386 | !self.is_set_low() | 452 | self.pin.is_set_high() |
| 387 | } | 453 | } |
| 388 | 454 | ||
| 389 | /// Is the output pin set as low? | 455 | /// Is the output pin set as low? |
| @@ -392,6 +458,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { | |||
| 392 | self.pin.is_set_low() | 458 | self.pin.is_set_low() |
| 393 | } | 459 | } |
| 394 | 460 | ||
| 461 | /// What level output is set to | ||
| 462 | #[inline] | ||
| 463 | pub fn get_output_level(&self) -> Level { | ||
| 464 | self.pin.get_output_level() | ||
| 465 | } | ||
| 466 | |||
| 395 | /// Toggle pin output | 467 | /// Toggle pin output |
| 396 | #[inline] | 468 | #[inline] |
| 397 | pub fn toggle(&mut self) { | 469 | pub fn toggle(&mut self) { |
