autd3_core/defined/
mod.rs

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
mod angle;
mod freq;

pub use std::f32::consts::PI;

#[cfg(feature = "use_meter")]
mod unit {
    /// meter
    pub const METER: f32 = 1.0;
}
#[cfg(not(feature = "use_meter"))]
mod unit {
    /// meter
    pub const METER: f32 = 1000.0;
}
pub use unit::*;

pub use angle::*;
pub use freq::*;

/// millimeter
pub const MILLIMETER: f32 = METER / 1000.0;

/// The absolute threshold of hearing in \[㎩\]
pub const ABSOLUTE_THRESHOLD_OF_HEARING: f32 = 20e-6;

/// The amplitude of T4010A1 in \[㎩*mm\]
pub const T4010A1_AMPLITUDE: f32 = 275.574_25 * 200.0 * MILLIMETER; // [㎩*mm]

/// The default timeout duration
pub const DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(200);

/// The period of ultrasound in discrete time units
pub const ULTRASOUND_PERIOD_COUNT: usize = 256;

#[cfg(not(feature = "dynamic_freq"))]
mod inner {
    use super::Freq;
    use std::time::Duration;

    #[inline(always)]
    /// The frequency of ultrasound
    pub const fn ultrasound_freq() -> Freq<u32> {
        Freq { freq: 40000 }
    }

    #[inline(always)]
    /// The period of ultrasound
    pub const fn ultrasound_period() -> Duration {
        Duration::from_micros(25)
    }
}

#[cfg(feature = "dynamic_freq")]
mod inner {
    use std::sync::LazyLock;

    use super::Freq;
    use crate::defined::Hz;

    static LAZY_FREQ: LazyLock<Freq<u32>> =
        LazyLock::new(|| match std::env::var("AUTD3_ULTRASOUND_FREQ") {
            Ok(freq) => match freq.parse::<u32>() {
                Ok(freq) => {
                    tracing::info!("Set ultrasound frequency to {} Hz.", freq);
                    freq * Hz
                }
                Err(_) => {
                    tracing::error!(
                        "Invalid ultrasound frequency ({} Hz), fallback to 40 kHz.",
                        freq
                    );
                    Freq { freq: 40000 }
                }
            },
            Err(_) => {
                tracing::warn!(
                    "Environment variable AUTD3_ULTRASOUND_FREQ is not set, fallback to 40 kHz."
                );
                Freq { freq: 40000 }
            }
        });

    #[inline]
    /// The frequency of ultrasound
    pub fn ultrasound_freq() -> Freq<u32> {
        *LAZY_FREQ
    }

    #[doc(hidden)]
    pub const DRP_ROM_SIZE: usize = 32;
}

pub use inner::*;

/// \[㎜\]
#[allow(non_upper_case_globals)]
pub const mm: f32 = MILLIMETER;