autd3_driver/
error.rs

1use std::{convert::Infallible, time::Duration};
2
3use autd3_core::{
4    datagram::CombinedError, derive::SamplingConfigError, gain::GainError, link::LinkError,
5    modulation::ModulationError,
6};
7use thiserror::Error;
8
9use crate::{firmware::cpu::GainSTMMode, firmware::fpga::*};
10
11/// A interface for error handling in autd3-driver.
12#[derive(Error, Debug, PartialEq, Clone)]
13#[non_exhaustive]
14pub enum AUTDDriverError {
15    /// Modulation buffer size is out of range.
16    #[error(
17        "Modulation buffer size ({0}) is out of range ([{min}, {max}])",
18        min = MOD_BUF_SIZE_MIN,
19        max = MOD_BUF_SIZE_MAX
20    )]
21    ModulationSizeOutOfRange(usize),
22
23    /// Invalid silencer completion time.
24    #[error("Silencer completion time ({0:?}) must be a multiple of the ultrasound period")]
25    InvalidSilencerCompletionTime(Duration),
26    /// Silencer completion time is out of range.
27    #[error("Silencer completion time ({0:?}) is out of range")]
28    SilencerCompletionTimeOutOfRange(Duration),
29
30    /// Sampling config error
31    #[error("{0}")]
32    SamplingConfig(#[from] SamplingConfigError),
33
34    /// Invalid STM period.
35    #[error("STM sampling period ({1:?}/{0}) must be integer")]
36    STMPeriodInvalid(usize, Duration),
37
38    /// FociSTM buffer size is out of range.
39    #[error(
40        "The number of total foci ({0}) is out of range ([{min}, {max}])",
41        min = STM_BUF_SIZE_MIN,
42        max = FOCI_STM_BUF_SIZE_MAX
43    )]
44    FociSTMTotalSizeOutOfRange(usize),
45    /// Number of foci is out of range.
46    #[error(
47        "Number of foci ({0}) is out of range ([{min}, {max}])",
48        min = 1,
49        max = FOCI_STM_FOCI_NUM_MAX
50    )]
51    FociSTMNumFociOutOfRange(usize),
52    /// FociSTM point is out of range.
53    #[error(
54        "Point coordinate ({0}, {1}, {2}) is out of range ([{x_min}, {x_max}], [{y_min}, {y_max}], [{z_min}, {z_max}])",
55        x_min = FOCI_STM_FIXED_NUM_UNIT * FOCI_STM_FIXED_NUM_LOWER_X as f32,
56        x_max = FOCI_STM_FIXED_NUM_UNIT * FOCI_STM_FIXED_NUM_UPPER_X as f32,
57        y_min = FOCI_STM_FIXED_NUM_UNIT * FOCI_STM_FIXED_NUM_LOWER_Y as f32,
58        y_max = FOCI_STM_FIXED_NUM_UNIT * FOCI_STM_FIXED_NUM_UPPER_Y as f32,
59        z_min = FOCI_STM_FIXED_NUM_UNIT * FOCI_STM_FIXED_NUM_LOWER_Z as f32,
60        z_max = FOCI_STM_FIXED_NUM_UNIT * FOCI_STM_FIXED_NUM_UPPER_Z as f32,
61    )]
62    FociSTMPointOutOfRange(f32, f32, f32),
63    /// GainSTM buffer size is out of range.
64    #[error(
65        "GainSTM size ({0}) is out of range ([{min}, {max}])",
66        min = STM_BUF_SIZE_MIN,
67        max = GAIN_STM_BUF_SIZE_MAX
68    )]
69    GainSTMSizeOutOfRange(usize),
70    /// GainSTM mode is not supported.
71    #[error("GainSTMMode ({0:?}) is not supported")]
72    GainSTMModeNotSupported(GainSTMMode),
73
74    /// Error in the modulation.
75    #[error("{0}")]
76    Modulation(#[from] ModulationError),
77    /// Error in the gain.
78    #[error("{0}")]
79    Gain(#[from] GainError),
80    /// Error in the Link.
81    #[error("{0}")]
82    Link(#[from] LinkError),
83
84    /// Link is closed.
85    #[error("Link is closed")]
86    LinkClosed,
87    /// Failed to confirm the response from the device.
88    #[error("Failed to confirm the response from the device")]
89    ConfirmResponseFailed,
90
91    /// Invalid date time.
92    #[error("The input data is invalid.")]
93    InvalidDateTime,
94
95    /// Not supported tag.
96    ///
97    /// Occurs when the software is not compatible with the firmware.
98    #[error("Not supported tag")]
99    NotSupportedTag,
100    #[doc(hidden)]
101    #[error("Invalid message ID")]
102    InvalidMessageID,
103    #[doc(hidden)]
104    #[error("Invalid info type")]
105    InvalidInfoType,
106    #[doc(hidden)]
107    #[error("Invalid GainSTM mode")]
108    InvalidGainSTMMode,
109    #[doc(hidden)]
110    #[error("Unknown firmware error: {0}")]
111    UnknownFirmwareError(u8),
112    /// Invalid segment transition.
113    #[error("Invalid segment transition")]
114    InvalidSegmentTransition,
115    /// Invalid segment transition mode.
116    #[error("Invalid transition mode")]
117    InvalidTransitionMode,
118    /// Miss transition time.
119    #[error("Miss transition time")]
120    MissTransitionTime,
121    /// Silencer cannot complete phase/intensity completion in the specified sampling period.
122    #[error(
123        "Silencer cannot complete phase/intensity completion in the specified sampling period. Please lower the sampling frequency or make the completion time of Silencer longer than the sampling period."
124    )]
125    InvalidSilencerSettings,
126}
127
128impl AUTDDriverError {
129    #[doc(hidden)]
130    #[must_use]
131    pub const fn firmware_err(ack: u8) -> Self {
132        match ack {
133            0x80 => AUTDDriverError::NotSupportedTag,
134            0x81 => AUTDDriverError::InvalidMessageID,
135            0x84 => AUTDDriverError::InvalidInfoType,
136            0x85 => AUTDDriverError::InvalidGainSTMMode,
137            0x88 => AUTDDriverError::InvalidSegmentTransition,
138            0x8B => AUTDDriverError::MissTransitionTime,
139            0x8E => AUTDDriverError::InvalidSilencerSettings,
140            0x8F => AUTDDriverError::InvalidTransitionMode,
141            _ => AUTDDriverError::UnknownFirmwareError(ack),
142        }
143    }
144}
145
146// GRCOV_EXCL_START
147impl From<Infallible> for AUTDDriverError {
148    fn from(_: Infallible) -> Self {
149        unreachable!()
150    }
151}
152
153impl<E1, E2> From<CombinedError<E1, E2>> for AUTDDriverError
154where
155    E1: std::error::Error,
156    E2: std::error::Error,
157    AUTDDriverError: From<E1> + From<E2>,
158{
159    fn from(err: CombinedError<E1, E2>) -> Self {
160        match err {
161            CombinedError::E1(e) => AUTDDriverError::from(e),
162            CombinedError::E2(e) => AUTDDriverError::from(e),
163        }
164    }
165}
166// GRCOV_EXCL_STOP
167
168#[cfg(test)]
169mod tests {
170    use super::*;
171    use std::error::Error;
172
173    #[test]
174    fn test_unknown_firmware_err() {
175        let err = AUTDDriverError::firmware_err(0xFF);
176        assert!(err.source().is_none());
177        assert_eq!(format!("{}", err), "Unknown firmware error: 255");
178        assert_eq!(format!("{:?}", err), "UnknownFirmwareError(255)");
179    }
180}