1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use crate::{
geometry::{Geometry, Transducer, Vector3},
interface::{DatagramBody, Empty, Filled, Sendable},
};
use anyhow::{Ok, Result};
use autd3_driver::{
SeqFocus, TxDatagram, FPGA_CLK_FREQ, POINT_STM_BODY_DATA_SIZE, POINT_STM_HEAD_DATA_SIZE,
STM_SAMPLING_FREQ_DIV_MIN,
};
use super::STM;
pub struct PointSTM {
control_points: Vec<(Vector3, u8)>,
sample_freq_div: u32,
sent: usize,
}
impl PointSTM {
pub fn new() -> Self {
Self::with_control_points(vec![])
}
pub fn with_control_points(control_points: Vec<(Vector3, u8)>) -> Self {
Self {
control_points,
sample_freq_div: 4096,
sent: 0,
}
}
pub fn add(&mut self, point: Vector3, duty_shift: u8) -> Result<()> {
if self.control_points.len() + 1 > autd3_driver::POINT_STM_BUF_SIZE_MAX {
return Err(autd3_driver::FPGAError::PointSTMOutOfBuffer(
self.control_points.len() + 1,
)
.into());
}
self.control_points.push((point, duty_shift));
Ok(())
}
pub fn size(&self) -> usize {
self.control_points.len()
}
pub fn control_points(&self) -> &[(Vector3, u8)] {
&self.control_points
}
}
impl Default for PointSTM {
fn default() -> Self {
Self::new()
}
}
impl<T: Transducer> DatagramBody<T> for PointSTM {
fn init(&mut self) -> Result<()> {
self.sent = 0;
Ok(())
}
fn pack(&mut self, geometry: &Geometry<T>, tx: &mut TxDatagram) -> Result<()> {
autd3_driver::point_stm_head(tx);
if DatagramBody::<T>::is_finished(self) {
return Ok(());
}
let is_first_frame = self.sent == 0;
let max_size = if is_first_frame {
POINT_STM_HEAD_DATA_SIZE
} else {
POINT_STM_BODY_DATA_SIZE
};
let send_size = (self.control_points.len() - self.sent).min(max_size);
let is_last_frame = self.sent + send_size == self.control_points.len();
let points: Vec<Vec<_>> = geometry
.devices()
.iter()
.map(|dev| {
self.control_points()[self.sent..(self.sent + send_size)]
.iter()
.map(|(p, d)| {
let lp = dev.local_position(p);
SeqFocus::new(lp.x, lp.y, lp.z, *d)
})
.collect()
})
.collect();
autd3_driver::point_stm_body(
&points,
is_first_frame,
self.sample_freq_div,
geometry.sound_speed(),
is_last_frame,
tx,
)?;
self.sent += send_size;
Ok(())
}
fn is_finished(&self) -> bool {
self.sent == self.control_points.len()
}
}
impl<T: Transducer> Sendable<T> for PointSTM {
type H = Empty;
type B = Filled;
fn init(&mut self) -> Result<()> {
DatagramBody::<T>::init(self)
}
fn pack(&mut self, _msg_id: u8, geometry: &Geometry<T>, tx: &mut TxDatagram) -> Result<()> {
DatagramBody::<T>::pack(self, geometry, tx)
}
fn is_finished(&self) -> bool {
DatagramBody::<T>::is_finished(self)
}
}
impl STM for PointSTM {
fn set_freq(&mut self, freq: f64) -> f64 {
let sample_freq = self.size() as f64 * freq;
let div = ((FPGA_CLK_FREQ as f64 / sample_freq) as u32)
.clamp(STM_SAMPLING_FREQ_DIV_MIN, u32::MAX);
self.sample_freq_div = div;
STM::freq(self)
}
fn freq(&self) -> f64 {
STM::sampling_freq(self) / self.size() as f64
}
fn sampling_freq(&self) -> f64 {
FPGA_CLK_FREQ as f64 / self.sample_freq_div as f64
}
fn set_sampling_freq_div(&mut self, freq_div: u32) {
self.sample_freq_div = freq_div;
}
fn sampling_freq_div(&mut self) -> u32 {
self.sample_freq_div
}
}