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#[derive(Error, Debug, PartialEq, Clone)]
15#[non_exhaustive]
16pub enum AUTDDriverError {
17 #[error("Silencer completion time ({0:?}) must be a multiple of the ultrasound period")]
19 InvalidSilencerCompletionTime(Duration),
20 #[error("Silencer completion time ({0:?}) is out of range")]
22 SilencerCompletionTimeOutOfRange(Duration),
23
24 #[error("{0}")]
26 SamplingConfig(#[from] SamplingConfigError),
27
28 #[error("STM sampling period ({1:?}/{0}) must be integer")]
30 STMPeriodInvalid(usize, Duration),
31
32 #[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 #[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 #[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 #[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 #[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 #[error("GPIO output type ({0}) is not supported")]
59 UnsupportedGPIOOutputType(String),
60
61 #[error("{0}")]
63 PulseWidth(#[from] PulseWidthError),
64
65 #[error("{0}")]
67 Modulation(#[from] ModulationError),
68 #[error("{0}")]
70 Gain(#[from] GainError),
71 #[error("{0}")]
73 Link(#[from] LinkError),
74
75 #[error("Unknown group key({0})")]
77 UnknownKey(String),
78 #[error("Unused group key({0})")]
80 UnusedKey(String),
81
82 #[error("Failed to confirm the response from the device")]
84 ConfirmResponseFailed,
85
86 #[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 #[error("The input data is invalid.")]
92 InvalidDateTime,
93
94 #[error("Firmware version mismatch")]
96 FirmwareVersionMismatch,
97
98 #[error("Unsupported operation")]
100 UnsupportedOperation,
101 #[error("Unsupported firmware")]
103 UnsupportedFirmware,
104
105 #[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 #[error("Invalid segment transition")]
124 InvalidSegmentTransition,
125 #[error("Invalid transition mode")]
127 InvalidTransitionMode,
128 #[error("Miss transition time")]
130 MissTransitionTime,
131 #[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
138impl 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