use core::fmt;
use core::fmt::{Display, Formatter};
#[cfg(not(feature = "async"))]
use embedded_hal::spi;
#[cfg(feature = "async")]
use embedded_hal_async::spi;
#[cfg(feature = "defmt")]
use defmt::Format;
use crate::ll;
pub enum Error<SPI>
where
SPI: spi::ErrorType,
{
Spi(ll::Error<SPI>),
Fcs,
Phy,
BufferTooSmall {
required_len: usize,
},
ReedSolomon,
FrameWaitTimeout,
Overrun,
PreambleDetectionTimeout,
SfdTimeout,
FrameFilteringRejection,
Frame(byte::Error),
DelayedSendTooLate,
DelayedSendPowerUpWarning,
InvalidConfiguration,
RxNotFinished,
StillAsleep,
BadRssiCalculation,
RxConfigFrameFilteringUnsupported,
InitializationFailed,
PGFCalibrationFailed,
}
impl<SPI> From<ll::Error<SPI>> for Error<SPI>
where
SPI: spi::ErrorType,
{
fn from(error: ll::Error<SPI>) -> Self {
Error::Spi(error)
}
}
impl<SPI> Display for Error<SPI>
where
SPI: spi::ErrorType,
{
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self)
}
}
#[cfg(feature = "std")]
impl<SPI> std::error::Error for Error<SPI> where SPI: spi::ErrorType {}
impl<SPI> fmt::Debug for Error<SPI>
where
SPI: spi::ErrorType,
SPI::Error: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Spi(error) => write!(f, "Spi({:?})", error),
Error::Fcs => write!(f, "Fcs"),
Error::Phy => write!(f, "Phy"),
Error::BufferTooSmall { required_len } => {
write!(f, "BufferTooSmall {{ required_len: {:?} }}", required_len,)
}
Error::ReedSolomon => write!(f, "ReedSolomon"),
Error::FrameWaitTimeout => write!(f, "FrameWaitTimeout"),
Error::Overrun => write!(f, "Overrun"),
Error::PreambleDetectionTimeout => write!(f, "PreambleDetectionTimeout"),
Error::SfdTimeout => write!(f, "SfdTimeout"),
Error::FrameFilteringRejection => write!(f, "FrameFilteringRejection"),
Error::Frame(error) => write!(f, "Frame({:?})", error),
Error::DelayedSendTooLate => write!(f, "DelayedSendTooLate"),
Error::DelayedSendPowerUpWarning => write!(f, "DelayedSendPowerUpWarning"),
Error::InvalidConfiguration => write!(f, "InvalidConfiguration"),
Error::RxNotFinished => write!(f, "RxNotFinished"),
Error::StillAsleep => write!(f, "StillAsleep"),
Error::BadRssiCalculation => write!(f, "BadRssiCalculation"),
Error::RxConfigFrameFilteringUnsupported => {
write!(f, "RxConfigFrameFilteringUnsupported")
}
Error::InitializationFailed => write!(f, "InitializationFailed"),
Error::PGFCalibrationFailed => write!(f, "PGFCalibrationFailed"),
}
}
}
#[cfg(feature = "defmt")]
impl<SPI> Format for Error<SPI>
where
SPI: spi::SpiDevice<u8>,
SPI::Error: defmt::Format,
{
fn format(&self, f: defmt::Formatter) {
match self {
Error::Spi(error) => defmt::write!(f, "Spi({:?})", error),
Error::Fcs => defmt::write!(f, "Fcs"),
Error::Phy => defmt::write!(f, "Phy"),
Error::BufferTooSmall { required_len } => {
defmt::write!(f, "BufferTooSmall {{ required_len: {:?} }}", required_len,)
}
Error::ReedSolomon => defmt::write!(f, "ReedSolomon"),
Error::FrameWaitTimeout => defmt::write!(f, "FrameWaitTimeout"),
Error::Overrun => defmt::write!(f, "Overrun"),
Error::PreambleDetectionTimeout => defmt::write!(f, "PreambleDetectionTimeout"),
Error::SfdTimeout => defmt::write!(f, "SfdTimeout"),
Error::FrameFilteringRejection => defmt::write!(f, "FrameFilteringRejection"),
Error::Frame(error) => defmt::write!(f, "Frame({:?})", defmt::Debug2Format(error)),
Error::DelayedSendTooLate => defmt::write!(f, "DelayedSendTooLate"),
Error::DelayedSendPowerUpWarning => defmt::write!(f, "DelayedSendPowerUpWarning"),
Error::InvalidConfiguration => defmt::write!(f, "InvalidConfiguration"),
Error::RxNotFinished => defmt::write!(f, "RxNotFinished"),
Error::StillAsleep => defmt::write!(f, "StillAsleep"),
Error::BadRssiCalculation => defmt::write!(f, "BadRssiCalculation"),
Error::RxConfigFrameFilteringUnsupported => {
defmt::write!(f, "RxConfigFrameFilteringUnsupported")
}
Error::InitializationFailed => defmt::write!(f, "InitializationFailed"),
Error::PGFCalibrationFailed => defmt::write!(f, "PGFCalibrationFailed"),
}
}
}
#[cfg(test)]
mod test {
use super::*;
use embedded_hal_mock::eh1::spi::Mock as SpiMock;
#[test]
fn test_debug() {
let error = Error::<SpiMock<u8>>::BufferTooSmall { required_len: 42 };
let s = std::format!("error: {:?}", error);
assert_eq!(s, "error: BufferTooSmall { required_len: 42 }");
}
}