use core::{convert::From, ops::Not};
#[cfg(feature = "defmt-03")]
use crate::defmt;
pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}
impl Error for core::convert::Infallible {
fn kind(&self) -> ErrorKind {
match *self {}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[non_exhaustive]
pub enum ErrorKind {
Other,
}
impl Error for ErrorKind {
#[inline]
fn kind(&self) -> ErrorKind {
*self
}
}
impl core::fmt::Display for ErrorKind {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Other => write!(
f,
"A different error occurred. The original error may contain more information"
),
}
}
}
pub trait ErrorType {
type Error: Error;
}
impl<T: ErrorType + ?Sized> ErrorType for &T {
type Error = T::Error;
}
impl<T: ErrorType + ?Sized> ErrorType for &mut T {
type Error = T::Error;
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum PinState {
Low,
High,
}
impl From<bool> for PinState {
#[inline]
fn from(value: bool) -> Self {
match value {
false => PinState::Low,
true => PinState::High,
}
}
}
impl Not for PinState {
type Output = PinState;
#[inline]
fn not(self) -> Self::Output {
match self {
PinState::High => PinState::Low,
PinState::Low => PinState::High,
}
}
}
impl From<PinState> for bool {
#[inline]
fn from(value: PinState) -> bool {
match value {
PinState::Low => false,
PinState::High => true,
}
}
}
pub trait OutputPin: ErrorType {
fn set_low(&mut self) -> Result<(), Self::Error>;
fn set_high(&mut self) -> Result<(), Self::Error>;
#[inline]
fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> {
match state {
PinState::Low => self.set_low(),
PinState::High => self.set_high(),
}
}
}
impl<T: OutputPin + ?Sized> OutputPin for &mut T {
#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
T::set_low(self)
}
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
T::set_high(self)
}
#[inline]
fn set_state(&mut self, state: PinState) -> Result<(), Self::Error> {
T::set_state(self, state)
}
}
pub trait StatefulOutputPin: OutputPin {
fn is_set_high(&mut self) -> Result<bool, Self::Error>;
fn is_set_low(&mut self) -> Result<bool, Self::Error>;
fn toggle(&mut self) -> Result<(), Self::Error> {
let was_low: bool = self.is_set_low()?;
self.set_state(PinState::from(was_low))
}
}
impl<T: StatefulOutputPin + ?Sized> StatefulOutputPin for &mut T {
#[inline]
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
T::is_set_high(self)
}
#[inline]
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
T::is_set_low(self)
}
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
T::toggle(self)
}
}
pub trait InputPin: ErrorType {
fn is_high(&mut self) -> Result<bool, Self::Error>;
fn is_low(&mut self) -> Result<bool, Self::Error>;
}
impl<T: InputPin + ?Sized> InputPin for &mut T {
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
T::is_high(self)
}
#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
T::is_low(self)
}
}