From b909ccb716b9db46e5b70cea502887ae22bd4e2e Mon Sep 17 00:00:00 2001 From: TT Date: Sat, 9 Nov 2019 11:20:22 +0900 Subject: [PATCH 1/5] feat: add lever operations (center, span, search) --- main.c | 3 ++ ui.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 104 insertions(+), 17 deletions(-) diff --git a/main.c b/main.c index a69a434d..dfba1fba 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 aa38228a..8c9bb71a 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; + } } } } From c7af840f535455c52507d332021ec850004ef1a0 Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 17 Nov 2019 10:54:39 +0900 Subject: [PATCH 2/5] feat: add feedback of lever mode --- ili9341.c | 9 +++++++++ nanovna.h | 7 +++++++ plot.c | 22 +++++++++++++++------- ui.c | 53 ++++++++++++++++++++++++----------------------------- 4 files changed, 55 insertions(+), 36 deletions(-) diff --git a/ili9341.c b/ili9341.c index b551df84..99b4627b 100644 --- a/ili9341.c +++ b/ili9341.c @@ -360,6 +360,15 @@ ili9341_drawstring_5x7(const char *str, int x, int y, uint16_t fg, uint16_t bg) } } +void +ili9341_drawstring_5x7_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool invert) +{ + if (invert) + ili9341_drawstring_5x7(str, x, y, bg, fg); + else + ili9341_drawstring_5x7(str, x, y, fg, bg); +} + void ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size) { diff --git a/nanovna.h b/nanovna.h index 49dac8d7..f127bb49 100644 --- a/nanovna.h +++ b/nanovna.h @@ -274,6 +274,7 @@ void ili9341_bulk(int x, int y, int w, int h); void ili9341_fill(int x, int y, int w, int h, int color); void ili9341_drawchar_5x7(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg); void ili9341_drawstring_5x7(const char *str, int x, int y, uint16_t fg, uint16_t bg); +void ili9341_drawstring_5x7_inv(const char *str, int x, int y, uint16_t fg, uint16_t bg, bool inv); void ili9341_drawchar_size(uint8_t ch, int x, int y, uint16_t fg, uint16_t bg, uint8_t size); void ili9341_drawstring_size(const char *str, int x, int y, uint16_t fg, uint16_t bg, uint8_t size); void ili9341_drawfont(uint8_t ch, const font_t *font, int x, int y, uint16_t fg, uint16_t bg); @@ -341,12 +342,18 @@ void clear_all_config_prop_data(void); * ui.c */ +// lever_mode +enum { + LM_MARKER, LM_SEARCH, LM_CENTER, LM_SPAN +}; + typedef struct { int8_t digit; /* 0~5 */ int8_t digit_mode; int8_t current_trace; /* 0..3 */ uint32_t value; // for editing at numeric input area uint32_t previous_value; + uint8_t lever_mode; } uistat_t; extern uistat_t uistat; diff --git a/plot.c b/plot.c index 354a842d..6ef28d7b 100644 --- a/plot.c +++ b/plot.c @@ -1535,7 +1535,7 @@ cell_draw_marker_info(int m, int n, int w, int h) ypos -= n * CELLHEIGHT; chsnprintf(buf, sizeof buf, "%d:", active_marker + 1); xpos += 5; - cell_drawstring_5x7(w, h, buf, xpos, ypos, 0xffff); + cell_drawstring_invert_5x7(w, h, buf, xpos, ypos, 0xffff, uistat.lever_mode == LM_MARKER); xpos += 14; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { frequency_string(buf, sizeof buf, frequencies[idx]); @@ -1610,14 +1610,22 @@ draw_frequencies(void) } else if (frequency1 < 0) { int fcenter = frequency0; int fspan = -frequency1; - strcpy(buf, "CENTER "); - frequency_string(buf+7, 24-7, fcenter); + int x = OFFSETX; + strcpy(buf, "CENTER"); + ili9341_drawstring_5x7_inv(buf, x, 233, 0xffff, 0x0000, uistat.lever_mode == LM_CENTER); + x += 5 * 6; + strcpy(buf, " "); + frequency_string(buf+1, 24-1, fcenter); strcat(buf, " "); - ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000); - strcpy(buf, "SPAN "); - frequency_string(buf+5, 24-5, fspan); + ili9341_drawstring_5x7(buf, x, 233, 0xffff, 0x0000); + x = 205; + strcpy(buf, "SPAN"); + ili9341_drawstring_5x7_inv(buf, x, 233, 0xffff, 0x0000, uistat.lever_mode == LM_SPAN); + x += 5 * 4; + strcpy(buf, " "); + frequency_string(buf+1, 24-1, fspan); strcat(buf, " "); - ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000); + ili9341_drawstring_5x7(buf, x, 233, 0xffff, 0x0000); } else { int fcenter = frequency0; chsnprintf(buf, 24, "CW %d.%03d %03d MHz ", diff --git a/ui.c b/ui.c index 8c9bb71a..41215590 100644 --- a/ui.c +++ b/ui.c @@ -28,7 +28,8 @@ uistat_t uistat = { digit: 6, - current_trace: 0 + current_trace: 0, + lever_mode: LM_MARKER }; @@ -71,14 +72,9 @@ 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; @@ -771,6 +767,7 @@ menu_stimulus_cb(int item) case 2: /* CENTER */ case 3: /* SPAN */ case 4: /* CW */ + uistat.lever_mode = item == 3 ? LM_SPAN : LM_CENTER; status = btn_wait_release(); if (status & EVT_BUTTON_DOWN_LONG) { ui_mode_numeric(item); @@ -779,7 +776,6 @@ 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(); @@ -820,22 +816,22 @@ menu_marker_op_cb(int item) break; case 3: /* MARKERS->SPAN */ { - if (previous_marker == active_marker) - return; - int32_t freq2 = get_marker_frequency(previous_marker); - if (freq2 < 0) - return; - if (freq > freq2) { - freq2 = freq; - freq = get_marker_frequency(previous_marker); + if (previous_marker == -1 || active_marker == previous_marker) { + int32_t center = get_sweep_frequency(ST_CENTER); + int32_t span = center - freq; + if (span < 0) span = -span; + set_sweep_frequency(ST_SPAN, span * 2); + } else { + int32_t freq2 = get_marker_frequency(previous_marker); + if (freq2 < 0) + return; + if (freq > freq2) { + freq2 = freq; + freq = get_marker_frequency(previous_marker); + } + set_sweep_frequency(ST_START, freq); + set_sweep_frequency(ST_STOP, freq2); } - set_sweep_frequency(ST_START, freq); - set_sweep_frequency(ST_STOP, freq2); -#if 0 - int32_t span = (freq - freq2) * 2; - if (span < 0) span = -span; - set_sweep_frequency(ST_SPAN, span); -#endif } break; } @@ -873,7 +869,7 @@ menu_marker_search_cb(int item) break; } redraw_marker(active_marker, TRUE); - lever_mode = LM_SEARCH; + uistat.lever_mode = LM_SEARCH; } void @@ -918,7 +914,7 @@ menu_marker_sel_cb(int item) } redraw_marker(active_marker, TRUE); draw_menu(); - lever_mode = LM_MARKER; + uistat.lever_mode = LM_MARKER; } const menuitem_t menu_calop[] = { @@ -1720,7 +1716,7 @@ step_round(uint32_t v) { // decade step uint32_t x = 1; - for (x = 1; x * 10 < v; x *= 10) + for (x = 1; x*10 < v; x *= 10) ; // 1-2-5 step @@ -1735,13 +1731,12 @@ step_round(uint32_t v) static void lever_zoom_span(int status) { + uint32_t span = get_sweep_frequency(ST_SPAN); 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 + 1); span = step_round(span * 3); set_sweep_frequency(ST_SPAN, span); } @@ -1768,7 +1763,7 @@ ui_process_normal(void) if (status & EVT_BUTTON_SINGLE_CLICK) { ui_mode_menu(); } else { - switch (lever_mode) { + switch (uistat.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; From dd4eaed4752130e7c9fa50ac39cd8fb2051b4ef2 Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 17 Nov 2019 11:16:08 +0900 Subject: [PATCH 3/5] fix: change to marker lever mode on transform enabled --- main.c | 1 + ui.c | 1 + 2 files changed, 2 insertions(+) diff --git a/main.c b/main.c index dfba1fba..623bb5f9 100644 --- a/main.c +++ b/main.c @@ -1779,6 +1779,7 @@ set_domain_mode(int mode) // accept DOMAIN_FREQ or DOMAIN_TIME if (mode != (domain_mode & DOMAIN_MODE)) { domain_mode = (domain_mode & ~DOMAIN_MODE) | (mode & DOMAIN_MODE); redraw_request |= REDRAW_FREQUENCY; + uistat.lever_mode = LM_MARKER; } } diff --git a/ui.c b/ui.c index 41215590..7632d778 100644 --- a/ui.c +++ b/ui.c @@ -699,6 +699,7 @@ menu_transform_cb(int item) } else { domain_mode = (domain_mode & ~DOMAIN_MODE) | DOMAIN_TIME; } + uistat.lever_mode = LM_MARKER; draw_frequencies(); ui_mode_normal(); break; From 3114df7fe912f207f291ca490ce55b791bac0dfe Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 17 Nov 2019 11:16:36 +0900 Subject: [PATCH 4/5] fix #92 : 'for' loop initial declarations --- fft.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fft.h b/fft.h index 9f8c44cb..0c367b72 100644 --- a/fft.h +++ b/fft.h @@ -28,7 +28,8 @@ static uint16_t reverse_bits(uint16_t x, int n) { uint16_t result = 0; - for (int i = 0; i < n; i++, x >>= 1) + int i; + for (i = 0; i < n; i++, x >>= 1) result = (result << 1) | (x & 1U); return result; } @@ -43,8 +44,10 @@ static void fft256(float array[][2], const uint8_t dir) { const uint8_t real = dir & 1; const uint8_t imag = ~real & 1; + uint16_t i; + uint16_t size; - for (uint16_t i = 0; i < n; i++) { + for (i = 0; i < n; i++) { uint16_t j = reverse_bits(i, levels); if (j > i) { float temp = array[i][real]; @@ -57,11 +60,13 @@ static void fft256(float array[][2], const uint8_t dir) { } // Cooley-Tukey decimation-in-time radix-2 FFT - for (uint16_t size = 2; size <= n; size *= 2) { + for (size = 2; size <= n; size *= 2) { uint16_t halfsize = size / 2; uint16_t tablestep = n / size; - for (uint16_t i = 0; i < n; i += size) { - for (uint16_t j = i, k = 0; j < i + halfsize; j++, k += tablestep) { + uint16_t i; + for (i = 0; i < n; i += size) { + uint16_t j, k; + for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) { uint16_t l = j + halfsize; float tpre = array[l][real] * cos(2 * M_PI * k / 256) + array[l][imag] * sin(2 * M_PI * k / 256); float tpim = -array[l][real] * sin(2 * M_PI * k / 256) + array[l][imag] * cos(2 * M_PI * k / 256); From ed85ca193b4ea5ed724606af62745c7b00b1f4cc Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 17 Nov 2019 11:28:50 +0900 Subject: [PATCH 5/5] fix: make touch cal default suitable for 2.8 panel --- main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 623bb5f9..6d52c881 100644 --- a/main.c +++ b/main.c @@ -609,7 +609,8 @@ config_t config = { .menu_normal_color = 0xffff, .menu_active_color = 0x7777, .trace_color = { RGB565(0,255,255), RGB565(255,0,40), RGB565(0,0,255), RGB565(50,255,0) }, - .touch_cal = { 693, 605, 124, 171 }, //{ 620, 600, 160, 190 }, +// .touch_cal = { 693, 605, 124, 171 }, // 2.4 inch LCD panel + .touch_cal = { 338, 522, 153, 192 }, // 2.8 inch LCD panel .default_loadcal = 0, .harmonic_freq_threshold = 300000000, .checksum = 0