From 57002875d6bf9ac4378955a4e33f77154fd32edd Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 02:22:46 +0300 Subject: [PATCH] Add convenience GPIO functions to RP --- embassy-rp/src/gpio.rs | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 131330e79..5e8126b4d 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -14,6 +14,24 @@ pub enum Level { High, } +impl From for Level { + fn from(val: bool) -> Self { + match val { + true => Self::High, + false => Self::Low, + } + } +} + +impl Into for Level { + fn into(self) -> bool { + match self { + Level::Low => false, + Level::High => true, + } + } +} + /// Represents a pull setting for an input. #[derive(Debug, Eq, PartialEq)] pub enum Pull { @@ -34,6 +52,7 @@ pub struct Input<'d, T: Pin> { } impl<'d, T: Pin> Input<'d, T> { + #[inline] pub fn new(pin: impl Unborrow + 'd, pull: Pull) -> Self { let mut pin = Flex::new(pin); pin.set_as_input(); @@ -41,13 +60,21 @@ impl<'d, T: Pin> Input<'d, T> { Self { pin } } + #[inline] pub fn is_high(&self) -> bool { self.pin.is_high() } + #[inline] pub fn is_low(&self) -> bool { self.pin.is_low() } + + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + self.pin.is_high().into() + } } pub struct Output<'d, T: Pin> { @@ -79,6 +106,15 @@ impl<'d, T: Pin> Output<'d, T> { self.pin.set_low() } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Is the output pin set as high? #[inline] pub fn is_set_high(&self) -> bool { @@ -91,6 +127,12 @@ impl<'d, T: Pin> Output<'d, T> { self.pin.is_set_low() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.pin.is_set_high().into() + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -129,6 +171,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.set_as_output() } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.set_low(), + Level::High => self.set_high(), + } + } + /// Is the output level high? #[inline] pub fn is_set_high(&self) -> bool { @@ -141,6 +192,12 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.is_set_as_output() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.is_set_high().into() + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -236,6 +293,12 @@ impl<'d, T: Pin> Flex<'d, T> { unsafe { self.pin.sio_in().read() & self.bit() == 0 } } + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + self.is_high().into() + } + /// Set the output as high. #[inline] pub fn set_high(&mut self) { @@ -248,6 +311,15 @@ impl<'d, T: Pin> Flex<'d, T> { unsafe { self.pin.sio_out().value_clr().write_value(self.bit()) } } + /// Set the output level. + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.set_low(), + Level::High => self.set_high(), + } + } + /// Is the output level high? #[inline] pub fn is_set_high(&self) -> bool { @@ -260,6 +332,12 @@ impl<'d, T: Pin> Flex<'d, T> { !self.is_set_high() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + self.is_set_high().into() + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) {