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
/*
 * File: silencer_config.rs
 * Project: src
 * Created Date: 28/04/2022
 * Author: Shun Suzuki
 * -----
 * Last Modified: 31/05/2022
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2022 Shun Suzuki. All rights reserved.
 *
 */

use crate::{
    geometry::{Geometry, Transducer},
    interface::{DatagramHeader, Empty, Filled, Sendable},
};
use anyhow::Result;
use autd3_driver::TxDatagram;

pub struct SilencerConfig {
    pub(crate) step: u16,
    pub(crate) cycle: u16,
    sent: bool,
}

impl SilencerConfig {
    pub fn new(step: u16, cycle: u16) -> Self {
        SilencerConfig {
            step,
            cycle,
            sent: false,
        }
    }

    pub fn none() -> Self {
        Self::new(0xFFFF, 4096)
    }
}

impl DatagramHeader for SilencerConfig {
    fn init(&mut self) -> Result<()> {
        self.sent = false;
        Ok(())
    }

    fn pack(&mut self, msg_id: u8, tx: &mut TxDatagram) -> Result<()> {
        if self.sent {
            autd3_driver::null_header(msg_id, tx);
            Ok(())
        } else {
            self.sent = true;
            autd3_driver::config_silencer(msg_id, self.cycle, self.step, tx)
        }
    }

    fn is_finished(&self) -> bool {
        true
    }
}

impl<T: Transducer> Sendable<T> for SilencerConfig {
    type H = Filled;
    type B = Empty;

    fn init(&mut self) -> Result<()> {
        DatagramHeader::init(self)
    }

    fn pack(&mut self, msg_id: u8, _geometry: &Geometry<T>, tx: &mut TxDatagram) -> Result<()> {
        DatagramHeader::pack(self, msg_id, tx)
    }

    fn is_finished(&self) -> bool {
        DatagramHeader::is_finished(self)
    }
}

impl Default for SilencerConfig {
    fn default() -> Self {
        Self::new(10, 4096)
    }
}