From 9a63892480615f35b06ced2ceba5086f435a7ea1 Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 19 Nov 2016 13:11:49 +0900 Subject: [PATCH] add menu structure and behavior --- main.c | 134 +++++++++++++++++++++++---------------- nanovna.h | 3 + ui.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 262 insertions(+), 61 deletions(-) diff --git a/main.c b/main.c index 2b6202c..b7e651a 100644 --- a/main.c +++ b/main.c @@ -370,6 +370,7 @@ ensure_edit_config(void) //memcpy(¤t_config, active, sizeof(config_t)); active = ¤t_config; + cal_status = 0; } @@ -665,6 +666,77 @@ void apply_error_term(void) } } +void +cal_collect(int type) +{ + ensure_edit_config(); + chMtxLock(&mutex); + + switch (type) { + case CAL_LOAD: + cal_status |= CALSTAT_LOAD; + memcpy(cal_data[CAL_LOAD], measured[0], sizeof measured[0]); + break; + + case CAL_OPEN: + cal_status |= CALSTAT_OPEN; + cal_status &= ~(CALSTAT_ES|CALSTAT_APPLY); + memcpy(cal_data[CAL_OPEN], measured[0], sizeof measured[0]); + break; + + case CAL_SHORT: + cal_status |= CALSTAT_SHORT; + cal_status &= ~(CALSTAT_ER|CALSTAT_APPLY); + memcpy(cal_data[CAL_SHORT], measured[0], sizeof measured[0]); + break; + + case CAL_THRU: + cal_status |= CALSTAT_THRU; + memcpy(cal_data[CAL_THRU], measured[1], sizeof measured[0]); + break; + + case CAL_ISOLN: + cal_status |= CALSTAT_ISOLN; + memcpy(cal_data[CAL_ISOLN], measured[1], sizeof measured[0]); + break; + } + chMtxUnlock(&mutex); +} + +void +cal_done(void) +{ + ensure_edit_config(); + if (!(cal_status & CALSTAT_LOAD)) + eterm_set(ETERM_ED, 0.0, 0.0); + //adjust_ed(); + if ((cal_status & CALSTAT_SHORT) && (cal_status & CALSTAT_OPEN)) { + eterm_calc_es(); + eterm_calc_er(-1); + } else if (cal_status & CALSTAT_OPEN) { + eterm_copy(CAL_SHORT, CAL_OPEN); + eterm_set(ETERM_ES, 0.0, 0.0); + eterm_calc_er(1); + } else if (cal_status & CALSTAT_SHORT) { + eterm_set(ETERM_ES, 0.0, 0.0); + cal_status &= ~CALSTAT_SHORT; + eterm_calc_er(-1); + } else { + eterm_set(ETERM_ER, 1.0, 0.0); + eterm_set(ETERM_ES, 0.0, 0.0); + } + + if (!(cal_status & CALSTAT_ISOLN)) + eterm_set(ETERM_EX, 0.0, 0.0); + if (cal_status & CALSTAT_THRU) { + eterm_calc_et(); + } else { + eterm_set(ETERM_ET, 1.0, 0.0); + } + + cal_status |= CALSTAT_APPLY; +} + static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[]) { const char *items[] = { "load", "open", "short", "thru", "isoln", "Es", "Er", "Et", "cal'ed" }; @@ -681,67 +753,17 @@ static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[]) char *cmd = argv[0]; if (strcmp(cmd, "load") == 0) { - ensure_edit_config(); - cal_status |= CALSTAT_LOAD; - chMtxLock(&mutex); - memcpy(cal_data[CAL_LOAD], measured[0], sizeof measured[0]); - chMtxUnlock(&mutex); + cal_collect(CAL_LOAD); } else if (strcmp(cmd, "open") == 0) { - ensure_edit_config(); - cal_status |= CALSTAT_OPEN; - cal_status &= ~(CALSTAT_ES|CALSTAT_APPLY); - chMtxLock(&mutex); - memcpy(cal_data[CAL_OPEN], measured[0], sizeof measured[0]); - chMtxUnlock(&mutex); + cal_collect(CAL_OPEN); } else if (strcmp(cmd, "short") == 0) { - ensure_edit_config(); - cal_status |= CALSTAT_SHORT; - cal_status &= ~(CALSTAT_ER|CALSTAT_APPLY); - chMtxLock(&mutex); - memcpy(cal_data[CAL_SHORT], measured[0], sizeof measured[0]); - chMtxUnlock(&mutex); + cal_collect(CAL_SHORT); } else if (strcmp(cmd, "thru") == 0) { - ensure_edit_config(); - cal_status |= CALSTAT_THRU; - chMtxLock(&mutex); - memcpy(cal_data[CAL_THRU], measured[1], sizeof measured[0]); - chMtxUnlock(&mutex); + cal_collect(CAL_THRU); } else if (strcmp(cmd, "isoln") == 0) { - ensure_edit_config(); - cal_status |= CALSTAT_ISOLN; - chMtxLock(&mutex); - memcpy(cal_data[CAL_ISOLN], measured[1], sizeof measured[0]); - chMtxUnlock(&mutex); + cal_collect(CAL_ISOLN); } else if (strcmp(cmd, "done") == 0) { - ensure_edit_config(); - if (!(cal_status & CALSTAT_LOAD)) - eterm_set(ETERM_ED, 0.0, 0.0); - //adjust_ed(); - if ((cal_status & CALSTAT_SHORT) && (cal_status & CALSTAT_OPEN)) { - eterm_calc_es(); - eterm_calc_er(-1); - } else if (cal_status & CALSTAT_OPEN) { - eterm_copy(CAL_SHORT, CAL_OPEN); - eterm_set(ETERM_ES, 0.0, 0.0); - eterm_calc_er(1); - } else if (cal_status & CALSTAT_SHORT) { - eterm_set(ETERM_ES, 0.0, 0.0); - cal_status &= ~CALSTAT_SHORT; - eterm_calc_er(-1); - } else { - eterm_set(ETERM_ER, 1.0, 0.0); - eterm_set(ETERM_ES, 0.0, 0.0); - } - - if (!(cal_status & CALSTAT_ISOLN)) - eterm_set(ETERM_EX, 0.0, 0.0); - if (cal_status & CALSTAT_THRU) { - eterm_calc_et(); - } else { - eterm_set(ETERM_ET, 1.0, 0.0); - } - - cal_status |= CALSTAT_APPLY; + cal_done(); draw_cal_status(); return; } else if (strcmp(cmd, "on") == 0) { diff --git a/nanovna.h b/nanovna.h index f3fc1bc..e6b719f 100644 --- a/nanovna.h +++ b/nanovna.h @@ -168,6 +168,9 @@ extern float measured[2][101][2]; #define ETERM_ET 3 /* error term transmission tracking */ #define ETERM_EX 4 /* error term isolation */ +void cal_collect(int type); +void cal_done(void); + /* * flash.c diff --git a/ui.c b/ui.c index b27a83e..957f1c3 100644 --- a/ui.c +++ b/ui.c @@ -162,6 +162,177 @@ ui_digit(void) } } + +// type of menu item +enum { + MT_NONE, + MT_BLANK, + MT_SUBMENU, + MT_CALLBACK, + MT_CANCEL +}; + +typedef void (*menuaction_cb_t)(int item); + + +static void menu_move_back(void); + + + +static void +menu_cal_cb(int item) +{ + switch (item) { + case 0: // OPEN + cal_collect(CAL_OPEN); + break; + case 1: // SHORT + cal_collect(CAL_SHORT); + break; + case 2: // LOAD + cal_collect(CAL_LOAD); + break; + case 3: // ISOLN + cal_collect(CAL_ISOLN); + break; + case 4: // THRU + cal_collect(CAL_THRU); + break; + } + selection++; + draw_cal_status(); +} + +static void +menu_caldone_cb(int item) +{ + cal_done(); + draw_cal_status(); + menu_move_back(); +} + +static void +menu_recall_cb(int item) +{ + if (item < 0 || item > 5) + return; + if (caldata_recall(item) == 0) { + ui_status = FALSE; + ui_hide(); + set_sweep(freq_start, freq_stop); + draw_cal_status(); + } +} + +typedef struct { + uint8_t type; + char *label; + const void *reference; +} menuitem_t; + +const menuitem_t menu_cal[] = { + { MT_CALLBACK, "OPEN", menu_cal_cb }, + { MT_CALLBACK, "SHORT", menu_cal_cb }, + { MT_CALLBACK, "LOAD", menu_cal_cb }, + { MT_CALLBACK, "ISOLN", menu_cal_cb }, + { MT_CALLBACK, "THRU", menu_cal_cb }, + { MT_CALLBACK, "DONE", menu_caldone_cb }, + { MT_CANCEL, "BACK", NULL }, + { MT_NONE, NULL, NULL } // sentinel +}; + +const menuitem_t menu_recall[] = { + { MT_CALLBACK, "0", menu_recall_cb }, + { MT_CALLBACK, "1", menu_recall_cb }, + { MT_CALLBACK, "2", menu_recall_cb }, + { MT_CALLBACK, "3", menu_recall_cb }, + { MT_CALLBACK, "4", menu_recall_cb }, + { MT_CANCEL, "BACK", NULL }, + { MT_NONE, NULL, NULL } // sentinel +}; + +const menuitem_t menu_top[] = { + { MT_SUBMENU, "CAL", menu_cal }, + { MT_SUBMENU, "RECALL", menu_recall }, + { MT_CANCEL, "BACK", NULL }, + { MT_NONE, NULL, NULL } // sentinel +}; + +#define MENU_STACK_DEPTH_MAX 4 +uint8_t menu_current_level = 0; +const menuitem_t *menu_stack[4] = { + menu_top, NULL, NULL, NULL +}; + +static void menu_move_back(void) +{ + if (menu_current_level == 0) + return; + menu_current_level--; + erase_buttons(); +} + +static void menu_move_top(void) +{ + if (menu_current_level == 0) + return; + menu_current_level = 0; + erase_buttons(); +} + +void menu_invoke(int selection) +{ + const menuitem_t *menu = menu_stack[menu_current_level]; + menu = &menu[selection]; + + switch (menu->type) { + case MT_NONE: + case MT_BLANK: + ui_status = FALSE; + ui_hide(); + break; + + case MT_CANCEL: + menu_move_back(); + break; + + case MT_CALLBACK: { + menuaction_cb_t cb = (menuaction_cb_t)menu->reference; + if (cb == NULL) + return; + (*cb)(selection); + break; + } + + case MT_SUBMENU: + if (menu_current_level >= 3) + break; + menu_current_level++; + menu_stack[menu_current_level] = (const menuitem_t*)menu->reference; + selection = 0; + break; + } +} + + +void +draw_menu_buttons(const menuitem_t *menu) +{ + int i = 0; + for (i = 0; i < 7; i++) { + if (menu[i].type == MT_NONE) + break; + if (menu[i].type == MT_BLANK) + continue; + int y = 32*i; + uint16_t bg = 0xffff; + if (i == selection) + bg = 0x7777; + ili9341_fill(320-60, y, 60, 30, bg); + ili9341_drawstring_5x7(menu[i].label, 320-54, y+12, 0x0000, bg); + } +} + const char *menu_items[] = { "OPEN", "SHORT", "LOAD", "ISOLN", "THRU", NULL, "DONE" }; @@ -169,6 +340,9 @@ const char *menu_items[] = { void draw_buttons(void) { +#if 1 + draw_menu_buttons(menu_stack[menu_current_level]); +#else int i = 0; for (i = 0; i < 7; i++) { int y = 32*i; @@ -180,6 +354,7 @@ draw_buttons(void) ili9341_drawstring_5x7(menu_items[i], 320-54, y+12, 0x0000, bg); } } +#endif } void @@ -216,12 +391,13 @@ ui_process(void) int n; if (status != 0) { if (status & EVT_BUTTON_SINGLE_CLICK) { - //markers[active_marker].index = 30; - ui_status = !ui_status; - if (ui_status) + if (ui_status) { + menu_invoke(selection); + } else { + ui_status = TRUE; + } + if (ui_status) ui_show(); - else - ui_hide(); } else { if (ui_status) { if (status & EVT_UP) {