diff --git a/driver/cli b/driver/cli index 1678fd1..3730bca 100755 Binary files a/driver/cli and b/driver/cli differ diff --git a/driver/cli.cpp b/driver/cli.cpp index 2e24c75..a40e257 100644 --- a/driver/cli.cpp +++ b/driver/cli.cpp @@ -71,6 +71,23 @@ void signal_handler(int signal) } } +void abort_handler(std::exception& ex) +{ + ViewInfo* view = new ViewInfo(); + view->setTitle("Fehler"); + std::string msg(ex.what()); + msg += "\n\nBeende in 5 Sekunden."; + view->setText(msg.c_str()); + view->setLabelClose(""); + view->repaint(); + + std::this_thread::sleep_for(std::chrono::milliseconds(5000)); + + cleanup(); + std::cerr << std::endl << "*** EXCEPTION ***" << std::endl << ex.what() << std::endl; + exit(EXIT_FAILURE); +} + void init() { // init b15 driver @@ -79,6 +96,7 @@ void init() std::cout << std::endl << "Starte in 3s ..." << std::endl; sleep(3); #endif + B15F::setAbortHandler(&abort_handler); // init all ncurses stuff initscr(); @@ -366,8 +384,11 @@ int main() { init(); - show_main(0); + int exit_code = EXIT_SUCCESS; + + show_main(0); cleanup(); - return EXIT_SUCCESS; + + return exit_code; } diff --git a/driver/drv/b15f.cpp b/driver/drv/b15f.cpp index abbf044..79b310b 100644 --- a/driver/drv/b15f.cpp +++ b/driver/drv/b15f.cpp @@ -1,6 +1,7 @@ #include "b15f.h" B15F* B15F::instance = nullptr; +errorhandler_t B15F::errorhandler = nullptr; B15F::B15F() { @@ -228,7 +229,7 @@ uint16_t B15F::analogRead(uint8_t channel) uint16_t adc = usart.readInt(); if(adc > 1023) - throw DriverException("Bad ADC data detected"); + throw DriverException("Bad ADC data detected (1)"); return adc; } @@ -251,7 +252,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) - throw DriverException("Bad ADC data detected"); + throw DriverException("Bad ADC data detected (2)"); } uint8_t aw = usart.readByte(); @@ -270,6 +271,14 @@ void B15F::delay_us(uint16_t us) { std::this_thread::sleep_for(std::chrono::microseconds(us)); } + +B15F& B15F::getInstance(void) +{ + if(!instance) + instance = new B15F(); + + return *instance; +} // https://stackoverflow.com/a/478960 std::string B15F::exec(std::string cmd) { @@ -284,11 +293,26 @@ std::string B15F::exec(std::string cmd) { } return result; } - -B15F& B15F::getInstance(void) -{ - if(!instance) - instance = new B15F(); - return *instance; + +void B15F::abort(std::string msg) +{ + DriverException ex(msg); + abort(ex); +} + +void B15F::abort(std::exception& ex) +{ + if(errorhandler) + errorhandler(ex); + else + { + std::cerr << "NOTICE: B15F::errorhandler not set" << std::endl; + throw ex; + } +} + +void B15F::setAbortHandler(errorhandler_t func) +{ + errorhandler = func; } diff --git a/driver/drv/b15f.h b/driver/drv/b15f.h index 2ffff03..9cfed2d 100644 --- a/driver/drv/b15f.h +++ b/driver/drv/b15f.h @@ -18,6 +18,9 @@ #include "driverexception.h" #include "timeoutexception.h" +typedef std::function errorhandler_t; + + class B15F { private: @@ -88,7 +91,25 @@ public: * \param cmd Der Befehl */ static std::string exec(std::string cmd); - + + /** + * Multithread sicherer Abbruch des B15F-Treibers + * \param msg Beschreibung der Abbruchursache + */ + static void abort(std::string msg); + + /** + * Multithread sicherer Abbruch des B15F-Treibers + * \param ex Exception als Abbruchursache + */ + static void abort(std::exception& ex); + + /** + * Setzt eine Fehlerbehandlungsroutine für den Treiberabbruch (abort) + * \param func Funktion, die Exception als Parameter bekommt + */ + static void setAbortHandler(errorhandler_t func); + /*************************************/ @@ -184,6 +205,7 @@ private: USART usart; static B15F* instance; + static errorhandler_t errorhandler; // CONSTANTS diff --git a/driver/drv/driverexception.h b/driver/drv/driverexception.h index 05f8e3e..18a88a9 100644 --- a/driver/drv/driverexception.h +++ b/driver/drv/driverexception.h @@ -30,3 +30,4 @@ protected: }; #endif // DRIVEREXCEPTION_H + diff --git a/driver/main b/driver/main index 196b9fa..dbfb611 100755 Binary files a/driver/main and b/driver/main differ diff --git a/driver/ui/view_monitor.cpp b/driver/ui/view_monitor.cpp index 81a7957..ea8b083 100644 --- a/driver/ui/view_monitor.cpp +++ b/driver/ui/view_monitor.cpp @@ -125,7 +125,15 @@ void ViewMonitor::worker() } catch(...) { - drv.reconnect(); + try + { + drv.reconnect(); + } + catch(...) + { + B15F::abort("yoho meine dudes"); + return; + } } } } diff --git a/firmware/B15F.elf b/firmware/B15F.elf index 4360990..9207531 100755 Binary files a/firmware/B15F.elf and b/firmware/B15F.elf differ