From 98dcce81ca239a0bb04b91a6f109aee6c0b18ada Mon Sep 17 00:00:00 2001 From: chemicstry Date: Wed, 13 Jul 2022 01:43:22 +0300 Subject: [PATCH] Add more convenience GPIO functions --- embassy-stm32/src/gpio.rs | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index 806b5eb68..b97d5772e 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs @@ -141,6 +141,14 @@ impl<'d, T: Pin> Flex<'d, T> { state == vals::Idr::LOW } + #[inline] + pub fn get_level(&self) -> Level { + match self.is_high() { + true => Level::High, + false => Level::Low, + } + } + #[inline] pub fn is_set_high(&self) -> bool { !self.is_set_low() @@ -153,6 +161,15 @@ impl<'d, T: Pin> Flex<'d, T> { state == vals::Odr::LOW } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + match self.is_set_high() { + true => Level::High, + false => Level::Low, + } + } + #[inline] pub fn set_high(&mut self) { self.pin.set_high(); @@ -164,6 +181,14 @@ impl<'d, T: Pin> Flex<'d, T> { self.pin.set_low(); } + #[inline] + pub fn set_level(&mut self, level: Level) { + match level { + Level::Low => self.pin.set_low(), + Level::High => self.pin.set_high(), + } + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -281,6 +306,14 @@ impl<'d, T: Pin> Input<'d, T> { pub fn is_low(&self) -> bool { self.pin.is_low() } + + #[inline] + pub fn get_level(&self) -> Level { + match self.pin.is_high() { + true => Level::High, + false => Level::Low, + } + } } /// Digital input or output level. @@ -291,6 +324,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, + } + } +} + /// GPIO output driver. pub struct Output<'d, T: Pin> { pub(crate) pin: Flex<'d, T>, @@ -320,6 +371,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 { @@ -332,6 +392,15 @@ 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 { + match self.pin.is_set_high() { + true => Level::High, + false => Level::Low, + } + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) { @@ -368,6 +437,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.is_low() } + /// Returns current pin level + #[inline] + pub fn get_level(&self) -> Level { + match self.pin.is_high() { + true => Level::High, + false => Level::Low, + } + } + /// Set the output as high. #[inline] pub fn set_high(&mut self) { @@ -380,6 +458,15 @@ impl<'d, T: Pin> OutputOpenDrain<'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 { @@ -392,6 +479,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { self.pin.is_set_low() } + /// What level output is set to + #[inline] + pub fn get_set_level(&self) -> Level { + match self.pin.is_set_high() { + true => Level::High, + false => Level::Low, + } + } + /// Toggle pin output #[inline] pub fn toggle(&mut self) {