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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::{
fpga::{Duty, LegacyDrive, Phase},
Drive, FOCUS_STM_FIXED_NUM_UNIT,
};
#[repr(C)]
pub struct STMFocus {
pub(crate) buf: [u16; 4],
}
impl STMFocus {
pub fn new(x: f64, y: f64, z: f64, duty_shift: u8) -> Self {
let x = (x / FOCUS_STM_FIXED_NUM_UNIT).round() as i32;
let y = (y / FOCUS_STM_FIXED_NUM_UNIT).round() as i32;
let z = (z / FOCUS_STM_FIXED_NUM_UNIT).round() as i32;
let d0 = (x & 0xFFFF) as u16;
let d1 =
((y << 2) & 0xFFFC) as u16 | ((x >> 30) & 0x0002) as u16 | ((x >> 16) & 0x0001) as u16;
let d2 =
((z << 4) & 0xFFF0) as u16 | ((y >> 28) & 0x0008) as u16 | ((y >> 14) & 0x0007) as u16;
let d3 = (((duty_shift as u16) << 6) & 0x3FC0)
| ((z >> 26) & 0x0020) as u16
| ((z >> 12) & 0x001F) as u16;
Self {
buf: [d0, d1, d2, d3],
}
}
}
pub const FOCUS_STM_BODY_INITIAL_SIZE: usize = 14;
#[repr(C)]
pub struct FocusSTMBodyInitial<T: ?Sized> {
data: T,
}
impl FocusSTMBodyInitial<[u16]> {
pub fn set_size(&mut self, size: u16) {
self.data[0] = size;
}
pub fn set_freq_div(&mut self, freq_div: u32) {
self.data[1] = (freq_div & 0x0000FFFF) as _;
self.data[2] = ((freq_div >> 16) & 0x0000FFFF) as _;
}
pub fn set_sound_speed(&mut self, sound_speed: u32) {
self.data[3] = (sound_speed & 0x0000FFFF) as _;
self.data[4] = ((sound_speed >> 16) & 0x0000FFFF) as _;
}
pub fn set_start_idx(&mut self, idx: u16) {
self.data[5] = idx;
}
pub fn set_finish_idx(&mut self, idx: u16) {
self.data[6] = idx;
}
pub fn set_points(&mut self, points: &[STMFocus]) {
self.data[7..]
.chunks_mut(std::mem::size_of::<STMFocus>() / std::mem::size_of::<u16>())
.zip(points.iter())
.for_each(|(d, s)| d.copy_from_slice(&s.buf));
}
}
pub const FOCUS_STM_BODY_SUBSEQUENT_SIZE: usize = 2;
#[repr(C)]
pub struct FocusSTMBodySubsequent<T: ?Sized> {
data: T,
}
impl FocusSTMBodySubsequent<[u16]> {
pub fn data(&self) -> &[u16] {
&self.data
}
pub fn set_size(&mut self, size: u16) {
self.data[0] = size;
}
pub fn set_points(&mut self, points: &[STMFocus]) {
self.data[1..]
.chunks_mut(std::mem::size_of::<STMFocus>() / std::mem::size_of::<u16>())
.zip(points.iter())
.for_each(|(d, s)| d.copy_from_slice(&s.buf));
}
}
#[repr(u16)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Mode {
PhaseDutyFull = 0x0001,
PhaseFull = 0x0002,
PhaseHalf = 0x0004,
}
impl Default for Mode {
fn default() -> Self {
Mode::PhaseDutyFull
}
}
#[repr(C)]
pub struct GainSTMBodyInitial<T: ?Sized> {
data: T,
}
impl GainSTMBodyInitial<[u16]> {
pub fn data(&self) -> &[u16] {
&self.data
}
pub fn set_freq_div(&mut self, freq_div: u32) {
self.data[0] = (freq_div & 0x0000FFFF) as _;
self.data[1] = ((freq_div >> 16) & 0x0000FFFF) as _;
}
pub fn set_mode(&mut self, mode: Mode) {
self.data[2] = mode as u16;
}
pub fn set_cycle(&mut self, cycle: usize) {
self.data[3] = cycle as u16;
}
pub fn set_start_idx(&mut self, idx: u16) {
self.data[4] = idx;
}
pub fn set_finish_idx(&mut self, idx: u16) {
self.data[5] = idx;
}
}
#[repr(C)]
pub struct GainSTMBodySubsequent<T: ?Sized> {
data: T,
}
#[repr(C)]
pub struct LegacyPhaseFull<const N: usize> {
phase_0: u8,
phase_1: u8,
}
impl LegacyPhaseFull<0> {
pub fn set(&mut self, d: &Drive) {
self.phase_0 = LegacyDrive::to_phase(d);
}
}
impl LegacyPhaseFull<1> {
pub fn set(&mut self, d: &Drive) {
self.phase_1 = LegacyDrive::to_phase(d);
}
}
#[repr(C)]
pub struct LegacyPhaseHalf<const N: usize> {
phase_01: u8,
phase_23: u8,
}
impl LegacyPhaseHalf<0> {
pub fn set(&mut self, d: &Drive) {
let phase = LegacyDrive::to_phase(d);
self.phase_01 = (self.phase_01 & 0xF0) | ((phase >> 4) & 0x0F);
}
}
impl LegacyPhaseHalf<1> {
pub fn set(&mut self, d: &Drive) {
let phase = LegacyDrive::to_phase(d);
self.phase_01 = (self.phase_01 & 0x0F) | (phase & 0xF0);
}
}
impl LegacyPhaseHalf<2> {
pub fn set(&mut self, d: &Drive) {
let phase = LegacyDrive::to_phase(d);
self.phase_23 = (self.phase_23 & 0xF0) | ((phase >> 4) & 0x0F);
}
}
impl LegacyPhaseHalf<3> {
pub fn set(&mut self, d: &Drive) {
let phase = LegacyDrive::to_phase(d);
self.phase_23 = (self.phase_23 & 0x0F) | (phase & 0xF0);
}
}
impl GainSTMBodySubsequent<[u16]> {
pub fn data(&self) -> &[u16] {
&self.data
}
pub fn legacy_drives_mut(&mut self) -> &mut [LegacyDrive] {
unsafe { std::mem::transmute(&mut self.data) }
}
pub fn phases_mut(&mut self) -> &mut [Phase] {
unsafe { std::mem::transmute(&mut self.data) }
}
pub fn duties_mut(&mut self) -> &mut [Duty] {
unsafe { std::mem::transmute(&mut self.data) }
}
pub fn legacy_phase_full_mut<const N: usize>(&mut self) -> &mut [LegacyPhaseFull<N>] {
unsafe { std::mem::transmute(&mut self.data) }
}
pub fn legacy_phase_half_mut<const N: usize>(&mut self) -> &mut [LegacyPhaseHalf<N>] {
unsafe { std::mem::transmute(&mut self.data) }
}
}
#[repr(C)]
pub struct Body<T: ?Sized> {
data: T,
}
impl Body<[u16]> {
pub fn data(&self) -> &[u16] {
&self.data
}
pub fn focus_stm_initial(&self) -> &FocusSTMBodyInitial<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn focus_stm_initial_mut(&mut self) -> &mut FocusSTMBodyInitial<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn focus_stm_subsequent(&self) -> &FocusSTMBodySubsequent<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn focus_stm_subsequent_mut(&mut self) -> &mut FocusSTMBodySubsequent<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn gain_stm_initial(&self) -> &GainSTMBodyInitial<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn gain_stm_initial_mut(&mut self) -> &mut GainSTMBodyInitial<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn gain_stm_subsequent(&self) -> &GainSTMBodySubsequent<[u16]> {
unsafe { std::mem::transmute(self) }
}
pub fn gain_stm_subsequent_mut(&mut self) -> &mut GainSTMBodySubsequent<[u16]> {
unsafe { std::mem::transmute(self) }
}
}