embassy/embassy-stm32/src/cordic/errors.rs

145 lines
3.8 KiB
Rust
Raw Normal View History

2024-03-19 22:19:06 +08:00
use super::{Function, Scale};
/// Error for [Cordic](super::Cordic)
#[derive(Debug)]
pub enum CordicError {
/// Config error
ConfigError(ConfigError),
2024-03-22 17:29:10 +08:00
/// Argument length is incorrect
ArgumentLengthIncorrect,
/// Result buffer length error
ResultLengthNotEnough,
2024-03-21 13:25:40 +08:00
/// Input value is out of range for Q1.x format
NumberOutOfRange(NumberOutOfRange),
2024-03-22 17:29:10 +08:00
/// Argument error
ArgError(ArgError),
2024-03-21 13:25:40 +08:00
}
impl From<ConfigError> for CordicError {
fn from(value: ConfigError) -> Self {
Self::ConfigError(value)
}
}
impl From<NumberOutOfRange> for CordicError {
fn from(value: NumberOutOfRange) -> Self {
Self::NumberOutOfRange(value)
}
2024-03-19 22:19:06 +08:00
}
2024-03-22 17:29:10 +08:00
impl From<ArgError> for CordicError {
fn from(value: ArgError) -> Self {
Self::ArgError(value)
}
}
2024-03-19 22:19:06 +08:00
#[cfg(feature = "defmt")]
impl defmt::Format for CordicError {
fn format(&self, fmt: defmt::Formatter) {
use CordicError::*;
match self {
ConfigError(e) => defmt::write!(fmt, "{}", e),
2024-03-22 17:29:10 +08:00
ResultLengthNotEnough => defmt::write!(fmt, "Output buffer length is not long enough"),
ArgumentLengthIncorrect => defmt::write!(fmt, "Argument length incorrect"),
2024-03-21 13:25:40 +08:00
NumberOutOfRange(e) => defmt::write!(fmt, "{}", e),
2024-03-22 17:29:10 +08:00
ArgError(e) => defmt::write!(fmt, "{}", e),
2024-03-19 22:19:06 +08:00
}
}
}
2024-03-22 00:24:53 +08:00
/// Error during parsing [Cordic::Config](super::Config)
2024-03-21 16:06:34 +08:00
#[allow(dead_code)]
2024-03-19 22:19:06 +08:00
#[derive(Debug)]
pub struct ConfigError {
pub(super) func: Function,
pub(super) scale_range: [u8; 2],
}
#[cfg(feature = "defmt")]
impl defmt::Format for ConfigError {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "For FUNCTION: {},", self.func);
if self.scale_range[0] == self.scale_range[1] {
defmt::write!(fmt, " SCALE value should be {}", self.scale_range[0])
} else {
defmt::write!(
fmt,
" SCALE value should be {} <= SCALE <= {}",
self.scale_range[0],
self.scale_range[1]
)
}
}
}
2024-03-22 17:29:10 +08:00
/// Input value is out of range for Q1.x format
#[allow(missing_docs)]
#[derive(Debug)]
pub enum NumberOutOfRange {
BelowLowerBound,
AboveUpperBound,
}
#[cfg(feature = "defmt")]
impl defmt::Format for NumberOutOfRange {
fn format(&self, fmt: defmt::Formatter) {
use NumberOutOfRange::*;
match self {
BelowLowerBound => defmt::write!(fmt, "input value should be equal or greater than -1"),
AboveUpperBound => defmt::write!(fmt, "input value should be equal or less than 1"),
}
}
}
2024-03-19 22:19:06 +08:00
/// Error on checking input arguments
2024-03-21 16:06:34 +08:00
#[allow(dead_code)]
2024-03-19 22:19:06 +08:00
#[derive(Debug)]
pub struct ArgError {
pub(super) func: Function,
pub(super) scale: Option<Scale>,
pub(super) arg_range: [f32; 2], // only for debug display, f32 is ok
pub(super) inclusive_upper_bound: bool,
pub(super) arg_type: ArgType,
}
#[cfg(feature = "defmt")]
impl defmt::Format for ArgError {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "For FUNCTION: {},", self.func);
if let Some(scale) = self.scale {
defmt::write!(fmt, " when SCALE is {},", scale);
}
2024-03-21 13:25:40 +08:00
defmt::write!(fmt, " {} should be", self.arg_type);
2024-03-19 22:19:06 +08:00
2024-03-21 13:25:40 +08:00
if self.inclusive_upper_bound {
defmt::write!(
fmt,
" {} <= {} <= {}",
self.arg_range[0],
self.arg_type,
self.arg_range[1]
)
} else {
defmt::write!(
fmt,
" {} <= {} < {}",
self.arg_range[0],
self.arg_type,
self.arg_range[1]
)
};
2024-03-19 22:19:06 +08:00
}
}
#[derive(Debug)]
2024-03-21 13:25:40 +08:00
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2024-03-19 22:19:06 +08:00
pub(super) enum ArgType {
Arg1,
Arg2,
}