B15F Board 15 Famulus Edition

view_monitor.cpp
1 #include "view_monitor.h"
2
3 ViewMonitor::ViewMonitor() : t_worker(&ViewMonitor::worker, this)
4 {
5 }
6
7 call_t ViewMonitor::keypress(int& key)
8 {
9 switch(key)
10 {
11
12 case KEY_MOUSE:
13 {
14 // http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
15 MEVENT event;
16 bool hit = false;
17 if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
18 {
19 size_t column = start_x + close_offset_x;
20 size_t row = start_y + close_offset_y;
21 size_t mouse_x = event.x, mouse_y = event.y;
22 if(mouse_y == row && mouse_x >= column && mouse_x < column + label_close.length())
23 hit = true;
24 }
25 if(!hit)
26 break;
27
28 // fall through to next case
29 [[fallthrough]];
30 }
31 case KEY_ENT:
32 run_worker = false;
33 key = -1; // do return from view
34 wclear(win);
35 wrefresh(win);
36 t_worker.join();
37 break;
38 default:
39 break;
40 }
41 return calls[0];
42 }
43
44 std::string ViewMonitor::fancyDigitalString(uint8_t& b)
45 {
46 std::string bitstring(std::bitset<8>(b).to_string());
47 std::reverse(bitstring.begin(), bitstring.end());
48
49 std::stringstream str;
50 str << bitstring;
51 str << " ";
52 str << "0x" << std::setfill ('0') << std::setw(2) << std::hex << (int) b << std::dec;
53 return str.str();
54 }
55
56 std::string ViewMonitor::fancyAnalogString(uint16_t& v)
57 {
58 std::stringstream str;
59 double volt = round(v * 100.0 * 5.0 / 1023.0) / 100.0;
60
61 str << std::setfill ('0') << std::setw(4) << (int) v << " " << std::fixed << std::setprecision(2) << volt << " V ";
62
63 str << "[";
64 uint8_t p = round(v * 40.0 / 1023.0);
65 for(uint8_t i = 0; i < p; i++)
66 str << "X";
67 for(uint8_t i = 0; i < 40 - p; i++)
68 str << " ";
69 str << "]" << std::endl;
70
71 return str.str();
72 }
73
74 void ViewMonitor::worker()
75 {
76 B15F& drv = B15F::getInstance();
77 while(run_worker)
78 {
79 try
80 {
81 std::this_thread::sleep_for(std::chrono::milliseconds(100));
82
83 uint8_t be0 = drv.digitalRead0();
84 uint8_t be1 = drv.digitalRead1();
85 uint8_t dsw = drv.readDipSwitch();
86 uint16_t adc[8];
87 for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
88 adc[i] = drv.analogRead(i);
89
90
91 std::stringstream str;
92
93 // hline
94 for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
95 if(i % 2 == 0)
96 str << "-";
97 else
98 str << " ";
99 str << std::endl;
100
101 str << "Digitale Enigaenge:" << std::endl;
102 str << "Binaere Eingabe 0: " << fancyDigitalString(be0) << std::endl;
103 str << "Binaere Eingabe 1: " << fancyDigitalString(be1) << std::endl;
104 str << "Dip Schalter (S7): " << fancyDigitalString(dsw) << std::endl;
105
106 // hline
107 for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
108 if(i % 2 == 0)
109 str << "-";
110 else
111 str << " ";
112 str << std::endl;
113
114 str << "Analoge Eingaenge:" << std::endl;
115 for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
116 {
117 str << "Kanal " << std::to_string((int) i) << ": ";
118 str << fancyAnalogString(adc[i]) << std::endl;
119 }
120
121 text = str.str();
122 repaint();
123 }
124 catch(DriverException& ex)
125 {
126 std::cout << "DriverException: " << ex.what() << std::endl;
127 drv.delay_ms(1000);
128 }
129 catch(...)
130 {
131 try
132 {
133 drv.reconnect();
134 }
135 catch(...)
136 {
137 B15F::abort("Die Verbindung ist unterbrochen worden. Wurde ein Stecker gezogen? :D");
138 return;
139 }
140 }
141 }
142 }
ViewMonitor
Definition: view_monitor.h:13
B15F::digitalRead0
uint8_t digitalRead0(void)
Definition: b15f.cpp:245
B15F::readDipSwitch
uint8_t readDipSwitch(void)
Definition: b15f.cpp:277
B15F::delay_ms
void delay_ms(uint16_t ms)
Definition: b15f.cpp:135
B15F::getInstance
static B15F & getInstance(void)
Definition: b15f.cpp:10
B15F
Definition: b15f.h:38
B15F::abort
static void abort(std::string msg)
Definition: b15f.cpp:169
B15F::analogRead
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:330
B15F::digitalRead1
uint8_t digitalRead1(void)
Definition: b15f.cpp:261
B15F::reconnect
void reconnect(void)
Definition: b15f.cpp:18
DriverException
Definition: driverexception.h:10