diff --git a/driver/cli b/driver/cli index ee76f2f..8b7669f 100755 Binary files a/driver/cli and b/driver/cli differ diff --git a/driver/cli.cpp b/driver/cli.cpp index 391a125..357946a 100644 --- a/driver/cli.cpp +++ b/driver/cli.cpp @@ -17,7 +17,7 @@ #include "ui/view_selection.h" #include "ui/view_info.h" #include "ui/view_monitor.h" -//#include "drv/b15f.h" +#include "drv/b15f.h" // global error message std::string ERR_MSG; @@ -54,7 +54,9 @@ void signal_handler(int signal) void init() { // init b15 driver - //B15F::getInstance(); + B15F::getInstance(); + //std::cout << std::endl << "Starte in 3s ..." << std::endl; + //sleep(3); // init all ncurses stuff initscr(); @@ -70,7 +72,7 @@ void init() signal(SIGINT, signal_handler); // set view context - View::setWinContext(newwin(32, 128, 0, 0)); + View::setWinContext(newwin(25, 85, 0, 0)); } void finish(int) @@ -118,7 +120,7 @@ void show_monitor(int) { ViewMonitor* view = new ViewMonitor(); view->setTitle("Monitor"); - view->setText("U0 = "); + view->setText("\nErfasse Messwerte..."); view->setLabelClose("[ Zurueck ]"); view->repaint(); @@ -131,6 +133,10 @@ void show_main(int) ViewSelection* view = new ViewSelection(); view->setTitle("B15F - Command Line Interface"); view->addChoice("[ Monitor - Eingaben beobachten ]", &show_monitor); + view->addChoice("[ Digitale Ausgabe BE0 ]", &show_monitor); + view->addChoice("[ Digitale Ausgabe BE1 ]", &show_monitor); + view->addChoice("[ Analoge Ausgabe AA0 ]", &show_monitor); + view->addChoice("[ Analoge Ausgabe AA1 ]", &show_monitor); view->addChoice("[ Informationen ]", &show_info); view->addChoice("", nullptr); view->addChoice("[ Beenden ]", &finish); diff --git a/driver/drv/b15f.cpp b/driver/drv/b15f.cpp index 4a9cae5..4348642 100644 --- a/driver/drv/b15f.cpp +++ b/driver/drv/b15f.cpp @@ -27,18 +27,6 @@ void B15F::init() std::cout << "OK" << std::endl; - // Temporärer Test - /* - uint8_t block[16]; - while(1) - { - usart.writeBlock(&block[0], 0, sizeof(block)); - usart.printStatistics(); - usleep(1000); - } - throw std::runtime_error("SCHLUSS");*/ - - std::cout << PRE << "Teste Verbindung... " << std::flush; uint8_t tries = 3; @@ -154,7 +142,7 @@ std::vector B15F::getBoardInfo(void) bool B15F::activateSelfTestMode() { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_ST); uint8_t aw = usart.readByte(); @@ -163,71 +151,85 @@ bool B15F::activateSelfTestMode() bool B15F::digitalWrite0(uint8_t port) { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_BA0); usart.writeByte(port); - uint8_t aw = usart.readByte(); + uint8_t aw = usart.readByte(); + delay_us(1); return aw == MSG_OK; } bool B15F::digitalWrite1(uint8_t port) { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_BA1); usart.writeByte(port); - uint8_t aw = usart.readByte(); + uint8_t aw = usart.readByte(); + delay_us(1); return aw == MSG_OK; } uint8_t B15F::digitalRead0() { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_BE0); - return usart.readByte(); + uint8_t byte = usart.readByte(); + delay_us(1); + return byte; } uint8_t B15F::digitalRead1() { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_BE1); - return usart.readByte(); + uint8_t byte = usart.readByte(); + delay_us(1); + return byte; } uint8_t B15F::readDipSwitch() { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_DSW); - return usart.readByte(); + uint8_t byte = usart.readByte(); + delay_us(1); + return byte; } bool B15F::analogWrite0(uint16_t value) { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_AA0); usart.writeInt(value); - uint8_t aw = usart.readByte(); + uint8_t aw = usart.readByte(); + delay_us(1); return aw == MSG_OK; } bool B15F::analogWrite1(uint16_t value) { - usart.clearInputBuffer(); + //usart.clearInputBuffer(); usart.writeByte(RQ_AA1); usart.writeInt(value); - uint8_t aw = usart.readByte(); + uint8_t aw = usart.readByte(); + delay_us(1); return aw == MSG_OK; } uint16_t B15F::analogRead(uint8_t channel) { - usart.clearInputBuffer(); + if(channel > 7) + throw DriverException("Bad ADC channel: " + std::to_string(channel)); + //usart.clearInputBuffer(); usart.writeByte(RQ_ADC); usart.writeByte(channel); - return usart.readInt(); + uint16_t adc = usart.readInt(); + delay_us(1); + return adc; } void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count) @@ -243,12 +245,6 @@ void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset usart.writeInt(static_cast(delta)); usart.writeInt(count); - /* - uint8_t aw = usart.readByte(); - - if(aw != MSG_OK) - throw DriverException("Mikrocontroller nicht synchron"); - */ for(uint16_t i = 0; i < count; i++) { diff --git a/driver/drv/timeoutexception.h b/driver/drv/timeoutexception.h index dea32b3..159ec7b 100644 --- a/driver/drv/timeoutexception.h +++ b/driver/drv/timeoutexception.h @@ -12,10 +12,10 @@ public: { } - explicit TimeoutException(const std::string& message, int timeout) : msg(message), timeout(timeout) + explicit TimeoutException(const std::string& message, int timeout) : msg(message), m_timeout(timeout) { if(!msg.length()) - msg = "Timeout reached (" + std::to_string(timeout) + ")"; + msg = "Timeout reached (" + std::to_string(m_timeout) + ")"; } virtual ~TimeoutException() throw () @@ -29,7 +29,7 @@ public: protected: std::string msg; - int timeout; + int m_timeout; }; #endif // TIMEOUTEXCEPTION_H diff --git a/driver/ui/view_monitor.cpp b/driver/ui/view_monitor.cpp index 96d808e..12880ca 100644 --- a/driver/ui/view_monitor.cpp +++ b/driver/ui/view_monitor.cpp @@ -13,14 +13,19 @@ std::function 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 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(); + } } } diff --git a/driver/ui/view_monitor.h b/driver/ui/view_monitor.h index 5c9e0e0..bb64cda 100644 --- a/driver/ui/view_monitor.h +++ b/driver/ui/view_monitor.h @@ -3,7 +3,10 @@ #include #include +#include +#include #include "view_info.h" +#include "../drv/b15f.h" class ViewMonitor : public ViewInfo { @@ -12,6 +15,10 @@ public: ViewMonitor(void); virtual std::function keypress(int& key) override; +private: + std::string fancyDigitalString(uint8_t& b); + std::string fancyAnalogString(uint16_t& v); + protected: virtual void worker(void); volatile bool run_worker = true;