diff --git a/main.c b/main.c index a69a434..dfba1fb 100644 --- a/main.c +++ b/main.c @@ -834,6 +834,9 @@ void set_sweep_frequency(int type, int32_t freq) { int cal_applied = cal_status & CALSTAT_APPLY; + // negative value indicate overflow, do nothing + if (freq < 0) + return; switch (type) { case ST_START: freq_mode_startstop(); diff --git a/ui.c b/ui.c index aa38228..8c9bb71 100644 --- a/ui.c +++ b/ui.c @@ -71,9 +71,14 @@ enum { KM_START, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR, KM_SCALEDELAY }; +enum { + LM_MARKER, LM_SEARCH, LM_CENTER, LM_SPAN +}; + uint8_t ui_mode = UI_NORMAL; uint8_t keypad_mode; int8_t selection = 0; +uint8_t lever_mode = LM_MARKER; typedef struct { uint8_t type; @@ -774,6 +779,7 @@ menu_stimulus_cb(int item) ui_mode_keypad(item); ui_process_keypad(); } + lever_mode = item == 3 ? LM_SPAN : LM_CENTER; break; case 5: /* PAUSE */ toggle_sweep(); @@ -867,6 +873,7 @@ menu_marker_search_cb(int item) break; } redraw_marker(active_marker, TRUE); + lever_mode = LM_SEARCH; } void @@ -911,6 +918,7 @@ menu_marker_sel_cb(int item) } redraw_marker(active_marker, TRUE); draw_menu(); + lever_mode = LM_MARKER; } const menuitem_t menu_calop[] = { @@ -1665,6 +1673,93 @@ ui_mode_normal(void) ui_mode = UI_NORMAL; } +static void +lever_move_marker(int status) +{ + do { + if (active_marker >= 0 && markers[active_marker].enabled) { + if ((status & EVT_DOWN) && markers[active_marker].index > 0) { + markers[active_marker].index--; + markers[active_marker].frequency = frequencies[markers[active_marker].index]; + redraw_marker(active_marker, FALSE); + } + if ((status & EVT_UP) && markers[active_marker].index < 100) { + markers[active_marker].index++; + markers[active_marker].frequency = frequencies[markers[active_marker].index]; + redraw_marker(active_marker, FALSE); + } + } + status = btn_wait_release(); + } while (status != 0); + if (active_marker >= 0) + redraw_marker(active_marker, TRUE); +} + +static void +lever_search_marker(int status) +{ + if (active_marker >= 0) { + if (status & EVT_DOWN) { + int i = marker_search_left(markers[active_marker].index); + if (i != -1) + markers[active_marker].index = i; + } else if (status & EVT_UP) { + int i = marker_search_right(markers[active_marker].index); + if (i != -1) + markers[active_marker].index = i; + } + redraw_marker(active_marker, TRUE); + } +} + +// ex. 10942 -> 10000 +// 6791 -> 5000 +// 341 -> 200 +static uint32_t +step_round(uint32_t v) +{ + // decade step + uint32_t x = 1; + for (x = 1; x * 10 < v; x *= 10) + ; + + // 1-2-5 step + if (x * 2 > v) + return x; + else if (x * 5 > v) + return x * 2; + else + return x * 5; +} + +static void +lever_zoom_span(int status) +{ + if (status & EVT_UP) { + uint32_t span = get_sweep_frequency(ST_SPAN); + span = step_round(span - 1); + set_sweep_frequency(ST_SPAN, span); + } else if (status & EVT_DOWN) { + uint32_t span = get_sweep_frequency(ST_SPAN); + span = step_round(span); + span = step_round(span * 3); + set_sweep_frequency(ST_SPAN, span); + } +} + +static void +lever_move_center(int status) +{ + uint32_t center = get_sweep_frequency(ST_CENTER); + uint32_t span = get_sweep_frequency(ST_SPAN); + span = step_round(span / 3); + if (status & EVT_UP) { + set_sweep_frequency(ST_CENTER, center + span); + } else if (status & EVT_DOWN) { + set_sweep_frequency(ST_CENTER, center - span); + } +} + static void ui_process_normal(void) { @@ -1673,23 +1768,12 @@ ui_process_normal(void) if (status & EVT_BUTTON_SINGLE_CLICK) { ui_mode_menu(); } else { - do { - if (active_marker >= 0 && markers[active_marker].enabled) { - if ((status & EVT_DOWN) && markers[active_marker].index > 0) { - markers[active_marker].index--; - markers[active_marker].frequency = frequencies[markers[active_marker].index]; - redraw_marker(active_marker, FALSE); - } - if ((status & EVT_UP) && markers[active_marker].index < 100) { - markers[active_marker].index++; - markers[active_marker].frequency = frequencies[markers[active_marker].index]; - redraw_marker(active_marker, FALSE); - } - } - status = btn_wait_release(); - } while (status != 0); - if (active_marker >= 0) - redraw_marker(active_marker, TRUE); + switch (lever_mode) { + case LM_MARKER: lever_move_marker(status); break; + case LM_SEARCH: lever_search_marker(status); break; + case LM_CENTER: lever_move_center(status); break; + case LM_SPAN: lever_zoom_span(status); break; + } } } }