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
use crate::common::Segment;

/// FPGA state
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FPGAState {
    pub(crate) state: u8,
}

impl FPGAState {
    /// Check if thermal sensor is asserted
    pub const fn is_thermal_assert(&self) -> bool {
        (self.state & (1 << 0)) != 0
    }

    /// Current mod segment
    pub const fn current_mod_segment(&self) -> Segment {
        match self.state & (1 << 1) {
            0 => Segment::S0,
            _ => Segment::S1,
        }
    }

    /// Current stm segment
    pub const fn current_stm_segment(&self) -> Option<Segment> {
        if !self.is_stm_mode() {
            return None;
        }
        match self.state & (1 << 2) {
            0 => Some(Segment::S0),
            _ => Some(Segment::S1),
        }
    }

    /// Current gain segment
    pub const fn current_gain_segment(&self) -> Option<Segment> {
        if !self.is_gain_mode() {
            return None;
        }
        match self.state & (1 << 2) {
            0 => Some(Segment::S0),
            _ => Some(Segment::S1),
        }
    }

    /// Check if gain mode
    pub const fn is_gain_mode(&self) -> bool {
        (self.state & (1 << 3)) != 0
    }

    /// Check if stm mode
    pub const fn is_stm_mode(&self) -> bool {
        !self.is_gain_mode()
    }

    pub const fn state(&self) -> u8 {
        self.state
    }
}

#[cfg(test)]
mod tests {
    use std::mem::size_of;

    use super::*;

    #[test]
    fn fpga_state() {
        assert_eq!(size_of::<FPGAState>(), 1);

        let info = FPGAState { state: 0x00 };
        assert!(!info.is_thermal_assert());
        assert_eq!(Segment::S0, info.current_mod_segment());
        assert_eq!(Some(Segment::S0), info.current_stm_segment());
        assert_eq!(None, info.current_gain_segment());
        assert_eq!(info.state(), 0x00);

        let info = FPGAState { state: 0x09 };
        assert!(info.is_thermal_assert());
        assert_eq!(None, info.current_stm_segment());
        assert_eq!(Some(Segment::S0), info.current_gain_segment());
        assert_eq!(info.state(), 0x09);
    }

    #[test]
    fn fpga_state_derive() {
        let info = FPGAState { state: 0x00 };
        let info2 = info;

        assert_eq!(info, info2);
        assert_eq!(format!("{:?}", info), "FPGAState { state: 0 }");
    }
}