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()); nicht mehr notwendig, B15F invertiert automatisch den port
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 
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::keypress
virtual call_t keypress(int &key) override
Definition: view_monitor.cpp:7
ViewMonitor
Definition: view_monitor.h:15
ViewInfo::text_offset_x
constexpr static int text_offset_x
Relativer Abstand des Textes zum linken Rahmen.
Definition: view_info.h:55
View::width
int width
width of view in terminal characters
Definition: view.h:79
B15F::digitalRead0
uint8_t digitalRead0(void)
Definition: b15f.cpp:255
DriverException::what
virtual const char * what() const
Definition: driverexception.h:40
B15F::readDipSwitch
uint8_t readDipSwitch(void)
Definition: b15f.cpp:293
ViewMonitor::ViewMonitor
ViewMonitor(void)
Definition: view_monitor.cpp:3
B15F::delay_ms
void delay_ms(uint16_t ms)
Definition: b15f.cpp:141
View::calls
std::vector< call_t > calls
calls (function pointers) for different button actions in the view (if any)
Definition: view.h:84
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:175
View::win
static WINDOW * win
static window contexts for all views
Definition: view.h:86
ViewInfo::close_offset_x
int close_offset_x
Relative X Koordinate des Buttons zum Schließen im Window.
Definition: view_info.h:53
ViewInfo::label_close
std::string label_close
Beschriftung für Button zum Schließen.
Definition: view_info.h:52
B15F::analogRead
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:346
View::repaint
virtual void repaint(void)
Definition: view.cpp:52
ViewMonitor::run_worker
volatile bool run_worker
Legt fest, ob die Schleife im worker() weiterhin fortgeführt werden soll. Bei false bricht die Schlei...
Definition: view_monitor.h:47
ViewMonitor::worker
virtual void worker(void)
Funktion, die vom Worker-Thread ausgeführt, um die Anzeige zu aktualisieren.
Definition: view_monitor.cpp:74
ViewInfo::close_offset_y
int close_offset_y
Relative Y Koordinate des Buttons zum Schließen im Window.
Definition: view_info.h:54
B15F::digitalRead1
uint8_t digitalRead1(void)
Definition: b15f.cpp:274
B15F::reconnect
void reconnect(void)
Definition: b15f.cpp:18
View::start_x
int start_x
x offset (characters) in the terminal, used to center the window on repaint()
Definition: view.h:81
ViewInfo::text
std::string text
Benachrichtigungstext dieser View.
Definition: view_info.h:51
View::start_y
int start_y
y offset (characters) in the terminal, used to center the window on repaint()
Definition: view.h:82
ViewMonitor::t_worker
std::thread t_worker
Worker-Thread.
Definition: view_monitor.h:48
DriverException
Definition: driverexception.h:10
View::KEY_ENT
constexpr static int KEY_ENT
Key value for the Enter key.
Definition: view.h:87