tof_control/tui/
rat_app.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
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
use crate::helper::{
    rb_type::{RBInfo, RBTemp, RBVcp, RBPh, RBMag},
    ltb_type::{LTBTemp, LTBThreshold},
    pb_type::{PBTemp, PBVcp},
    pa_type::{PATemp, PAReadBias},
};

pub struct App<'a> {
    pub title: &'a str,
    pub should_quit: bool,
    pub tabs: TabsState<'a>,
    pub reload: bool,
    pub rb_data: RBData,
    pub ltb_data: LTBData,
    pub pb_data: PBData,
    pub pa_data: PAData,
}

impl<'a> App<'a> {
    pub fn new(title: &'a str) -> App<'a> {
        App {
            title,
            should_quit: false,
            tabs: TabsState::new(Self::define_tabs()),
            reload: false,
            rb_data: RBData::new(),
            ltb_data: LTBData::new(),
            pb_data: PBData::new(),
            pa_data: PAData::new(),
        }
    }

    fn define_tabs() -> Vec<&'a str> {
        let mut tabs: Vec<&str> = Default::default();

        match RBInfo::read_sub_board().unwrap_or(0) {
            0 => tabs = vec!["RB"],
            1 => tabs = vec!["RB", "LTB"],
            2 => tabs = vec!["RB", "PB", "PA"],
            _ => (),
        };

        tabs
    }

    pub fn reload_data(&mut self) {
        if self.reload {
            self.rb_data = RBData::new();
            self.ltb_data = LTBData::new();
            self.pb_data = PBData::new();
            self.pa_data = PAData::new();
        }
    }
}

pub struct TabsState<'a> {
    pub titles: Vec<&'a str>,
    pub index: usize,
}

impl<'a> TabsState<'a> {
    pub fn new(titles: Vec<&'a str>) -> TabsState {
        TabsState {
            titles,
            index: 0,
        }
    }

    pub fn next(&mut self) {
        self.index = (self.index + 1) % self.titles.len();
    }

    pub fn previous(&mut self) {
        if self.index > 0 {
            self.index -= 1;
        } else {
            self.index = self.titles.len() - 1;
        }
    }
}

pub struct RBData {
    pub info: RBInfo,
    pub temp: RBTemp,
    pub vcp: RBVcp,
    pub ph: RBPh,
    pub mag: RBMag,
}

impl RBData {
    pub fn new() -> RBData {
        RBData {
            info: RBInfo::new(),
            temp: RBTemp::new(),
            vcp: RBVcp::new(),
            ph: RBPh::new(),
            mag: RBMag::new(),
        }
    }
}

pub struct LTBData {
    pub temp: LTBTemp,
    pub threshold: LTBThreshold,
}

impl LTBData {
    pub fn new() -> LTBData {
        LTBData {
            temp: LTBTemp::new(),
            threshold: LTBThreshold::new(),
        }
    }
}

pub struct PBData {
    pub temp: PBTemp,
    pub vcp: PBVcp,
}

impl PBData {
    pub fn new() -> PBData {
        PBData {
            temp: PBTemp::new(),
            vcp: PBVcp::new(),
        }
    }
}

pub struct PAData {
    pub temp: PATemp,
    pub bias: PAReadBias,
}

impl PAData {
    pub fn new() -> PAData {
        PAData {
            temp: PATemp::new(),
            bias: PAReadBias::new(),
        }
    }
}