| 1 | use super::{Function, Scale}; |
| 2 | |
| 3 | /// Error for [Cordic](super::Cordic) |
| 4 | #[derive (Debug)] |
| 5 | pub enum CordicError { |
| 6 | /// Config error |
| 7 | ConfigError(ConfigError), |
| 8 | /// Argument length is incorrect |
| 9 | ArgumentLengthIncorrect, |
| 10 | /// Result buffer length error |
| 11 | ResultLengthNotEnough, |
| 12 | /// Input value is out of range for Q1.x format |
| 13 | NumberOutOfRange(NumberOutOfRange), |
| 14 | /// Argument error |
| 15 | ArgError(ArgError), |
| 16 | } |
| 17 | |
| 18 | impl From<ConfigError> for CordicError { |
| 19 | fn from(value: ConfigError) -> Self { |
| 20 | Self::ConfigError(value) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl From<NumberOutOfRange> for CordicError { |
| 25 | fn from(value: NumberOutOfRange) -> Self { |
| 26 | Self::NumberOutOfRange(value) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl From<ArgError> for CordicError { |
| 31 | fn from(value: ArgError) -> Self { |
| 32 | Self::ArgError(value) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | #[cfg (feature = "defmt" )] |
| 37 | impl defmt::Format for CordicError { |
| 38 | fn format(&self, fmt: defmt::Formatter) { |
| 39 | use CordicError::*; |
| 40 | |
| 41 | match self { |
| 42 | ConfigError(e: &ConfigError) => defmt::write!(fmt, "{}" , e), |
| 43 | ResultLengthNotEnough => defmt::write!(fmt, "Output buffer length is not long enough" ), |
| 44 | ArgumentLengthIncorrect => defmt::write!(fmt, "Argument length incorrect" ), |
| 45 | NumberOutOfRange(e: &NumberOutOfRange) => defmt::write!(fmt, "{}" , e), |
| 46 | ArgError(e: &ArgError) => defmt::write!(fmt, "{}" , e), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// Error during parsing [Cordic::Config](super::Config) |
| 52 | #[allow (dead_code)] |
| 53 | #[derive (Debug)] |
| 54 | pub struct ConfigError { |
| 55 | pub(super) func: Function, |
| 56 | pub(super) scale_range: [u8; 2], |
| 57 | } |
| 58 | |
| 59 | #[cfg (feature = "defmt" )] |
| 60 | impl defmt::Format for ConfigError { |
| 61 | fn format(&self, fmt: defmt::Formatter) { |
| 62 | defmt::write!(fmt, "For FUNCTION: {}," , self.func); |
| 63 | |
| 64 | if self.scale_range[0] == self.scale_range[1] { |
| 65 | defmt::write!(fmt, " SCALE value should be {}" , self.scale_range[0]) |
| 66 | } else { |
| 67 | defmt::write!( |
| 68 | fmt, |
| 69 | " SCALE value should be {} <= SCALE <= {}" , |
| 70 | self.scale_range[0], |
| 71 | self.scale_range[1] |
| 72 | ) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// Input value is out of range for Q1.x format |
| 78 | #[allow (missing_docs)] |
| 79 | #[derive (Debug)] |
| 80 | pub enum NumberOutOfRange { |
| 81 | BelowLowerBound, |
| 82 | AboveUpperBound, |
| 83 | } |
| 84 | |
| 85 | #[cfg (feature = "defmt" )] |
| 86 | impl defmt::Format for NumberOutOfRange { |
| 87 | fn format(&self, fmt: defmt::Formatter) { |
| 88 | use NumberOutOfRange::*; |
| 89 | |
| 90 | match self { |
| 91 | BelowLowerBound => defmt::write!(fmt, "input value should be equal or greater than -1" ), |
| 92 | AboveUpperBound => defmt::write!(fmt, "input value should be equal or less than 1" ), |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// Error on checking input arguments |
| 98 | #[allow (dead_code)] |
| 99 | #[derive (Debug)] |
| 100 | pub struct ArgError { |
| 101 | pub(super) func: Function, |
| 102 | pub(super) scale: Option<Scale>, |
| 103 | pub(super) arg_range: [f32; 2], // only for debug display, f32 is ok |
| 104 | pub(super) inclusive_upper_bound: bool, |
| 105 | pub(super) arg_type: ArgType, |
| 106 | } |
| 107 | |
| 108 | #[cfg (feature = "defmt" )] |
| 109 | impl defmt::Format for ArgError { |
| 110 | fn format(&self, fmt: defmt::Formatter) { |
| 111 | defmt::write!(fmt, "For FUNCTION: {}," , self.func); |
| 112 | |
| 113 | if let Some(scale) = self.scale { |
| 114 | defmt::write!(fmt, " when SCALE is {}," , scale); |
| 115 | } |
| 116 | |
| 117 | defmt::write!(fmt, " {} should be" , self.arg_type); |
| 118 | |
| 119 | if self.inclusive_upper_bound { |
| 120 | defmt::write!( |
| 121 | fmt, |
| 122 | " {} <= {} <= {}" , |
| 123 | self.arg_range[0], |
| 124 | self.arg_type, |
| 125 | self.arg_range[1] |
| 126 | ) |
| 127 | } else { |
| 128 | defmt::write!( |
| 129 | fmt, |
| 130 | " {} <= {} < {}" , |
| 131 | self.arg_range[0], |
| 132 | self.arg_type, |
| 133 | self.arg_range[1] |
| 134 | ) |
| 135 | }; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #[derive (Debug)] |
| 140 | #[cfg_attr (feature = "defmt" , derive(defmt::Format))] |
| 141 | pub(super) enum ArgType { |
| 142 | Arg1, |
| 143 | Arg2, |
| 144 | } |
| 145 | |