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#[derive(Error, Debug, PartialEq, Clone)]
16#[non_exhaustive]
17pub enum AUTDDriverError {
18 #[error("Silencer completion time ({0:?}) must be a multiple of the ultrasound period")]
20 InvalidSilencerCompletionTime(Duration),
21 #[error("Silencer completion time ({0:?}) is out of range")]
23 SilencerCompletionTimeOutOfRange(Duration),
24
25 #[error("{0}")]
27 SamplingConfig(#[from] SamplingConfigError),
28
29 #[error("STM sampling period ({1:?}/{0}) must be integer")]
31 STMPeriodInvalid(usize, Duration),
32
33 #[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 #[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 #[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 #[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 #[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 #[error("GPIO output type ({0}) is not supported")]
60 UnsupportedGPIOOutputType(String),
61
62 #[error("{0}")]
64 Modulation(#[from] ModulationError),
65 #[error("{0}")]
67 Gain(#[from] GainError),
68 #[error("{0}")]
70 Link(#[from] LinkError),
71
72 #[error("Unknown group key({0})")]
74 UnknownKey(String),
75 #[error("Unused group key({0})")]
77 UnusedKey(String),
78
79 #[error("Failed to confirm the response from the device")]
81 ConfirmResponseFailed,
82
83 #[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 #[error("The input data is invalid.")]
89 InvalidDateTime,
90
91 #[error("Firmware version mismatch")]
93 FirmwareVersionMismatch,
94
95 #[error("Unsupported operation")]
97 UnsupportedOperation,
98 #[error("Unsupported firmware")]
100 UnsupportedFirmware,
101
102 #[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 #[error("Invalid segment transition")]
121 InvalidSegmentTransition,
122 #[error("Invalid transition mode")]
124 InvalidTransitionMode,
125 #[error("Miss transition time")]
127 MissTransitionTime,
128 #[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
135impl 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