autd3_driver/
error.rs

1use std::{convert::Infallible, time::Duration};
2
3use autd3_core::{
4    common::{FOCI_STM_FOCI_NUM_MIN, MOD_BUF_SIZE_MIN, STM_BUF_SIZE_MIN},
5    datagram::CombinedError,
6    firmware::{FirmwareLimits, PulseWidthError, SamplingConfigError},
7    gain::GainError,
8    link::LinkError,
9    modulation::ModulationError,
10};
11use thiserror::Error;
12
13/// A interface for error handling in autd3-driver.
14#[derive(Error, Debug, PartialEq, Clone)]
15#[non_exhaustive]
16pub enum AUTDDriverError {
17    /// Invalid silencer completion time.
18    #[error("Silencer completion time ({0:?}) must be a multiple of the ultrasound period")]
19    InvalidSilencerCompletionTime(Duration),
20    /// Silencer completion time is out of range.
21    #[error("Silencer completion time ({0:?}) is out of range")]
22    SilencerCompletionTimeOutOfRange(Duration),
23
24    /// Sampling config error
25    #[error("{0}")]
26    SamplingConfig(#[from] SamplingConfigError),
27
28    /// Invalid STM period.
29    #[error("STM sampling period ({1:?}/{0}) must be integer")]
30    STMPeriodInvalid(usize, Duration),
31
32    /// Modulation buffer size is out of range.
33    #[error("Modulation buffer size ({0}) is out of range ([{min}, {max}])", min = MOD_BUF_SIZE_MIN, max = .1.mod_buf_size_max)]
34    ModulationSizeOutOfRange(usize, FirmwareLimits),
35
36    /// FociSTM buffer size is out of range.
37    #[error("The number of total foci ({0}) is out of range ([{min}, {max}])", min = STM_BUF_SIZE_MIN, max = .1.foci_stm_buf_size_max)]
38    FociSTMTotalSizeOutOfRange(usize, FirmwareLimits),
39    /// Number of foci is out of range.
40    #[error("Number of foci ({0}) is out of range ([{min}, {max}])", min = FOCI_STM_FOCI_NUM_MIN, max = .1.num_foci_max)]
41    FociSTMNumFociOutOfRange(usize, FirmwareLimits),
42    /// FociSTM point is out of range.
43    #[error(
44        "Point coordinate ({0}, {1}, {2}) is out of range ([{min_x}, {max_x}], [{min_y}, {max_y}], [{min_z}, {max_z}])",
45        min_x = .3.foci_stm_lower_x(),
46        max_x = .3.foci_stm_upper_x(),
47        min_y = .3.foci_stm_lower_y(),
48        max_y = .3.foci_stm_upper_y(),
49        min_z = .3.foci_stm_lower_z(),
50        max_z = .3.foci_stm_upper_z()
51    )]
52    FociSTMPointOutOfRange(f32, f32, f32, FirmwareLimits),
53    /// GainSTM buffer size is out of range.
54    #[error("GainSTM size ({0}) is out of range ([{min}, {max}])", min = STM_BUF_SIZE_MIN, max = .1.gain_stm_buf_size_max)]
55    GainSTMSizeOutOfRange(usize, FirmwareLimits),
56
57    /// GPIO output type is not supported.
58    #[error("GPIO output type ({0}) is not supported")]
59    UnsupportedGPIOOutputType(String),
60
61    /// Pulse width error.
62    #[error("{0}")]
63    PulseWidth(#[from] PulseWidthError),
64
65    /// Error in the modulation.
66    #[error("{0}")]
67    Modulation(#[from] ModulationError),
68    /// Error in the gain.
69    #[error("{0}")]
70    Gain(#[from] GainError),
71    /// Error in the Link.
72    #[error("{0}")]
73    Link(#[from] LinkError),
74
75    /// Unknown group key.
76    #[error("Unknown group key({0})")]
77    UnknownKey(String),
78    /// Unused group key.
79    #[error("Unused group key({0})")]
80    UnusedKey(String),
81
82    /// Failed to confirm the response from the device.
83    #[error("Failed to confirm the response from the device")]
84    ConfirmResponseFailed,
85
86    /// Failed to read firmware version.
87    #[error("Read firmware info failed: {}", .0.iter().enumerate().filter(|&(_, &b)| !b).map(|(i, _)| i.to_string()).collect::<Vec<_>>().join(", "))]
88    ReadFirmwareVersionFailed(Vec<bool>),
89
90    /// Invalid date time.
91    #[error("The input data is invalid.")]
92    InvalidDateTime,
93
94    /// Firmware version mismatch.
95    #[error("Firmware version mismatch")]
96    FirmwareVersionMismatch,
97
98    /// Unsupported operation.
99    #[error("Unsupported operation")]
100    UnsupportedOperation,
101    /// Unsupported firmware.
102    #[error("Unsupported firmware")]
103    UnsupportedFirmware,
104
105    /// Not supported tag.
106    ///
107    /// Occurs when the software is not compatible with the firmware.
108    #[error("Not supported tag")]
109    NotSupportedTag,
110    #[doc(hidden)]
111    #[error("Invalid message ID")]
112    InvalidMessageID,
113    #[doc(hidden)]
114    #[error("Invalid info type")]
115    InvalidInfoType,
116    #[doc(hidden)]
117    #[error("Invalid GainSTM mode")]
118    InvalidGainSTMMode,
119    #[doc(hidden)]
120    #[error("Unknown firmware error: {0}")]
121    UnknownFirmwareError(u8),
122    /// Invalid segment transition.
123    #[error("Invalid segment transition")]
124    InvalidSegmentTransition,
125    /// Invalid segment transition mode.
126    #[error("Invalid transition mode")]
127    InvalidTransitionMode,
128    /// Miss transition time.
129    #[error("Miss transition time")]
130    MissTransitionTime,
131    /// Silencer cannot complete phase/intensity interpolation in the specified sampling period.
132    #[error(
133        "Silencer cannot complete phase/intensity interpolation in the specified sampling period. Please lower the sampling frequency or make the completion time of Silencer longer than the sampling period of the AM/STM."
134    )]
135    InvalidSilencerSettings,
136}
137
138// GRCOV_EXCL_START
139impl From<Infallible> for AUTDDriverError {
140    fn from(_: Infallible) -> Self {
141        unreachable!()
142    }
143}
144
145impl<E1, E2> From<CombinedError<E1, E2>> for AUTDDriverError
146where
147    E1: std::error::Error,
148    E2: std::error::Error,
149    AUTDDriverError: From<E1> + From<E2>,
150{
151    fn from(err: CombinedError<E1, E2>) -> Self {
152        match err {
153            CombinedError::E1(e) => AUTDDriverError::from(e),
154            CombinedError::E2(e) => AUTDDriverError::from(e),
155        }
156    }
157}
158// GRCOV_EXCL_STOP