Monitor geht

This commit is contained in:
Tristan Krause 2019-04-05 10:38:59 +02:00
parent 5a058dd18c
commit 00f0be902d
6 changed files with 135 additions and 45 deletions

View file

@ -13,14 +13,19 @@ std::function<void(int)> ViewMonitor::keypress(int& key)
{
// http://pronix.linuxdelta.de/C/Linuxprogrammierung/Linuxsystemprogrammieren_C_Kurs_Kapitel10b.shtml
MEVENT event;
bool hit = false;
if(getmouse(&event) == OK && event.bstate & (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED))
{
size_t column = start_x + close_offset_x;
size_t row = start_y + close_offset_y;
if(event.y == row && event.x >= column && event.x < column + label_close.length())
key = -1; // do return from view
hit = true;
}
break;
if(!hit)
break;
// fall through to next case
__attribute__ ((fallthrough));
}
case KEY_ENT:
run_worker = false;
@ -35,10 +40,86 @@ std::function<void(int)> ViewMonitor::keypress(int& key)
return calls[0];
}
std::string ViewMonitor::fancyDigitalString(uint8_t& b)
{
std::stringstream str;
str << std::bitset<8>(b).to_string();
str << " ";
str << "0x" << std::setfill ('0') << std::setw(2) << std::hex << (int) b << std::dec;
return str.str();
}
std::string ViewMonitor::fancyAnalogString(uint16_t& v)
{
std::stringstream str;
double volt = round(v * 100.0 * 5.0 / 1023.0) / 100.0;
str << std::setfill ('0') << std::setw(4) << (int) v << " " << std::fixed << std::setprecision(2) << volt << " V ";
str << "[";
uint8_t p = round(v * 40.0 / 1023.0);
for(uint8_t i = 0; i < p; i++)
str << "X";
for(uint8_t i = 0; i < 40 - p; i++)
str << " ";
str << "]" << std::endl;
return str.str();
}
void ViewMonitor::worker()
{
B15F& drv = B15F::getInstance();
while(run_worker)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
try
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
uint8_t be0 = drv.digitalRead0();
uint8_t be1 = drv.digitalRead1();
uint8_t dsw = drv.readDipSwitch();
uint16_t adc[8];
for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
adc[i] = drv.analogRead(i);
std::stringstream str;
// hline
for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
if(i % 2 == 0)
str << "-";
else
str << " ";
str << std::endl;
str << "Digitale Enigaenge:" << std::endl;
str << "Binaere Eingabe 0: " << fancyDigitalString(be0) << std::endl;
str << "Binaere Eingabe 1: " << fancyDigitalString(be1) << std::endl;
str << "Dip Schalter (S7): " << fancyDigitalString(dsw) << std::endl;
// hline
for(uint8_t i = 0; i < width - 2 * text_offset_x; i++)
if(i % 2 == 0)
str << "-";
else
str << " ";
str << std::endl;
str << "Analoge Eingaenge:" << std::endl;
for(uint8_t i = 0; i < sizeof(adc) / sizeof(adc[0]); i++)
{
str << "Kanal " << std::to_string((int) i) << ": ";
str << fancyAnalogString(adc[i]) << std::endl;
}
text = str.str();
repaint();
}
catch(...)
{
drv.reconnect();
}
}
}