B15F Board 15 Famulus Edition

ui.cpp
1 #include "ui.h"
2 #include "../drv/b15f.h"
3
4 std::vector<View*> win_stack;
5 std::thread t_refresh;
6
7 void show_main(int)
8 {
9 ViewSelection* view = new ViewSelection();
10 view->setTitle("B15F - Command Line Interface");
11 view->addChoice("[ Monitor - Eingaben beobachten ]", &show_monitor);
12 view->addChoice("[ Digitale Ausgabe BE0 ]", &show_digital_output0);
13 view->addChoice("[ Digitale Ausgabe BE1 ]", &show_digital_output1);
14 view->addChoice("[ Analoge Ausgabe AA0 ]", &show_analog_output0);
15 view->addChoice("[ Analoge Ausgabe AA1 ]", &show_analog_output1);
16 view->addChoice("[ Selbsttest des B15 ]", &show_selftest_info);
17 view->addChoice("[ Informationen ]", &show_info);
18 view->addChoice("", nullptr);
19 view->addChoice("[ Beenden ]", &finish);
20 view->repaint();
21
22 win_stack.push_back(view);
23 input(0);
24 }
25
26 void input(int)
27 {
28 call_t nextCall;
29 int key;
30 do
31 {
32 key = wgetch(View::getWinContext());
33 win_stack.back()->repaint();
34 nextCall = win_stack.back()->keypress(key);
35
36 if(key == -1)
37 view_back(key);
38
39 if(nextCall)
40 nextCall(key);
41 }
42 while(win_stack.size());
43 }
44
45 void view_back(int)
46 {
47 if(win_stack.size())
48 {
49 delete win_stack.back();
50 win_stack.pop_back();
51 }
52 if(win_stack.size())
53 win_stack.back()->repaint();
54 }
55
56 void finish(int)
57 {
58 cleanup();
59 exit(EXIT_SUCCESS);
60 }
61
62 void cleanup()
63 {
64 if(t_refresh.joinable())
65 t_refresh.join();
66 clrtoeol();
67 refresh();
68 endwin();
69 }
70
71 void show_info(int)
72 {
73 ViewInfo* view = new ViewInfo();
74 view->setTitle("Info");
75 view->setText("Informationen zu Board 15 Famulus Edition\nEs war einmal...");
76 view->setLabelClose("[ Zurueck ]");
77 view->repaint();
78
79 win_stack.push_back(view);
80 input(0);
81 }
82
83 void show_monitor(int)
84 {
85 ViewMonitor* view = new ViewMonitor();
86 view->setTitle("Monitor");
87 view->setText("\nErfasse Messwerte...");
88 view->setLabelClose("[ Zurueck ]");
89 view->repaint();
90
91 win_stack.push_back(view);
92 input(0);
93 }
94
95 void show_invalid_port_input(int)
96 {
97 ViewInfo* view = new ViewInfo();
98 view->setTitle("Falsche Eingabe");
99 view->setText("Bitte geben Sie einen Wert aus dem Intervall [0, FF] an.");
100 view->setLabelClose("[ Schliessen ]");
101 view->repaint();
102
103 win_stack.push_back(view);
104 input(0);
105 }
106
107 void show_invalid_dac_input(int)
108 {
109 ViewInfo* view = new ViewInfo();
110 view->setTitle("Falsche Eingabe");
111 view->setText("Bitte geben Sie einen Wert aus dem Intervall [0, 1023] an.");
112 view->setLabelClose("[ Schliessen ]");
113 view->repaint();
114
115 win_stack.push_back(view);
116 input(0);
117 }
118
119 void write_digital_output0(int)
120 {
121 try
122 {
123 int d = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput(), 0, 16);
124 if(d > 255 || 0 > d)
125 throw std::invalid_argument("bad value");
126 uint8_t port = static_cast<uint8_t>(d);
127
128 B15F& drv = B15F::getInstance();
129 drv.digitalWrite0(port);
130 view_back(0);
131 }
132 catch(std::invalid_argument& ex)
133 {
134 show_invalid_port_input(0);
135 }
136 }
137
138 void write_digital_output1(int)
139 {
140 try
141 {
142 int d = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput(), 0, 16);
143 if(d > 255 || 0 > d)
144 throw std::invalid_argument("bad value");
145 uint8_t port = static_cast<uint8_t>(d);
146
147 B15F& drv = B15F::getInstance();
148 drv.digitalWrite1(port);
149 view_back(0);
150 }
151 catch(std::invalid_argument& ex)
152 {
153 show_invalid_port_input(0);
154 }
155 }
156
157 void write_analog_output0(int)
158 {
159 try
160 {
161 uint16_t port = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput());
162 if(port > 1023)
163 throw std::invalid_argument("bad value");
164
165 B15F& drv = B15F::getInstance();
166 drv.analogWrite0(port);
167 view_back(0);
168 }
169 catch(std::invalid_argument& ex)
170 {
171 show_invalid_dac_input(0);
172 }
173 }
174
175 void write_analog_output1(int)
176 {
177 try
178 {
179 uint16_t port = std::stoi(static_cast<ViewPromt*>(win_stack.back())->getInput());
180 if(port > 1023)
181 throw std::invalid_argument("bad value");
182
183 B15F& drv = B15F::getInstance();
184 drv.analogWrite1(port);
185 view_back(0);
186 }
187 catch(std::invalid_argument& ex)
188 {
189 show_invalid_dac_input(0);
190 }
191 }
192
193 void show_digital_output0(int)
194 {
195 ViewPromt* view = new ViewPromt();
196 view->setTitle("Digitale Ausgabe BE0");
197 view->setMessage("\nAusgabe Port-Wert (hex): 0x");
198 view->setCancel("[ Zurueck ]", true);
199 view->setConfirm("[ OK ]", &write_digital_output0);
200 view->repaint();
201
202 win_stack.push_back(view);
203 input(0);
204 }
205
206 void show_digital_output1(int)
207 {
208 ViewPromt* view = new ViewPromt();
209 view->setTitle("Digitale Ausgabe BE1");
210 view->setMessage("\nAusgabe Port-Wert (hex): 0x");
211 view->setCancel("[ Zurueck ]", true);
212 view->setConfirm("[ OK ]", &write_digital_output1);
213 view->repaint();
214
215 win_stack.push_back(view);
216 input(0);
217 }
218
219 void show_analog_output0(int)
220 {
221 ViewPromt* view = new ViewPromt();
222 view->setTitle("Analoge Ausgabe AA0");
223 view->setMessage("\nAusgabe 10-Bit-Wert (0...1023): ");
224 view->setCancel("[ Zurueck ]", true);
225 view->setConfirm("[ OK ]", &write_analog_output0);
226 view->repaint();
227
228 win_stack.push_back(view);
229 input(0);
230 }
231
232 void show_analog_output1(int)
233 {
234 ViewPromt* view = new ViewPromt();
235 view->setTitle("Analoge Ausgabe AA1");
236 view->setMessage("\nAusgabe 10-Bit-Wert (0...1023): ");
237 view->setCancel("[ Zurueck ]", true);
238 view->setConfirm("[ OK ]", &write_analog_output1);
239 view->repaint();
240
241 win_stack.push_back(view);
242 input(0);
243 }
244
245 void start_selftest(int)
246 {
247 B15F& drv = B15F::getInstance();
248 drv.activateSelfTestMode();
249
250 ViewInfo* view = new ViewInfo();
251 view->setTitle("Selbsttest aktiv");
252 view->setText("Das B15 befindet sich jetzt im Selbsttestmodus.\n \nSelbsttest:\nZu Beginn geht der Reihe nach jede LED von BA0 bis BA1 an.\nDanach leuchten die LEDs an AA0 und AA1 kurz auf.\nZum Schluss spiegelt in einer Endlosschleife:\n* BA0 Port BE0\n* BA1 die DIP-Schalter S7\n* AA0 ADC0\n* AA1 ADC1");
253 view->setLabelClose("[ Selbsttest Beenden ]");
254 view->setCall(&stop_selftest);
255 view->repaint();
256
257 win_stack.push_back(view);
258 input(0);
259 }
260
261 void stop_selftest(int)
262 {
263 B15F& drv = B15F::getInstance();
264 drv.discard();
265 drv.delay_ms(B15F::WDT_TIMEOUT);
266 drv.reconnect();
267 drv.digitalWrite0(0);
268 drv.digitalWrite1(0);
269 }
270
271 void show_selftest_info(int)
272 {
273 ViewInfo* view = new ViewInfo();
274 view->setTitle("Selbsttest");
275 view->setText("Bitte entfernen Sie jetzt alle Draehte von den Anschlussklemmen und bestaetigen\nmit Enter.");
276 view->setLabelClose("[ Weiter ]");
277 view->setCall(&start_selftest);
278 view->repaint();
279
280 win_stack.push_back(view);
281 input(0);
282 }
ViewMonitor
Definition: view_monitor.h:13
ViewInfo
Definition: view_info.h:8
B15F::delay_ms
void delay_ms(uint16_t ms)
Definition: b15f.cpp:432
B15F::getInstance
static B15F & getInstance(void)
Definition: b15f.cpp:442
B15F
Definition: b15f.h:31
B15F::digitalWrite0
bool digitalWrite0(uint8_t)
Definition: b15f.cpp:179
B15F::activateSelfTestMode
bool activateSelfTestMode(void)
Definition: b15f.cpp:166
ViewSelection
Definition: view_selection.h:10
B15F::analogWrite1
bool analogWrite1(uint16_t port)
Definition: b15f.cpp:264
ViewPromt
Definition: view_promt.h:10
B15F::digitalWrite1
bool digitalWrite1(uint8_t)
Definition: b15f.cpp:193
B15F::discard
void discard(void)
Definition: b15f.cpp:72
B15F::WDT_TIMEOUT
constexpr static uint16_t WDT_TIMEOUT
Time in ms after which the watch dog timer resets the MCU.
Definition: b15f.h:241
B15F::reconnect
void reconnect(void)
Definition: b15f.cpp:57
B15F::analogWrite0
bool analogWrite0(uint16_t port)
Definition: b15f.cpp:249