autd3_driver/datagram/
silencer.rs1use std::{convert::Infallible, num::NonZeroU16};
2
3use autd3_core::{
4 common::{SILENCER_STEPS_INTENSITY_DEFAULT, SILENCER_STEPS_PHASE_DEFAULT, ULTRASOUND_PERIOD},
5 datagram::{Datagram, DeviceMask},
6 environment::Environment,
7 geometry::Geometry,
8};
9
10pub trait SilencerConfig: Clone + Copy {}
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct FixedCompletionTime {
15 pub intensity: std::time::Duration,
19 pub phase: std::time::Duration,
23 pub strict: bool,
31}
32impl SilencerConfig for FixedCompletionTime {}
33
34impl Default for FixedCompletionTime {
35 fn default() -> Self {
36 FixedCompletionTime {
37 intensity: SILENCER_STEPS_INTENSITY_DEFAULT as u32 * ULTRASOUND_PERIOD,
38 phase: SILENCER_STEPS_PHASE_DEFAULT as u32 * ULTRASOUND_PERIOD,
39 strict: true,
40 }
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46#[repr(C)]
47pub struct FixedCompletionSteps {
48 pub intensity: NonZeroU16,
52 pub phase: NonZeroU16,
56 pub strict: bool,
64}
65impl SilencerConfig for FixedCompletionSteps {}
66
67impl Default for FixedCompletionSteps {
68 fn default() -> Self {
69 FixedCompletionSteps {
70 intensity: NonZeroU16::new(SILENCER_STEPS_INTENSITY_DEFAULT).unwrap(),
71 phase: NonZeroU16::new(SILENCER_STEPS_PHASE_DEFAULT).unwrap(),
72 strict: true,
73 }
74 }
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79#[repr(C)]
80pub struct FixedUpdateRate {
81 pub intensity: NonZeroU16,
85 pub phase: NonZeroU16,
89}
90impl SilencerConfig for FixedUpdateRate {}
91
92#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub struct Silencer<T> {
95 pub config: T,
97}
98
99impl<T: SilencerConfig> Silencer<T> {
100 #[must_use]
102 pub const fn new(config: T) -> Self {
103 Self { config }
104 }
105}
106
107impl Silencer<FixedCompletionSteps> {
108 #[must_use]
110 pub const fn disable() -> Silencer<FixedCompletionSteps> {
111 Silencer {
112 config: FixedCompletionSteps {
113 intensity: NonZeroU16::MIN,
114 phase: NonZeroU16::MIN,
115 strict: true,
116 },
117 }
118 }
119}
120
121impl Default for Silencer<FixedCompletionSteps> {
122 fn default() -> Self {
123 Silencer {
124 config: Default::default(),
125 }
126 }
127}
128
129impl<T: SilencerConfig> Datagram<'_> for Silencer<T> {
130 type G = T;
131 type Error = Infallible;
132
133 fn operation_generator(
134 self,
135 _: &Geometry,
136 _: &Environment,
137 _: &DeviceMask,
138 ) -> Result<Self::G, Self::Error> {
139 Ok(self.config)
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146
147 #[test]
148 fn disable() {
149 let s = Silencer::disable();
150 assert_eq!(1, s.config.intensity.get());
151 assert_eq!(1, s.config.phase.get());
152 assert!(s.config.strict);
153 }
154
155 #[test]
156 fn fixed_completion_steps_default() {
157 let s: Silencer<FixedCompletionSteps> = Silencer::default();
158 assert_eq!(10, s.config.intensity.get());
159 assert_eq!(40, s.config.phase.get());
160 assert!(s.config.strict);
161 }
162
163 #[test]
164 fn fixed_completion_time_default() {
165 let s: Silencer<FixedCompletionTime> = Silencer::new(Default::default());
166 assert_eq!(std::time::Duration::from_micros(250), s.config.intensity);
167 assert_eq!(std::time::Duration::from_micros(1000), s.config.phase);
168 assert!(s.config.strict);
169 }
170}