Added basic GUI support

This commit is contained in:
microbuilder committed 2016-12-16 02:40:21 +01:00
1 parent 46793ddde8
commit 2b42724b2e
5 files changed
+184 -57

No files matched your search

Binary file not shown.
+2 -1
View File
@@ -40,7 +40,8 @@ commands_err_unknown_arg(char *arg_name)
int
commands_cmd_reset(void)
{
LOGINFO("attempting to reset (toggling DTR)\n");
// Can't log data w/GUI since it kills ncurses with -g option
//LOGINFO("attempting to reset (toggling DTR)\n");
/* Toggle the DTR line to provoke a reset */
serialport_set_dtr(1);
+180 -54
View File
@@ -3,6 +3,45 @@
#include <unistd.h>
#include <curses.h>
#include "infohelper.h"
#include "commands.h"
enum gui_action {
GUI_ACTION_ABOUT = 0,
GUI_ACTION_RESET,
GUI_ACTION_EXIT,
GUI_ACTION_NONE = 0xFF,
};
/* Advanced prototypes for command handler functions */
int gui_handler_about(void);
int gui_handler_reset(void);
static const struct {
const char *name;
const char shortcut;
const enum gui_action action;
int (*handler)(void);
} gui_menu_items[] = {
{ "About", '?', GUI_ACTION_ABOUT, gui_handler_about },
{ "Reset Device", 'r', GUI_ACTION_RESET, gui_handler_reset },
{ "Exit", 'X', GUI_ACTION_EXIT, NULL },
{ NULL, '\0', GUI_ACTION_NONE, NULL }
};
static volatile int g_gui_menu_item_count = 0;
int
gui_handler_about(void) {
printw("About Adafruit nRF52 bootutil!\n");
return 0;
}
int
gui_handler_reset(void) {
printw("Resetting the nRF52 (toggling DTR)\n");
commands_cmd_reset();
return 0;
}
static void
gui_config(void)
@@ -17,35 +56,72 @@ gui_config(void)
noecho();
/* Setup color pairs */
init_pair(1, COLOR_CYAN, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_BLACK);
/* Hide the cursor by default */
curs_set(0);
}
/*
void gui_menu_print(WINDOW *win, int highlight)
{
int rows, cols;
int x, y, i;
x = 2;
y = 2;
/* Get the window dimenstions */
getmaxyx(stdscr, rows, cols);
/* Draw some borders (hack, remove) */
box(win, 0, 0);
for(i = 0; i < 10; ++i) {
/* Add the title */
char *title = "Adafruit nRF52 Bootloader Utility";
attron(COLOR_PAIR(1));
attron(A_BOLD);
mvprintw(2, (cols-strlen(title))/2, "%s", title);
attroff(A_BOLD);
attroff(COLOR_PAIR(1));
/* Set the menu item start position */
y = 4;
x = (cols-strlen(title))/2;
wmove(win, y, x);
if (highlight > g_gui_menu_item_count) {
highlight = 0;
}
for(i = 0; i < g_gui_menu_item_count; ++i) {
/* Print the menu item name */
if(highlight == i + 1) {
wattron(win, A_REVERSE);
mvwprintw(win, y, x, "%s", "string");
mvwprintw(win, y, x, "%s", gui_menu_items[i].name);
wattroff(win, A_REVERSE);
} else {
mvwprintw(win, y, x, "%s", "string");
mvwprintw(win, y, x, "%s", gui_menu_items[i].name);
}
/* Print the shortcut */
if (gui_menu_items[i].shortcut != '\0') {
printw(" [");
attron(A_BOLD);
printw("%c", gui_menu_items[i].shortcut);
attroff(A_BOLD);
printw("]");
}
++y;
}
/* Change the cursor to displat any debug output at a safe location*/
wmove(win, 5 + g_gui_menu_item_count, x);
wrefresh(win);
}
*/
int
gui_init(void)
gui_init(const char *ttyname)
{
int i;
int ch;
int row, col;
int selected_menu_item;
int execute_menu_item;
int rows, cols;
WINDOW * mainwin;
/* Init ncurses window */
@@ -57,57 +133,107 @@ gui_init(void)
/* Setup the core window properties */
gui_config();
/* Get the window dimenstions */
getmaxyx(stdscr,row,col);
/* Draw some borders (hack, remove) */
box(mainwin, 0, 0);
/* Add the title */
char *title = "Adafruit nRF52 Bootloader Utility";
attron(COLOR_PAIR(1));
attron(A_BOLD);
mvprintw(row/2,(col-strlen(title))/2,"%s",title);
attroff(A_BOLD);
attroff(COLOR_PAIR(1));
/* Wait for some feedback */
mvprintw(1, 2, "Type any character to see it in bold and color");
ch = getch();
/* Display the key value */
mvprintw(2, 2, "The pressed key is ");
attron(COLOR_PAIR(1));
attron(A_BOLD);
switch(ch) {
case KEY_F(1):
printw("F1");
break;
case KEY_LEFT:
printw("LEFT");
break;
case KEY_RIGHT:
printw("RIGHT");
break;
case KEY_UP:
printw("UP");
break;
case KEY_DOWN:
printw("DOWN");
break;
default:
printw("%c", ch);
break;
/* Count the total menu items */
/* Check how many menu items we have */
g_gui_menu_item_count = -1;
while (gui_menu_items[++g_gui_menu_item_count].action != GUI_ACTION_NONE) {
}
if (g_gui_menu_item_count < 1) {
LOGERR("No menu items defined\n");
return EXIT_FAILURE;
}
/* Get the window dimenstions */
getmaxyx(stdscr, rows, cols);
/* Show the TTY/Serial port */
attron(COLOR_PAIR(2));
attron(A_BOLD);
mvprintw(rows-4, (cols-strlen(ttyname))/2, "%s", ttyname);
attroff(A_BOLD);
attroff(COLOR_PAIR(2));
/* Navigation help menu */
char *dataentry = "Use the arrow or shortcut keys to continue, 'X' to exit";
attron(COLOR_PAIR(1));
attron(A_BOLD);
mvprintw(rows-3, (cols-strlen(dataentry))/2, "%s", dataentry);
attroff(A_BOLD);
attroff(COLOR_PAIR(1));
refresh();
/* Wait a bit before closing */
sleep(2);
/* Render the menu */
selected_menu_item = 1;
gui_menu_print(mainwin, selected_menu_item);
/* Loop until we get a non navigation */
while(1) {
ch = getch();
execute_menu_item = -1;
switch(ch) {
case KEY_F(1):
break;
case KEY_LEFT:
selected_menu_item --;
break;
case KEY_RIGHT:
selected_menu_item++;
break;
case KEY_UP:
selected_menu_item--;
break;
case KEY_DOWN:
selected_menu_item++;
break;
case 10: /* ENTER */
execute_menu_item = selected_menu_item-1;
break;
case 'Z':
goto cleanup;
break;
default:
/* Check for hotkeys */
for (i=0;i<g_gui_menu_item_count;i++) {
if (ch == gui_menu_items[i].shortcut) {
execute_menu_item = i;
}
}
break;
}
/* Check if we should execute an action */
if (execute_menu_item != -1) {
if (gui_menu_items[execute_menu_item].action != GUI_ACTION_NONE) {
if (gui_menu_items[execute_menu_item].handler != NULL) {
attron(COLOR_PAIR(2));
attron(A_BOLD);
gui_menu_items[execute_menu_item].handler();
attroff(A_BOLD);
attroff(COLOR_PAIR(2));
wrefresh(mainwin);
}
}
if (gui_menu_items[execute_menu_item].action == GUI_ACTION_EXIT) {
goto cleanup;
}
}
/* Check menu boundaries */
if (selected_menu_item < 1) {
selected_menu_item = 1;
}
if (selected_menu_item > g_gui_menu_item_count) {
selected_menu_item = g_gui_menu_item_count;
}
/* Re-render the menu with the new menu item */
gui_menu_print(mainwin, selected_menu_item);
/* Update the display */
refresh();
}
cleanup:
/* Clean up after ourselves */
delwin(mainwin);
endwin();
+1 -1
View File
@@ -1,6 +1,6 @@
#ifndef __GUI_H__
#define __GUI_H__
int gui_init(void);
int gui_init(const char *ttyname);
#endif
+1 -1
View File
@@ -193,7 +193,7 @@ main(int argc, char **argv)
/* GUI handler */
if (guiflag) {
rc = gui_init();
rc = gui_init(tty);
if (rc != 0) {
goto error;
}