diff --git a/tools/bootutil/binaries/osx/bootutil b/tools/bootutil/binaries/osx/bootutil index bc456373..122b5ecc 100755 Binary files a/tools/bootutil/binaries/osx/bootutil and b/tools/bootutil/binaries/osx/bootutil differ diff --git a/tools/bootutil/commands.c b/tools/bootutil/commands.c index f522aba7..399ce3f4 100644 --- a/tools/bootutil/commands.c +++ b/tools/bootutil/commands.c @@ -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); diff --git a/tools/bootutil/gui.c b/tools/bootutil/gui.c index 391a0a1a..1f5e628d 100644 --- a/tools/bootutil/gui.c +++ b/tools/bootutil/gui.c @@ -3,6 +3,45 @@ #include #include #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) { + 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(); diff --git a/tools/bootutil/gui.h b/tools/bootutil/gui.h index c8875629..95bc4fa0 100644 --- a/tools/bootutil/gui.h +++ b/tools/bootutil/gui.h @@ -1,6 +1,6 @@ #ifndef __GUI_H__ #define __GUI_H__ -int gui_init(void); +int gui_init(const char *ttyname); #endif diff --git a/tools/bootutil/main.c b/tools/bootutil/main.c index 1867eac3..5cc4428e 100644 --- a/tools/bootutil/main.c +++ b/tools/bootutil/main.c @@ -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; }