ncurses begin

This commit is contained in:
Tristan Krause 2019-04-03 16:15:35 +02:00
parent 1b36640c4f
commit 282a0dbc34
5 changed files with 77 additions and 51 deletions

View file

@ -12,7 +12,7 @@ OBJ_MAIN = main.o
OUT_CLI = cli OUT_CLI = cli
OBJ_CLI = cli.o OBJ_CLI = cli.o
CFLAGS = -std=c++14 -O3 -Wall -Wextra CFLAGS = -std=c++14 -O3 -Wall -Wextra
LDFLAGS = LDFLAGS = -lcurses
OBJECTS = drv/usart.o drv/b15f.o drv/plottyfile.o drv/dot.o OBJECTS = drv/usart.o drv/b15f.o drv/plottyfile.o drv/dot.o
COMPILE = $(COMPILER_PATH) $(CFLAGS) COMPILE = $(COMPILER_PATH) $(CFLAGS)

Binary file not shown.

View file

@ -1,51 +1,97 @@
#include <stdio.h> #include <stdio.h>
#include <ncurses.h> // sudo apt-get install libncurses5-dev #include <ncurses.h> // sudo apt-get install libncurses5-dev
#include <vector>
#include <string>
#include <iostream>
#include <sys/ioctl.h>
#include <unistd.h>
#define WIDTH 30 #define width 30
#define HEIGHT 10 #define height 10
int startx = 0; WINDOW *win_menu;
int starty = 0;
char *choices[] = { std::vector<std::string> choices = {
"Choice 1", "Choice 1",
"Choice 2", "Choice 2",
"Choice 3", "Choice 3",
"Choice 4", "Choice 4",
"Exit", "Exit",
}; };
int n_choices = sizeof(choices) / sizeof(char *);
void print_menu(WINDOW *menu_win, int highlight);
void init_win(WINDOW*& win)
{
struct winsize size;
if (ioctl(0, TIOCGWINSZ, (char *) &size) < 0)
throw std::runtime_error("TIOCGWINSZ error");
int start_x = (size.ws_col - width);
int start_y = (size.ws_row - height);
if(start_x % 2)
start_x++;
if(start_y % 2)
start_y++;
start_x /= 2;
start_y /= 2;
win = newwin(height, width, start_y, start_x);
keypad(win, TRUE);
refresh();
}
void print_menu(WINDOW *win_menu, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(win_menu, 0, 0);
for(i = 0; i < choices.size(); ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(win_menu, A_REVERSE);
mvwprintw(win_menu, y, x, "%s", choices[i].c_str());
wattroff(win_menu, A_REVERSE);
}
else
mvwprintw(win_menu, y, x, "%s", choices[i].c_str());
++y;
}
wrefresh(win_menu);
}
int main() int main()
{ WINDOW *menu_win; {
int highlight = 1; int highlight = 1;
int choice = 0; int choice = 0;
int c; int c;
initscr(); initscr();
start_color();
curs_set(0); // 0: invisible, 1: normal, 2: very visible
clear(); clear();
noecho(); noecho();
cbreak(); /* Line buffering disabled. pass on everything */ cbreak(); /* Line buffering disabled. pass on everything */
startx = (80 - WIDTH) / 2;
starty = (24 - HEIGHT) / 2;
menu_win = newwin(HEIGHT, WIDTH, starty, startx); init_win(win_menu);
keypad(menu_win, TRUE);
mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice"); mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
print_menu(win_menu, highlight);
refresh(); refresh();
print_menu(menu_win, highlight);
while(1) while(1)
{ c = wgetch(menu_win); { c = wgetch(win_menu);
switch(c) switch(c)
{ case KEY_UP: { case KEY_UP:
if(highlight == 1) if(highlight == 1)
highlight = n_choices; highlight = choices.size();
else else
--highlight; --highlight;
break; break;
case KEY_DOWN: case KEY_DOWN:
if(highlight == n_choices) if(highlight == choices.size())
highlight = 1; highlight = 1;
else else
++highlight; ++highlight;
@ -58,34 +104,13 @@ int main()
refresh(); refresh();
break; break;
} }
print_menu(menu_win, highlight); print_menu(win_menu, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */ if(choice != 0) /* User did a choice come out of the infinite loop */
break; break;
} }
mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1]); //mvprintw(23, 0, "You chose choice %d with choice string %s\n", choice, choices[choice - 1].c_str());
clrtoeol(); clrtoeol();
refresh(); refresh();
endwin(); endwin();
return 0; return 0;
} }
void print_menu(WINDOW *menu_win, int highlight)
{
int x, y, i;
x = 2;
y = 2;
box(menu_win, 0, 0);
for(i = 0; i < n_choices; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}

View file

@ -6,14 +6,15 @@ set xlabel 'U_{DS} [V]'
set ylabel 'I_D [mA]' set ylabel 'I_D [mA]'
set xrange [0:5] set xrange [0:5]
set yrange [0:50] set yrange [0:50]
set label at 4,3 'U_{GS} [V] = 460' left set label at 4,2 'U_{GS} [V] = 440' left
set label at 4,4 'U_{GS} [V] = 460' left
set label at 4,7 'U_{GS} [V] = 480' left set label at 4,7 'U_{GS} [V] = 480' left
set label at 3,13 'U_{GS} [V] = 500' left set label at 3,14 'U_{GS} [V] = 500' left
set label at 2,22 'U_{GS} [V] = 520' left set label at 2,22 'U_{GS} [V] = 520' left
set label at 1,33 'U_{GS} [V] = 540' left set label at 1,33 'U_{GS} [V] = 540' left
set label at 0,39 'U_{GS} [V] = 560' left set label at 0,38 'U_{GS} [V] = 560' left
set label at 0,39 'U_{GS} [V] = 580' left set label at 0,38 'U_{GS} [V] = 580' left
set label at 0,39 'U_{GS} [V] = 600' left set label at 0,38 'U_{GS} [V] = 600' left
unset output unset output
set terminal qt set terminal qt
unset output unset output

Binary file not shown.