autd3_driver/defined/
angle.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
/// \[°\]
#[allow(non_camel_case_types)]
pub struct deg;

/// \[rad\]
#[allow(non_camel_case_types)]
pub struct rad;

use derive_more::Debug;

/// Angle
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Angle {
    #[doc(hidden)]
    #[debug("{}°", _0)]
    Deg(f32),
    #[doc(hidden)]
    #[debug("{}rad", _0)]
    Rad(f32),
}

impl Angle {
    /// Returns the angle in radian
    pub fn radian(self) -> f32 {
        match self {
            Self::Deg(a) => a.to_radians(),
            Self::Rad(a) => a,
        }
    }

    /// Returns the angle in degree
    pub fn degree(self) -> f32 {
        match self {
            Self::Deg(a) => a,
            Self::Rad(a) => a.to_degrees(),
        }
    }
}

impl std::ops::Mul<deg> for f32 {
    type Output = Angle;

    fn mul(self, _rhs: deg) -> Self::Output {
        Self::Output::Deg(self)
    }
}

impl std::ops::Mul<rad> for f32 {
    type Output = Angle;

    fn mul(self, _rhs: rad) -> Self::Output {
        Self::Output::Rad(self)
    }
}

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

    #[test]
    fn dbg() {
        assert_eq!(format!("{:?}", 90.0 * deg), "90°");
        assert_eq!(format!("{:?}", 1.0 * rad), "1rad");
    }
}