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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * File: debug.rs
 * Project: link
 * Created Date: 10/05/2023
 * Author: Shun Suzuki
 * -----
 * Last Modified: 18/07/2023
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2023 Shun Suzuki. All rights reserved.
 *
 */

use std::time::Duration;

use autd3_core::{
    error::AUTDInternalError,
    geometry::{Geometry, Transducer},
    link::{get_logger, Link},
    CPUControlFlags, RxDatagram, TxDatagram, FPGA_SUB_CLK_FREQ, FPGA_SUB_CLK_FREQ_DIV, MSG_CLEAR,
    MSG_RD_CPU_VERSION, MSG_RD_CPU_VERSION_MINOR, MSG_RD_FPGA_FUNCTION, MSG_RD_FPGA_VERSION,
    MSG_RD_FPGA_VERSION_MINOR,
};
use autd3_firmware_emulator::CPUEmulator;

use spdlog::prelude::*;

/// Link to debug
pub struct Debug {
    is_open: bool,
    timeout: Duration,
    logger: Logger,
    cpus: Vec<CPUEmulator>,
}

impl Debug {
    pub fn new() -> Self {
        let logger = get_logger();
        logger.set_level_filter(LevelFilter::MoreSevereEqual(Level::Debug));
        Self {
            is_open: false,
            timeout: Duration::ZERO,
            logger,
            cpus: Vec::new(),
        }
    }

    /// set timeout
    pub fn with_timeout(self, timeout: Duration) -> Self {
        Self { timeout, ..self }
    }

    /// set log level
    pub fn with_log_level(self, level: LevelFilter) -> Self {
        self.logger.set_level_filter(level);
        self
    }

    /// set logger
    ///
    /// By default, the logger will display log messages on the console.
    pub fn with_logger(self, logger: Logger) -> Self {
        Self { logger, ..self }
    }

    pub fn emulators(&self) -> &[CPUEmulator] {
        &self.cpus
    }
}

impl Default for Debug {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Transducer> Link<T> for Debug {
    fn open(&mut self, geometry: &Geometry<T>) -> Result<(), AUTDInternalError> {
        debug!(logger: self.logger,"Open Debug link");

        if self.is_open {
            warn!(logger: self.logger,"Debug link is already opened.");
            return Ok(());
        }

        self.cpus = geometry
            .device_map()
            .iter()
            .enumerate()
            .map(|(i, &dev)| {
                let mut cpu = CPUEmulator::new(i, dev);
                cpu.init();
                cpu
            })
            .collect();
        trace!(logger: self.logger,"Initialize emulator");

        self.is_open = true;

        Ok(())
    }

    fn close(&mut self) -> Result<(), AUTDInternalError> {
        debug!(logger: self.logger,"Close Debug link");

        if !self.is_open {
            warn!(logger: self.logger,"Debug link is already closed.");
            return Ok(());
        }

        self.is_open = false;
        Ok(())
    }

