use crate::pac::gpio::Gpio;
use core::{convert::Infallible, marker::PhantomData};
use super::Level;
pub trait PinGpio {
const GPIO: Gpio;
const MASK: u32;
}
pub struct FlashPin<G> {
phantom: PhantomData<G>,
}
impl<G: PinGpio> FlashPin<G> {
pub const fn new() -> Self {
FlashPin {
phantom: PhantomData,
}
}
#[inline]
pub fn set_high(&self) {
super::pin_ctrl::pin_set_high(G::GPIO, G::MASK);
}
#[inline]
pub fn set_low(&self) {
super::pin_ctrl::pin_set_low(G::GPIO, G::MASK);
}
#[inline]
pub fn set_level(&self, level: Level) {
match level {
Level::Low => self.set_low(),
Level::High => self.set_high(),
}
}
#[inline]
pub fn is_set_high(&self) -> bool {
super::pin_ctrl::pin_is_set_high(G::GPIO, G::MASK)
}
#[inline]
pub fn is_set_low(&self) -> bool {
super::pin_ctrl::pin_is_set_low(G::GPIO, G::MASK)
}
#[inline]
pub fn toggle(&self) {
if self.is_set_low() {
self.set_high()
} else {
self.set_low()
}
}
#[inline]
pub fn is_high(&self) -> bool {
super::pin_ctrl::pin_is_high(G::GPIO, G::MASK)
}
#[inline]
pub fn is_low(&self) -> bool {
super::pin_ctrl::pin_is_low(G::GPIO, G::MASK)
}
#[inline]
pub fn get_level(&self) -> Level {
self.is_high().into()
}
}
impl<G: PinGpio> embedded_hal::digital::ErrorType for FlashPin<G> {
type Error = Infallible;
}
impl<G: PinGpio> embedded_hal::digital::InputPin for FlashPin<G> {
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(FlashPin::is_high(self))
}
#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(FlashPin::is_low(self))
}
}
impl<G: PinGpio> embedded_hal::digital::OutputPin for FlashPin<G> {
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
FlashPin::set_high(self);
Ok(())
}
#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
FlashPin::set_low(self);
Ok(())
}
}
impl<G: PinGpio> embedded_hal::digital::StatefulOutputPin for FlashPin<G> {
#[inline]
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(FlashPin::is_set_high(self))
}
#[inline]
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(FlashPin::is_set_low(self))
}
}