diff --git a/main.c b/main.c index 7114d98..d9001b1 100644 --- a/main.c +++ b/main.c @@ -673,6 +673,36 @@ set_sweep_frequency(int type, float frequency) } } +uint32_t +get_sweep_frequency(int type) +{ + uint32_t result = 0; + switch (type) { + case ST_START: + freq_mode_startstop(); + result = frequency0; + break; + case ST_STOP: + freq_mode_startstop(); + result = frequency1; + break; + case ST_CENTER: + freq_mode_centerspan(); + result = frequency0; + break; + case ST_SPAN: + freq_mode_centerspan(); + result = -frequency1; + break; + case ST_CW: + freq_mode_centerspan(); + result = frequency0; + break; + } + return result; +} + + static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[]) { if (argc == 0) { diff --git a/nanovna.h b/nanovna.h index adf395b..940525d 100644 --- a/nanovna.h +++ b/nanovna.h @@ -57,6 +57,7 @@ enum { }; void set_sweep_frequency(int type, float frequency); +uint32_t get_sweep_frequency(int type); float my_atof(const char *p); @@ -211,6 +212,7 @@ void request_to_redraw_grid(void); void redraw_frame(void); //void redraw_all(void); void request_to_draw_cells_behind_menu(void); +void request_to_draw_cells_behind_numeric_input(void); void redraw_marker(int marker, int update_info); void trace_get_info(int t, char *buf, int len); void plot_into_index(float measured[2][101][2]); @@ -289,7 +291,7 @@ extern uint8_t previous_marker; #define cal_status current_props._cal_status #define frequencies current_props._frequencies #define cal_data active_props->_cal_data -#define electrical_delay active_props->_electrical_delay +#define electrical_delay current_props._electrical_delay #define trace current_props._trace #define markers current_props._markers @@ -309,8 +311,10 @@ void clear_all_config_prop_data(void); */ typedef struct { - int digit; /* 0~5 */ - int current_trace; /* 0..3 */ + int8_t digit; /* 0~5 */ + int8_t digit_mode; + int8_t current_trace; /* 0..3 */ + uint32_t freq; } uistat_t; extern uistat_t uistat; diff --git a/plot.c b/plot.c index f24db8a..3ee50fb 100644 --- a/plot.c +++ b/plot.c @@ -1193,8 +1193,17 @@ request_to_draw_cells_behind_menu(void) { int n, m; for (m = 7; m <= 9; m++) - for (n = 0; n < (area_height+CELLHEIGHT-1) / CELLHEIGHT; n++) - //draw_cell(m, n); + for (n = 0; n < 8; n++) + mark_map(m, n); + redraw_requested = TRUE; +} + +void +request_to_draw_cells_behind_numeric_input(void) +{ + int n, m; + for (m = 0; m <= 9; m++) + for (n = 6; n < 8; n++) mark_map(m, n); redraw_requested = TRUE; } diff --git a/ui.c b/ui.c index 2ff4b25..3e39004 100644 --- a/ui.c +++ b/ui.c @@ -25,7 +25,10 @@ #include -uistat_t uistat; +uistat_t uistat = { + digit: 6, + current_trace: 0 +}; @@ -53,6 +56,7 @@ uistat_t uistat; static uint16_t last_button = 0b0000; static uint32_t last_button_down_ticks; static uint32_t last_button_repeat_ticks; +static int8_t inhibit_until_release = FALSE; enum { OP_NONE = 0, OP_LEVER, OP_TOUCH }; uint8_t operation_requested = OP_NONE; @@ -60,7 +64,7 @@ uint8_t operation_requested = OP_NONE; uint8_t previous_marker = 0; enum { - UI_NORMAL, UI_MENU, UI_KEYPAD + UI_NORMAL, UI_MENU, UI_NUMERIC, UI_KEYPAD }; enum { @@ -102,8 +106,10 @@ int8_t kp_index = 0; void ui_mode_normal(void); void ui_mode_menu(void); +void ui_mode_numeric(int _keypad_mode); void ui_mode_keypad(int _keypad_mode); void draw_menu(void); +void leave_ui_mode(void); void erase_menu_buttons(void); void ui_process_keypad(void); @@ -118,12 +124,17 @@ static int btn_check(void) int status = 0; uint32_t ticks = chVTGetSystemTime(); if (changed & (1<= BUTTON_DEBOUNCE_TICKS) { - // button pushed - status |= EVT_BUTTON_SINGLE_CLICK; + if (ticks - last_button_down_ticks >= BUTTON_DEBOUNCE_TICKS) { + if (cur_button & (1<= BUTTON_DOWN_LONG_TICKS) { + inhibit_until_release = TRUE; + return EVT_BUTTON_DOWN_LONG; + } + if ((changed & (1<= '0' && buf[i] <= '9') - c = buf[i] - '0'; - else { - i++; - continue; + else if (c >= '0' && c <= '9') + c = c - '0'; + else + c = -1; + + if (uistat.digit == 8-i) { + fg = RGB565(128,255,128); + focused = TRUE; + if (uistat.digit_mode) + bg = 0x0000; + } + + if (c >= 0) + ili9341_drawfont(c, &NF20x24, x, 208+4, fg, bg); + else if (focused) + ili9341_drawfont(0, &NF20x24, x, 208+4, fg, bg); + else + ili9341_fill(x, 208+4, 20, 24, bg); + + x += 20; + if (xsim[i] > 0) { + //ili9341_fill(x, 208+4, xsim[i], 20, bg); + x += xsim[i]; } - ili9341_drawfont(c, &NF20x24, i * 20 + 64, 208+4, 0x0000, 0xffff); - i++; } } @@ -1123,6 +1174,102 @@ erase_menu_buttons(void) ili9341_fill(320-60, 0, 60, 32*7, bg); } +void +erase_numeric_input(void) +{ + uint16_t bg = 0; + ili9341_fill(0, 240-32, 320, 32, bg); +} + +void +leave_ui_mode() +{ + if (ui_mode == UI_MENU) { + request_to_draw_cells_behind_menu(); + erase_menu_buttons(); + } else if (ui_mode == UI_NUMERIC) { + request_to_draw_cells_behind_numeric_input(); + erase_numeric_input(); + draw_frequencies(); + } +} + +void +fetch_numeric_target(void) +{ + uint32_t value; + switch (keypad_mode) { + case KM_START: + value = get_sweep_frequency(ST_START); + break; + case KM_STOP: + value = get_sweep_frequency(ST_STOP); + break; + case KM_CENTER: + value = get_sweep_frequency(ST_CENTER); + break; + case KM_SPAN: + value = get_sweep_frequency(ST_SPAN); + break; + case KM_CW: + value = get_sweep_frequency(ST_CW); + break; +#if 0 + case KM_SCALE: + value = get_trace_scale(uistat.current_trace); + break; + case KM_REFPOS: + value = get_trace_refpos(uistat.current_trace); + break; + case KM_EDELAY: + value = get_electrical_delay(); + break; +#endif + } + uistat.freq = value; +} + +void set_numeric_value(void) +{ + switch (keypad_mode) { + case KM_START: + set_sweep_frequency(ST_START, uistat.freq); + break; + case KM_STOP: + set_sweep_frequency(ST_STOP, uistat.freq); + break; + case KM_CENTER: + set_sweep_frequency(ST_CENTER, uistat.freq); + break; + case KM_SPAN: + set_sweep_frequency(ST_SPAN, uistat.freq); + break; + case KM_CW: + set_sweep_frequency(ST_CW, uistat.freq); + break; +#if 0 + case KM_SCALE: + set_trace_scale(uistat.current_trace, xxx); + break; + case KM_REFPOS: + set_trace_refpos(uistat.current_trace, xxx); + break; + case KM_EDELAY: + set_electrical_delay(xxx); + break; +#endif + } +} + +void +draw_numeric_area(void) +{ + char buf[10]; + chsnprintf(buf, sizeof buf, "%9d", uistat.freq); + draw_numeric_input(buf); +} + + void ui_mode_menu(void) { @@ -1137,6 +1284,25 @@ ui_mode_menu(void) draw_menu(); } +void +ui_mode_numeric(int _keypad_mode) +{ + if (ui_mode == UI_NUMERIC) + return; + + leave_ui_mode(); + + // keypads array + keypad_mode = _keypad_mode; + ui_mode = UI_NUMERIC; + area_width = AREA_WIDTH_NORMAL; + area_height = 240-32;//HEIGHT - 32; + + draw_numeric_area_frame(); + fetch_numeric_target(); + draw_numeric_area(); +} + void ui_mode_keypad(int _keypad_mode) { @@ -1153,7 +1319,7 @@ ui_mode_keypad(int _keypad_mode) ui_mode = UI_KEYPAD; area_width = AREA_WIDTH_NORMAL - (64-8); - area_height = HEIGHT; + area_height = HEIGHT - 32; draw_menu(); draw_keypad(); draw_numeric_input(""); @@ -1165,11 +1331,10 @@ ui_mode_normal(void) if (ui_mode == UI_NORMAL) return; - ui_mode = UI_NORMAL; area_width = AREA_WIDTH_NORMAL; area_height = HEIGHT; - erase_menu_buttons(); - request_to_draw_cells_behind_menu(); + leave_ui_mode(); + ui_mode = UI_NORMAL; } void @@ -1312,6 +1477,59 @@ keypad_apply_touch(void) return -1; } + +void +ui_process_numeric(void) +{ + int status = btn_check(); + if (status != 0) { + if (status == EVT_BUTTON_SINGLE_CLICK) { + status = btn_wait_release(); + if (uistat.digit_mode) { + if (status & (EVT_BUTTON_SINGLE_CLICK | EVT_BUTTON_DOWN_LONG)) { + uistat.digit_mode = FALSE; + draw_numeric_area(); + } + } else { + if (status & EVT_BUTTON_DOWN_LONG) { + uistat.digit_mode = TRUE; + draw_numeric_area(); + } else if (status & EVT_BUTTON_SINGLE_CLICK) { + set_numeric_value(); + ui_mode_normal(); + } + } + } else { + do { + if (uistat.digit_mode) { + if (status & EVT_DOWN && uistat.digit < 8) { + uistat.digit++; + draw_numeric_area(); + } + if (status & EVT_UP && uistat.digit > 0) { + uistat.digit--; + draw_numeric_area(); + } + } else { + int32_t step = 1; + int n; + for (n = uistat.digit; n > 0; n--) + step *= 10; + if (status & EVT_DOWN) { + uistat.freq += step; + draw_numeric_area(); + } + if (status & EVT_UP) { + uistat.freq -= step; + draw_numeric_area(); + } + } + status = btn_wait_release(); + } while (status != 0); + } + } +} + void ui_process_keypad(void) { @@ -1374,6 +1592,9 @@ ui_process_lever(void) case UI_MENU: ui_process_menu(); break; + case UI_NUMERIC: + ui_process_numeric(); + break; case UI_KEYPAD: ui_process_keypad(); break;