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::stringstream str;
47  str << std::bitset<8>(b).to_string();
48  str << " ";
49  str << "0x" << std::setfill ('0') << std::setw(2) << std::hex << (int) b << std::dec;
50  return str.str();
51 }
52 
53 std::string ViewMonitor::fancyAnalogString(uint16_t& v)
54 {
55  std::stringstream str;
56  double volt = round(v * 100.0 * 5.0 / 1023.0) / 100.0;
57 
58  str << std::setfill ('0') << std::setw(4) << (int) v << " " << std::fixed << std::setprecision(2) << volt << " V ";
59 
60  str << "[";
61  uint8_t p = round(v * 40.0 / 1023.0);
62  for(uint8_t i = 0; i < p; i++)
63  str << "X";
64  for(uint8_t i = 0; i < 40 - p; i++)
65  str << " ";
66  str << "]" << std::endl;
67 
68  return str.str();
69 }
70 
71 void ViewMonitor::worker()
72 {
73  B15F& drv = B15F::getInstance();
74  while(run_worker)
75  {
76  try
77  {
78  std::this_thread::sleep_for(std::chrono::milliseconds(100));
79 
80  uint8_t be0 = drv.digitalRead0();
81  uint8_t be1 = drv.digitalRead1();
82  uint8_t dsw = drv.readDipSwitch();
83  uint16_t adc[8];
84  for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
85  adc[i] = drv.analogRead(i);
86 
87 
88  std::stringstream str;
89 
90  // hline
91  for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
92  if(i % 2 == 0)
93  str << "-";
94  else
95  str << " ";
96  str << std::endl;
97 
98  str << "Digitale Enigaenge:" << std::endl;
99  str << "Binaere Eingabe 0: " << fancyDigitalString(be0) << std::endl;
100  str << "Binaere Eingabe 1: " << fancyDigitalString(be1) << std::endl;
101  str << "Dip Schalter (S7): " << fancyDigitalString(dsw) << std::endl;
102 
103  // hline
104  for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
105  if(i % 2 == 0)
106  str << "-";
107  else
108  str << " ";
109  str << std::endl;
110 
111  str << "Analoge Eingaenge:" << std::endl;
112  for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
113  {
114  str << "Kanal " << std::to_string((int) i) << ": ";
115  str << fancyAnalogString(adc[i]) << std::endl;
116  }
117 
118  text = str.str();
119  repaint();
120  }
121  catch(DriverException& ex)
122  {
123  std::cout << "DriverException: " << ex.what() << std::endl;
124  drv.delay_ms(1000);
125  }
126  catch(...)
127  {
128  try
129  {
130  drv.reconnect();
131  }
132  catch(...)
133  {
134  B15F::abort("yoho meine dudes");
135  return;
136  }
137  }
138  }
139 }
ViewMonitor
Definition: view_monitor.h:13
B15F::digitalRead0
uint8_t digitalRead0(void)
Definition: b15f.cpp:207
B15F::readDipSwitch
uint8_t readDipSwitch(void)
Definition: b15f.cpp:235
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::abort
static void abort(std::string msg)
Definition: b15f.cpp:467
B15F::analogRead
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:279
B15F::digitalRead1
uint8_t digitalRead1(void)
Definition: b15f.cpp:221
B15F::reconnect
void reconnect(void)
Definition: b15f.cpp:57
DriverException
Definition: driverexception.h:10