    fn send(&mut self, tx: &TxDatagram) -> Result<bool, AUTDInternalError> {
        debug!(logger: self.logger, "Send data");

        if !self.is_open {
            warn!(logger: self.logger, "Link is not opened");
            return Ok(false);
        }

        self.cpus.iter_mut().for_each(|cpu| {
            cpu.send(tx);
        });

        match tx.header().msg_id {
            MSG_CLEAR => {
                debug!(logger: self.logger,"\tOP: CLEAR");
            }
            MSG_RD_CPU_VERSION => {
                debug!(logger: self.logger,"\tOP: RD_CPU_VERSION");
            }
            MSG_RD_CPU_VERSION_MINOR => {
                debug!(logger: self.logger,"\tOP: RD_CPU_VERSION_MINOR");
            }
            MSG_RD_FPGA_VERSION => {
                debug!(logger: self.logger,"\tOP: RD_FPGA_VERSION");
            }
            MSG_RD_FPGA_VERSION_MINOR => {
                debug!(logger: self.logger,"\tOP: RD_FPGA_VERSION_MINOR");
            }
            MSG_RD_FPGA_FUNCTION => {
                debug!(logger: self.logger,"\tOP: RD_FPGA_FUNCTION");
            }
            _ => {}
        }

        debug!(logger: self.logger,"\tCPU Flag: {}", tx.header().cpu_flag);
        debug!(logger: self.logger,"\tFPGA Flag: {}", tx.header().fpga_flag);

        self.cpus.iter().for_each(|cpu| {
            debug!(logger: self.logger,"Status: {}", cpu.id());
            let fpga = cpu.fpga();
            if fpga.is_stm_mode() {
                if fpga.is_stm_gain_mode() {
                    if fpga.is_legacy_mode() {
                        debug!(logger: self.logger,"\tGain STM Legacy mode");
                    } else {
                        debug!(logger: self.logger,"\tGain STM mode");
                    }
                } else {
                    debug!(logger: self.logger,"\tFocus STM mode"); 
                }
                if tx.header().cpu_flag.contains(CPUControlFlags::STM_BEGIN) {
                    debug!(logger: self.logger,"\t\tSTM BEGIN");
                }
                if tx.header().cpu_flag.contains(CPUControlFlags::STM_END) {
                    let freq_div_stm = fpga.stm_frequency_division() as usize / FPGA_SUB_CLK_FREQ_DIV;
                    debug!(logger: self.logger,
                        "\t\tSTM END: cycle = {}, sampling_frequency = {} ({}/{}))",
                        fpga.stm_cycle(),
                        FPGA_SUB_CLK_FREQ / freq_div_stm,
                        FPGA_SUB_CLK_FREQ,
                        freq_div_stm
                    );
                    if self.logger.should_log(Level::Trace) {
                        let cycles = fpga.cycles();
                        ( 0..fpga.stm_cycle()).for_each(|j| {
                            trace!(logger: self.logger,"\tSTM[{}]:", j);
                            trace!(logger: self.logger,
                                "{}",
                                fpga.duties_and_phases(j).iter()
                                    .zip(cycles.iter())
                                    .enumerate()
                                    .map(|(i, (d, c))| {
                                        format!("\n\t\t{:<3}: duty = {:<4}, phase = {:<4}, cycle = {:<4}", i, d.0, d.1, c)
                                    })
                                    .collect::<Vec<_>>()
                                    .join("")
                            );
                        });
                    }
                }
            } else if fpga.is_legacy_mode() {
                debug!(logger: self.logger,"\tNormal Legacy mode");
            } else {
                debug!(logger: self.logger,"\tNormal Advanced mode");
            }
            debug!(logger: self.logger,
                "\tSilencer step = {}",
                fpga.silencer_step(),
            );
            let m = fpga.modulation();
            let freq_div_m = fpga.modulation_frequency_division() as usize / FPGA_SUB_CLK_FREQ_DIV;
            debug!(logger: self.logger,
                "\tModulation size = {}, sampling_frequency = {} ({}/{})",
                m.len(),
                FPGA_SUB_CLK_FREQ / freq_div_m,
                FPGA_SUB_CLK_FREQ,
                freq_div_m
            );
            if fpga.is_outputting() {
                debug!(logger: self.logger,"\t\t modulation = {:?}", m);
                if !fpga.is_stm_mode() && self.logger.should_log(Level::Trace) {
                    trace!(logger: self.logger,
                        "{}", 
                        fpga.duties_and_phases(0).iter()
                            .zip(fpga.cycles().iter())
                            .enumerate()
                            .map(|(i, (d, c))| {
                                format!("\n\t\t{:<3}: duty = {:<4}, phase = {:<4}, cycle = {:<4}", i, d.0, d.1, c)
                            })
                            .collect::<Vec<_>>()
                            .join("")
                    );
                }
            } else {
                info!(logger: self.logger,"\tWithout output");
            }
        });

        Ok(true)
    }

    fn receive(&mut self, rx: &mut RxDatagram) -> Result<bool, AUTDInternalError> {
        debug!(logger: self.logger, "Receive data");

        if !self.is_open {
            warn!(logger: self.logger, "Link is not opened");
            return Ok(false);
        }

        self.cpus.iter_mut().for_each(|cpu| {
            rx[cpu.id()].ack = cpu.ack();
            rx[cpu.id()].msg_id = cpu.msg_id();
        });

        Ok(true)
    }

    fn is_open(&self) -> bool {
        self.is_open
    }

    fn timeout(&self) -> Duration {
        self.timeout
    }

    fn send_receive(
        &mut self,
        tx: &TxDatagram,
        rx: &mut RxDatagram,
        timeout: Duration,
    ) -> Result<bool, AUTDInternalError> {
        debug!(logger: self.logger, "Timeout: {:?}", timeout);
        if !<Self as Link<T>>::send(self, tx)? {
            return Ok(false);
        }
        if timeout.is_zero() {
            return <Self as Link<T>>::receive(self, rx);
        }
        <Self as Link<T>>::wait_msg_processed(self, tx.header().msg_id, rx, timeout)
    }

    fn wait_msg_processed(
        &mut self,
        msg_id: u8,
        rx: &mut RxDatagram,
        timeout: Duration,
    ) -> Result<bool, AUTDInternalError> {
        let start = std::time::Instant::now();
        loop {
            std::thread::sleep(std::time::Duration::from_millis(1));
            if <Self as Link<T>>::receive(self, rx)? && rx.is_msg_processed(msg_id) {
                return Ok(true);
            }
            if start.elapsed() > timeout {
                return Ok(false);
            }
        }
    }
}