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

use autd3_derive::Modulation;

use autd3_driver::derive::prelude::*;

/// Without modulation
#[derive(Modulation, Clone, Copy)]
pub struct Static {
    amp: float,
    #[no_change]
    freq_div: u32,
}

impl Static {
    /// constructor
    pub fn new() -> Self {
        Self {
            amp: 1.0,
            freq_div: 5120,
        }
    }

    /// set amplitude
    ///
    /// # Arguments
    ///
    /// * `amp` - normalized amplitude of the ultrasound (from 0 to 1)
    ///
    pub fn with_amp(self, amp: float) -> Self {
        Self { amp, ..self }
    }

    pub fn amp(&self) -> float {
        self.amp
    }
}

impl Modulation for Static {
    fn calc(&self) -> Result<Vec<float>, AUTDInternalError> {
        Ok(vec![self.amp; 2])
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_static_default() {
        let m = Static::default();
        assert_approx_eq::assert_approx_eq!(m.amp, 1.0);
        assert_eq!(m.calc().unwrap(), vec![1.0, 1.0]);
    }

    #[test]
    fn test_static_new() {
        let m = Static::new();
        assert_approx_eq::assert_approx_eq!(m.amp, 1.0);
        assert_eq!(m.calc().unwrap(), vec![1.0, 1.0]);
    }

    #[test]
    fn test_static_with_amp() {
        let m = Static::new().with_amp(0.5);
        assert_approx_eq::assert_approx_eq!(m.amp, 0.5);
        assert_eq!(m.calc().unwrap(), vec![0.5, 0.5]);
    }
}