autd3capi_def/dynamic_datagram/
mod.rs

1/*
2 * File: mod.rs
3 * Project: dynamic_datagram
4 * Created Date: 06/12/2023
5 * Author: Shun Suzuki
6 * -----
7 * Last Modified: 06/12/2023
8 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
9 * -----
10 * Copyright (c) 2023 Shun Suzuki. All rights reserved.
11 *
12 */
13
14#![allow(clippy::missing_safety_doc)]
15
16mod debug;
17mod force_fan;
18mod mod_delay;
19mod reads_fpga_info;
20
21pub use debug::*;
22pub use force_fan::*;
23pub use mod_delay::*;
24pub use reads_fpga_info::*;
25
26use std::time::Duration;
27
28use autd3::prelude::*;
29use autd3_driver::{
30    datagram::Datagram, error::AUTDInternalError, fpga::SILENCER_STEP_DEFAULT, operation::Operation,
31};
32
33use crate::{G, M};
34
35pub trait DynamicDatagram {
36    #[allow(clippy::type_complexity)]
37    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError>;
38
39    fn timeout(&self) -> Option<Duration>;
40}
41
42pub struct DynamicDatagramPack {
43    pub d: Box<Box<dyn DynamicDatagram>>,
44    pub timeout: Option<std::time::Duration>,
45}
46
47impl Datagram for DynamicDatagramPack {
48    type O1 = Box<dyn Operation>;
49    type O2 = Box<dyn Operation>;
50
51    fn operation(self) -> Result<(Self::O1, Self::O2), AUTDInternalError> {
52        let Self { mut d, .. } = self;
53        d.operation()
54    }
55
56    fn timeout(&self) -> Option<Duration> {
57        if self.timeout.is_some() {
58            self.timeout
59        } else {
60            self.d.timeout()
61        }
62    }
63}
64
65pub struct DynamicDatagramPack2 {
66    pub d1: Box<Box<dyn DynamicDatagram>>,
67    pub d2: Box<Box<dyn DynamicDatagram>>,
68    pub timeout: Option<std::time::Duration>,
69}
70
71impl Datagram for DynamicDatagramPack2 {
72    type O1 = Box<dyn Operation>;
73    type O2 = Box<dyn Operation>;
74
75    fn operation(self) -> Result<(Self::O1, Self::O2), AUTDInternalError> {
76        let Self { mut d1, mut d2, .. } = self;
77        let (op1, _) = d1.operation()?;
78        let (op2, _) = d2.operation()?;
79        Ok((op1, op2))
80    }
81
82    fn timeout(&self) -> Option<Duration> {
83        self.timeout
84    }
85}
86
87impl DynamicDatagram for Synchronize {
88    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
89        Ok((
90            Box::<autd3_driver::operation::SyncOp>::default(),
91            Box::<autd3_driver::operation::NullOp>::default(),
92        ))
93    }
94
95    fn timeout(&self) -> Option<Duration> {
96        <Self as Datagram>::timeout(self)
97    }
98}
99
100impl DynamicDatagram for Stop {
101    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
102        Ok((
103            Box::new(<Self as Datagram>::O1::new(
104                SILENCER_STEP_DEFAULT,
105                SILENCER_STEP_DEFAULT,
106            )),
107            Box::<autd3_driver::operation::StopOp>::default(),
108        ))
109    }
110
111    fn timeout(&self) -> Option<Duration> {
112        <Self as Datagram>::timeout(self)
113    }
114}
115
116impl DynamicDatagram for Silencer {
117    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
118        Ok((
119            Box::new(<Self as Datagram>::O1::new(
120                self.step_intensity(),
121                self.step_phase(),
122            )),
123            Box::<autd3_driver::operation::NullOp>::default(),
124        ))
125    }
126
127    fn timeout(&self) -> Option<Duration> {
128        <Self as Datagram>::timeout(self)
129    }
130}
131
132impl DynamicDatagram for Clear {
133    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
134        Ok((
135            Box::<autd3_driver::operation::ClearOp>::default(),
136            Box::<autd3_driver::operation::NullOp>::default(),
137        ))
138    }
139
140    fn timeout(&self) -> Option<Duration> {
141        <Self as Datagram>::timeout(self)
142    }
143}
144
145impl DynamicDatagram for FocusSTM {
146    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
147        let freq_div = self.sampling_config().frequency_division();
148        Ok((
149            Box::new(<Self as Datagram>::O1::new(
150                self.clear(),
151                freq_div,
152                self.start_idx(),
153                self.finish_idx(),
154            )),
155            Box::<autd3_driver::operation::NullOp>::default(),
156        ))
157    }
158
159    fn timeout(&self) -> Option<Duration> {
160        <Self as Datagram>::timeout(self)
161    }
162}
163
164impl DynamicDatagram for GainSTM<Box<G>> {
165    fn operation(
166        &mut self,
167    ) -> Result<(Box<dyn Operation>, Box<dyn Operation>), autd3_driver::error::AUTDInternalError>
168    {
169        let freq_div = self.sampling_config().frequency_division();
170        Ok((
171            Box::new(<Self as Datagram>::O1::new(
172                self.clear(),
173                self.mode(),
174                freq_div,
175                self.start_idx(),
176                self.finish_idx(),
177            )),
178            Box::<autd3_driver::operation::NullOp>::default(),
179        ))
180    }
181
182    fn timeout(&self) -> Option<Duration> {
183        None
184    }
185}
186
187impl DynamicDatagram for Box<G> {
188    fn operation(
189        &mut self,
190    ) -> Result<(Box<dyn Operation>, Box<dyn Operation>), autd3_driver::error::AUTDInternalError>
191    {
192        let mut tmp: Box<G> = Box::<Null>::default();
193        std::mem::swap(&mut tmp, self);
194        Ok((
195            Box::new(autd3_driver::operation::GainOp::new(tmp)),
196            Box::<autd3_driver::operation::NullOp>::default(),
197        ))
198    }
199
200    fn timeout(&self) -> Option<Duration> {
201        None
202    }
203}
204
205impl DynamicDatagram for Box<M> {
206    fn operation(&mut self) -> Result<(Box<dyn Operation>, Box<dyn Operation>), AUTDInternalError> {
207        let freq_div = self.sampling_config().frequency_division();
208        let buf = self.calc()?;
209        Ok((
210            Box::new(autd3_driver::operation::ModulationOp::new(buf, freq_div)),
211            Box::<autd3_driver::operation::NullOp>::default(),
212        ))
213    }
214
215    fn timeout(&self) -> Option<Duration> {
216        None
217    }
218}