Add more convenience GPIO functions

This commit is contained in:
chemicstry 2022-07-13 01:43:22 +03:00
parent e4cacc3bb8
commit 98dcce81ca

View file

@ -141,6 +141,14 @@ impl<'d, T: Pin> Flex<'d, T> {
state == vals::Idr::LOW state == vals::Idr::LOW
} }
#[inline]
pub fn get_level(&self) -> Level {
match self.is_high() {
true => Level::High,
false => Level::Low,
}
}
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
!self.is_set_low() !self.is_set_low()
@ -153,6 +161,15 @@ impl<'d, T: Pin> Flex<'d, T> {
state == vals::Odr::LOW 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] #[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
self.pin.set_high(); self.pin.set_high();
@ -164,6 +181,14 @@ impl<'d, T: Pin> Flex<'d, T> {
self.pin.set_low(); 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 /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {
@ -281,6 +306,14 @@ impl<'d, T: Pin> Input<'d, T> {
pub fn is_low(&self) -> bool { pub fn is_low(&self) -> bool {
self.pin.is_low() 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. /// Digital input or output level.
@ -291,6 +324,24 @@ pub enum Level {
High, High,
} }
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl Into<bool> for Level {
fn into(self) -> bool {
match self {
Level::Low => false,
Level::High => true,
}
}
}
/// GPIO output driver. /// GPIO output driver.
pub struct Output<'d, T: Pin> { pub struct Output<'d, T: Pin> {
pub(crate) pin: Flex<'d, T>, pub(crate) pin: Flex<'d, T>,
@ -320,6 +371,15 @@ impl<'d, T: Pin> Output<'d, T> {
self.pin.set_low(); 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? /// Is the output pin set as high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
@ -332,6 +392,15 @@ impl<'d, T: Pin> Output<'d, T> {
self.pin.is_set_low() 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 /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {
@ -368,6 +437,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.is_low() 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. /// Set the output as high.
#[inline] #[inline]
pub fn set_high(&mut self) { pub fn set_high(&mut self) {
@ -380,6 +458,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.set_low(); 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? /// Is the output pin set as high?
#[inline] #[inline]
pub fn is_set_high(&self) -> bool { pub fn is_set_high(&self) -> bool {
@ -392,6 +479,15 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> {
self.pin.is_set_low() 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 /// Toggle pin output
#[inline] #[inline]
pub fn toggle(&mut self) { pub fn toggle(&mut self) {