diff --git a/driver/cli b/driver/cli index d0c668b..699162a 100755 Binary files a/driver/cli and b/driver/cli differ diff --git a/driver/cli.cpp b/driver/cli.cpp index 0947d0c..506e143 100644 --- a/driver/cli.cpp +++ b/driver/cli.cpp @@ -108,14 +108,18 @@ void finish(int) void view_back(int) { - win_stack.pop_back(); + if(win_stack.size()) + { + delete win_stack.back(); + win_stack.pop_back(); + } if(win_stack.size()) win_stack.back()->repaint(); } void input(int) { - std::function nextCall; + call_t nextCall; int key; do { @@ -136,7 +140,7 @@ void show_info(int) { ViewInfo* view = new ViewInfo(); view->setTitle("Info"); - view->setText("Informationen zu Board 15 Famulus Edition\nEs war einmal"); + view->setText("Informationen zu Board 15 Famulus Edition\nEs war einmal..."); view->setLabelClose("[ Zurueck ]"); view->repaint(); @@ -184,7 +188,10 @@ void write_digital_output0(int) { try { - uint8_t port = std::stoi(static_cast(win_stack.back())->getInput(), 0, 16); + int d = std::stoi(static_cast(win_stack.back())->getInput(), 0, 16); + if(d > 255 || 0 > d) + throw std::invalid_argument("bad value"); + uint8_t port = static_cast(d); B15F& drv = B15F::getInstance(); drv.digitalWrite0(port); @@ -200,7 +207,10 @@ void write_digital_output1(int) { try { - uint8_t port = std::stoi(static_cast(win_stack.back())->getInput(), 0, 16); + int d = std::stoi(static_cast(win_stack.back())->getInput(), 0, 16); + if(d > 255 || 0 > d) + throw std::invalid_argument("bad value"); + uint8_t port = static_cast(d); B15F& drv = B15F::getInstance(); drv.digitalWrite1(port); @@ -300,6 +310,42 @@ void show_analog_output1(int) input(0); } + +void stop_selftest(int) +{ + B15F& drv = B15F::getInstance(); + drv.discard(); +} + +void start_selftest(int) +{ + B15F& drv = B15F::getInstance(); + drv.activateSelfTestMode(); + + ViewInfo* view = new ViewInfo(); + view->setTitle("Selbsttest aktiv"); + view->setText("Das B15 befindet sich jetzt im Selbsttestmodus.\n \nSelbsttest:\nZu Beginn geht der Reihe nach jede LED von BA0 bis BA1 an.\nDanach leuchten die LEDs an AA0 und AA1 kurz auf.\nZum Schluss spiegelt in einer Endlosschleife:\n* BA0 Port BE0\n* BA1 die DIP-Schalter S7\n* AA0 ADC0\n* AA1 ADC1"); + view->setLabelClose("[ Selbsttest Beenden ]"); + view->setCall(&stop_selftest); + view->repaint(); + + win_stack.push_back(view); + input(0); +} + +void show_selftest(int) +{ + ViewInfo* view = new ViewInfo(); + view->setTitle("Selbsttest"); + view->setText("Bitte entfernen Sie jetzt alle Draehte von den Anschlussklemmen und bestaetigen\nmit Enter."); + view->setLabelClose("[ Weiter ]"); + view->setCall(&start_selftest); + view->repaint(); + + win_stack.push_back(view); + input(0); +} + void show_main(int) { ViewSelection* view = new ViewSelection(); @@ -309,6 +355,7 @@ void show_main(int) view->addChoice("[ Digitale Ausgabe BE1 ]", &show_digital_output1); view->addChoice("[ Analoge Ausgabe AA0 ]", &show_analog_output0); view->addChoice("[ Analoge Ausgabe AA1 ]", &show_analog_output1); + view->addChoice("[ Selbsttest des B15 ]", &show_selftest); 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 4348642..43a4a6d 100644 --- a/driver/drv/b15f.cpp +++ b/driver/drv/b15f.cpp @@ -55,8 +55,6 @@ void B15F::init() void B15F::reconnect() { - std::cout << PRE << "Verbindung unterbrochen, stelle Verbindung neu her: " << std::flush; - uint8_t tries = RECONNECT_TRIES; while(tries--) { @@ -228,6 +226,8 @@ uint16_t B15F::analogRead(uint8_t channel) usart.writeByte(RQ_ADC); usart.writeByte(channel); uint16_t adc = usart.readInt(); + if(adc > 1023) + throw DriverException("Bad ADC data detected"); delay_us(1); return adc; } @@ -251,7 +251,7 @@ void B15F::analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset buffer_a[i] = usart.readInt(); buffer_b[i] = usart.readInt(); if(buffer_a[i] > 1023 || buffer_b[i] > 1023) - std::cout << PRE << "bad data detected" << std::endl; + throw DriverException("Bad ADC data detected"); } uint8_t aw = usart.readByte(); diff --git a/driver/main b/driver/main deleted file mode 100755 index 09368ac..0000000 Binary files a/driver/main and /dev/null differ diff --git a/driver/ui/view.cpp b/driver/ui/view.cpp index cc50010..d84baf7 100644 --- a/driver/ui/view.cpp +++ b/driver/ui/view.cpp @@ -13,6 +13,10 @@ View::View() keypad(win, TRUE); } +View::~View() +{ +} + void View::setWinContext(WINDOW* win) { View::win = win; diff --git a/driver/ui/view.h b/driver/ui/view.h index 7c8fc31..4bf2ab5 100644 --- a/driver/ui/view.h +++ b/driver/ui/view.h @@ -11,11 +11,13 @@ #include extern std::string ERR_MSG; +typedef std::function call_t; class View { public: View(void); + virtual ~View(void); static void setWinContext(WINDOW* win); static WINDOW* getWinContext(void); @@ -26,14 +28,14 @@ public: virtual void repaint(void); virtual void draw(void) = 0; - virtual std::function keypress(int& key) = 0; + virtual call_t keypress(int& key) = 0; protected: int width, height; int start_x = 0, start_y = 0; std::string title; - std::vector> calls; + std::vector calls; static WINDOW* win; constexpr static int KEY_ENT = 10; diff --git a/driver/ui/view_info.cpp b/driver/ui/view_info.cpp index 0528d45..056ef0f 100644 --- a/driver/ui/view_info.cpp +++ b/driver/ui/view_info.cpp @@ -15,7 +15,7 @@ void ViewInfo::setLabelClose(std::string label) this->label_close = label; } -void ViewInfo::setCall(std::function call) +void ViewInfo::setCall(call_t call) { calls[0] = call; } @@ -34,7 +34,7 @@ void ViewInfo::draw() wattroff(win, A_REVERSE); } -std::function ViewInfo::keypress(int& key) +call_t ViewInfo::keypress(int& key) { switch(key) { diff --git a/driver/ui/view_info.h b/driver/ui/view_info.h index 755dfe4..be5cbf4 100644 --- a/driver/ui/view_info.h +++ b/driver/ui/view_info.h @@ -9,9 +9,9 @@ public: ViewInfo(void); virtual void setText(std::string text); virtual void setLabelClose(std::string label);; - virtual void setCall(std::function call); + virtual void setCall(call_t call); virtual void draw(void) override; - virtual std::function keypress(int& key) override; + virtual call_t keypress(int& key) override; protected: std::string text; diff --git a/driver/ui/view_monitor.cpp b/driver/ui/view_monitor.cpp index 8f32b61..0dc03cf 100644 --- a/driver/ui/view_monitor.cpp +++ b/driver/ui/view_monitor.cpp @@ -4,7 +4,7 @@ ViewMonitor::ViewMonitor() : t_worker(&ViewMonitor::worker, this) { } -std::function ViewMonitor::keypress(int& key) +call_t ViewMonitor::keypress(int& key) { switch(key) { diff --git a/driver/ui/view_monitor.h b/driver/ui/view_monitor.h index bb64cda..33552bf 100644 --- a/driver/ui/view_monitor.h +++ b/driver/ui/view_monitor.h @@ -13,7 +13,7 @@ class ViewMonitor : public ViewInfo public: ViewMonitor(void); - virtual std::function keypress(int& key) override; + virtual call_t keypress(int& key) override; private: std::string fancyDigitalString(uint8_t& b); diff --git a/driver/ui/view_promt.cpp b/driver/ui/view_promt.cpp index 241dfe3..57ab60d 100644 --- a/driver/ui/view_promt.cpp +++ b/driver/ui/view_promt.cpp @@ -2,12 +2,18 @@ void ViewPromt::draw() { + curs_set(2); // show cursor + int li = text_offset_y; + int ci = 0; for(std::string line : str_split(message + input, "\n")) - mvwprintw(win, li++, text_offset_x, "%s", line.c_str()); + { + mvwprintw(win, ++li, text_offset_x, "%s", line.c_str()); + ci = line.length() + text_offset_x; + } button_offset_x = (width - label_cancel.length() - sep.length() - label_confirm.length()) / 2; - button_offset_y = height - text_offset_y + 1; + button_offset_y = height - text_offset_y; if(selection == 0) { @@ -25,6 +31,7 @@ void ViewPromt::draw() mvwprintw(win, button_offset_y, button_offset_x + label_cancel.length() + sep.length(), "%s", label_confirm.c_str()); wattroff(win, A_REVERSE); } + wmove(win, li, ci); } void ViewPromt::setMessage(std::string message) @@ -99,7 +106,8 @@ std::function ViewPromt::keypress(int& key) if(selection == 0) // exit key = -1; // do return from view else - ret = call_confirm; + ret = call_confirm; + curs_set(0); // hide cursor again break; default: break; @@ -108,6 +116,7 @@ std::function ViewPromt::keypress(int& key) if(key >= ' ' && key <= '~') input += (char) key; - repaint(); + if(key != KEY_ENT) + repaint(); return ret; } diff --git a/driver/ui/view_promt.h b/driver/ui/view_promt.h index d48be9c..fb74100 100644 --- a/driver/ui/view_promt.h +++ b/driver/ui/view_promt.h @@ -10,10 +10,10 @@ class ViewPromt : public View public: virtual void draw(void) override; virtual void setMessage(std::string message); - virtual void setConfirm(std::string name, std::function call); + virtual void setConfirm(std::string name, call_t call); virtual void setCancel(std::string name, bool cancelable); virtual std::string getInput(void); - virtual std::function keypress(int& key) override; + virtual call_t keypress(int& key) override; protected: size_t selection = 1; @@ -22,11 +22,11 @@ protected: std::string label_confirm = "[ OK ]"; std::string sep = " "; std::string label_cancel = "[ Cancel ]"; - std::function call_confirm = nullptr; + call_t call_confirm = nullptr; bool cancelable = true; int button_offset_x = 0, button_offset_y = 0; constexpr static int text_offset_x = 2; - constexpr static int text_offset_y = 3; + constexpr static int text_offset_y = 2; }; #endif // VIEW_PROMT_H diff --git a/driver/ui/view_selection.cpp b/driver/ui/view_selection.cpp index 88c30c7..5b947bd 100644 --- a/driver/ui/view_selection.cpp +++ b/driver/ui/view_selection.cpp @@ -13,15 +13,15 @@ void ViewSelection::draw() } } -void ViewSelection::addChoice(std::string name, std::function call) +void ViewSelection::addChoice(std::string name, call_t call) { choices.push_back(name); calls.push_back(call); } -std::function ViewSelection::keypress(int& key) +call_t ViewSelection::keypress(int& key) { - std::function ret = nullptr; + call_t ret = nullptr; switch(key) { case KEY_UP: diff --git a/driver/ui/view_selection.h b/driver/ui/view_selection.h index 5079b2f..4963f73 100644 --- a/driver/ui/view_selection.h +++ b/driver/ui/view_selection.h @@ -9,8 +9,8 @@ class ViewSelection : public View { public: virtual void draw(void) override; - virtual void addChoice(std::string name, std::function call); - virtual std::function keypress(int& key) override; + virtual void addChoice(std::string name, call_t call); + virtual call_t keypress(int& key) override; protected: