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 
82 #ifdef __arm__
83  // Raspberry Pi is much slower
84  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
85 #else
86  // normal PC is faster
87  std::this_thread::sleep_for(std::chrono::milliseconds(100));
88 #endif
89 
90  uint8_t be0 = drv.digitalRead0();
91  uint8_t be1 = drv.digitalRead1();
92  uint8_t dsw = drv.readDipSwitch();
93  uint16_t adc[8];
94  for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
95  adc[i] = drv.analogRead(i);
96 
97 
98  std::stringstream str;
99 
100  // hline
101  for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
102  if(i % 2 == 0)
103  str << "-";
104  else
105  str << " ";
106  str << std::endl;
107 
108  str << "Digitale Enigaenge:" << std::endl;
109  str << "Binaere Eingabe 0: " << fancyDigitalString(be0) << std::endl;
110  str << "Binaere Eingabe 1: " << fancyDigitalString(be1) << std::endl;
111  str << "Dip Schalter (S7): " << fancyDigitalString(dsw) << std::endl;
112 
113  // hline
114  for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
115  if(i % 2 == 0)
116  str << "-";
117  else
118  str << " ";
119  str << std::endl;
120 
121  str << "Analoge Eingaenge:" << std::endl;
122  for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
123  {
124  str << "Kanal " << std::to_string((int) i) << ": ";
125  str << fancyAnalogString(adc[i]) << std::endl;
126  }
127 
128  text = str.str();
129  repaint();
130  }
131  catch(DriverException& ex)
132  {
133  std::cout << "DriverException: " << ex.what() << std::endl;
134  drv.delay_ms(1000);
135  }
136  catch(...)
137  {
138  try
139  {
140  drv.reconnect();
141  }
142  catch(...)
143  {
144  B15F::abort("Die Verbindung ist unterbrochen worden. Wurde ein Stecker gezogen? :D");
145  return;
146  }
147  }
148  }
149 }
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