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