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
use crate::{
    error::AUTDInternalError,
    fpga::{FPGA_CLK_FREQ, SAMPLING_FREQ_DIV_MAX, SAMPLING_FREQ_DIV_MIN},
};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SamplingConfiguration {
    div: u32,
}

impl SamplingConfiguration {
    pub const BASE_FREQUENCY: f64 = FPGA_CLK_FREQ as _;

    pub const FREQ_MIN: f64 = Self::BASE_FREQUENCY / SAMPLING_FREQ_DIV_MAX as f64;
    pub const FREQ_MAX: f64 = Self::BASE_FREQUENCY / SAMPLING_FREQ_DIV_MIN as f64;
    pub const PERIOD_MIN: u128 =
        (1000000000. / Self::BASE_FREQUENCY * SAMPLING_FREQ_DIV_MIN as f64) as u128;
    pub const PERIOD_MAX: u128 = 209715199999;

    pub const DISABLE: Self = Self { div: 0xFFFFFFFF };
    pub const FREQ_4K_HZ: Self = Self { div: 5120 };

    pub fn from_frequency_division(div: u32) -> Result<Self, AUTDInternalError> {
        if !(SAMPLING_FREQ_DIV_MIN..=SAMPLING_FREQ_DIV_MAX).contains(&div) {
            Err(AUTDInternalError::SamplingFreqDivOutOfRange(
                div,
                SAMPLING_FREQ_DIV_MIN,
                SAMPLING_FREQ_DIV_MAX,
            ))
        } else {
            Ok(Self { div })
        }
    }

    pub fn from_frequency(f: f64) -> Result<Self, AUTDInternalError> {
        let div = (Self::BASE_FREQUENCY / f) as u64;
        if div > SAMPLING_FREQ_DIV_MAX as u64 {
            return Err(AUTDInternalError::SamplingFreqOutOfRange(
                f as _,
                Self::FREQ_MIN,
                Self::FREQ_MAX,
            ));
        }
        Self::from_frequency_division(div as _).map_err(|_| {
            AUTDInternalError::SamplingFreqOutOfRange(f as _, Self::FREQ_MIN, Self::FREQ_MAX)
        })
    }

    pub fn from_period(p: std::time::Duration) -> Result<Self, AUTDInternalError> {
        let p = p.as_nanos();
        let div = (Self::BASE_FREQUENCY * (p as f64 / 1000000000.)) as u64;
        if div > SAMPLING_FREQ_DIV_MAX as u64 {
            return Err(AUTDInternalError::SamplingPeriodOutOfRange(
                p,
                Self::PERIOD_MIN,
                Self::PERIOD_MAX,
            ));
        }
        Self::from_frequency_division(div as _).map_err(|_| {
            AUTDInternalError::SamplingPeriodOutOfRange(p, Self::PERIOD_MIN, Self::PERIOD_MAX)
        })
    }

    pub const fn frequency_division(&self) -> u32 {
        self.div
    }

    pub fn frequency(&self) -> f64 {
        (Self::BASE_FREQUENCY / self.div as f64) as _
    }

    pub fn period(&self) -> std::time::Duration {
        let p = 1000000000. / Self::BASE_FREQUENCY * self.div as f64;
        std::time::Duration::from_nanos(p as _)
    }
}

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

    #[rstest::rstest]
    #[test]
    #[case::min(
        Ok(SamplingConfiguration {
            div: SAMPLING_FREQ_DIV_MIN
        }),
        SAMPLING_FREQ_DIV_MIN
    )]
    #[case::max(
        Ok(SamplingConfiguration {
            div: SAMPLING_FREQ_DIV_MAX
        }),
        SAMPLING_FREQ_DIV_MAX
    )]
    #[case::out_of_range(
        Err(AUTDInternalError::SamplingFreqDivOutOfRange(
            SAMPLING_FREQ_DIV_MIN - 1,
            SAMPLING_FREQ_DIV_MIN,
            SAMPLING_FREQ_DIV_MAX
        )),
        SAMPLING_FREQ_DIV_MIN - 1
    )]
    fn test_from_frequency_division(
        #[case] expected: Result<SamplingConfiguration, AUTDInternalError>,
        #[case] freq_div: u32,
    ) {
        assert_eq!(
            expected,
            SamplingConfiguration::from_frequency_division(freq_div)
        );
    }

    #[rstest::rstest]
    #[test]
    #[case::min(
        Ok(SamplingConfiguration {
            div: SAMPLING_FREQ_DIV_MIN
        }),
        SamplingConfiguration::FREQ_MAX
    )]
    #[case::max(
        Ok(SamplingConfiguration {
            div: SAMPLING_FREQ_DIV_MAX
        }),
        SamplingConfiguration::FREQ_MIN
    )]
    #[case::out_of_range_min(
        Err(AUTDInternalError::SamplingFreqOutOfRange(
            SamplingConfiguration::FREQ_MIN - 0.1,
            SamplingConfiguration::FREQ_MIN,
            SamplingConfiguration::FREQ_MAX
        )),
        SamplingConfiguration::FREQ_MIN - 0.1
    )]
    #[case::out_of_range_max(
        Err(AUTDInternalError::SamplingFreqOutOfRange(
            SamplingConfiguration::FREQ_MAX + 0.1,
            SamplingConfiguration::FREQ_MIN,
            SamplingConfiguration::FREQ_MAX
        )),
        SamplingConfiguration::FREQ_MAX + 0.1
    )]
    fn test_from_frequency(
        #[case] expected: Result<SamplingConfiguration, AUTDInternalError>,
        #[case] freq: f64,
    ) {
        assert_eq!(expected, SamplingConfiguration::from_frequency(freq));
    }

    #[rstest::rstest]
    #[test]
    #[case::min(
        Ok(SamplingConfiguration {
            div: SAMPLING_FREQ_DIV_MIN
        }),
        SamplingConfiguration::PERIOD_MIN
    )]
    #[case::max(
        Ok(SamplingConfiguration {
            div: SAMPLING_FREQ_DIV_MAX
        }),
        SamplingConfiguration::PERIOD_MAX
    )]
    #[case::out_of_range_min(
        Err(AUTDInternalError::SamplingPeriodOutOfRange(
            SamplingConfiguration::PERIOD_MIN - 1,
            SamplingConfiguration::PERIOD_MIN,
            SamplingConfiguration::PERIOD_MAX
        )),
        SamplingConfiguration::PERIOD_MIN - 1
    )]
    #[case::out_of_range_max(
        Err(AUTDInternalError::SamplingPeriodOutOfRange(
            SamplingConfiguration::PERIOD_MAX + 1,
            SamplingConfiguration::PERIOD_MIN,
            SamplingConfiguration::PERIOD_MAX
        )),
        SamplingConfiguration::PERIOD_MAX + 1
    )]
    fn test_from_period(
        #[case] expected: Result<SamplingConfiguration, AUTDInternalError>,
        #[case] period: u128,
    ) {
        assert_eq!(
            expected,
            SamplingConfiguration::from_period(std::time::Duration::from_nanos(period as _))
        );
    }
}