B15F Board 15 Famulus Edition

cli.cpp
1 //#define B15F_CLI_DEBUG
2
3 #include <stdio.h>
4 #include <ncurses.h> // sudo apt-get install libncurses5-dev
5 #include <vector>
6 #include <string>
7 #include <iostream>
8 #include <signal.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <future>
13 #include <thread>
14 #include <chrono>
15 #include "drv/b15f.h"
16 #include "ui/ui.h"
17 #include "ui/view_selection.h"
18 #include "ui/view_info.h"
19 #include "ui/view_monitor.h"
20 #include "ui/view_promt.h"
21
22 constexpr uint8_t WIN_WIDTH = 80;
23 constexpr uint8_t WIN_HEIGHT = 24;
24
25 volatile int win_changed_cooldown = 0;
26 volatile bool t_refresh_active = false;
27
28 void signal_handler(int signal)
29 {
30 if(signal == SIGWINCH)
31 {
32 win_changed_cooldown = 10; // 100ms
33
34 if (!t_refresh_active)
35 {
36 if(t_refresh.joinable())
37 t_refresh.join();
38 t_refresh_active = true;
39 t_refresh = std::thread([]()
40 {
41
42 while(win_changed_cooldown--)
43 std::this_thread::sleep_for(std::chrono::milliseconds(10));
44
45 t_refresh_active = false;
46
47 if(win_stack.size())
48 win_stack.back()->repaint();
49
50 });
51 }
52
53 }
54 else if(signal == SIGINT)
55 {
56 cleanup();
57 std::cout << "SIGINT - Abbruch." << std::endl;
58 exit(EXIT_FAILURE);
59 }
60 }
61
62 void abort_handler(std::exception& ex)
63 {
64 ViewInfo* view = new ViewInfo();
65 view->setTitle("Fehler");
66 std::string msg(ex.what());
67 msg += "\n\nBeende in 5 Sekunden.";
68 view->setText(msg.c_str());
69 view->setLabelClose("");
70 view->repaint();
71
72 std::this_thread::sleep_for(std::chrono::milliseconds(5000));
73
74 cleanup();
75 std::cerr << std::endl << "*** EXCEPTION ***" << std::endl << ex.what() << std::endl;
76 exit(EXIT_FAILURE);
77 }
78
79 void init()
80 {
81 // init b15 driver
82 B15F::getInstance();
83 #ifndef B15F_CLI_DEBUG
84 std::cout << std::endl << "Starte in 3s ..." << std::endl;
85 sleep(3);
86 #endif
87
88 // init all ncurses stuff
89 initscr();
90 start_color();
91 curs_set(0); // 0: invisible, 1: normal, 2: very visible
92 clear();
93 noecho();
94 cbreak(); // Line buffering disabled. pass on everything
95 mousemask(ALL_MOUSE_EVENTS, NULL);
96
97 // connect signals to handler
98 signal(SIGWINCH, signal_handler);
99 signal(SIGINT, signal_handler);
100
101 // set view context
102 View::setWinContext(newwin(WIN_HEIGHT, WIN_WIDTH, 0, 0));
103
104 // set graphical error handler
105 B15F::setAbortHandler(&abort_handler);
106 }
107
108
109 int main()
110 {
111 init();
112
113 int exit_code = EXIT_SUCCESS;
114
115 show_main(0);
116
117 cleanup();
118
119 return exit_code;
120 }
ViewInfo
Definition: view_info.h:8
B15F::getInstance
static B15F & getInstance(void)
Definition: b15f.cpp:10
B15F::setAbortHandler
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:186