From 0a03ff0a2ff5e8dd216a49e638d3089a411eac8e Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Wed, 1 Apr 2020 11:07:18 +0200 Subject: [PATCH] Added full marker tracking --- ili9341.c | 4 +- main.c | 8 +-- nanovna.h | 11 +++- sa_core.c | 155 +++++++++++++++++++++++++++++------------------------- ui.c | 10 ++-- ui_sa.c | 18 +++++-- 6 files changed, 120 insertions(+), 86 deletions(-) diff --git a/ili9341.c b/ili9341.c index 0cd5ad2..f423b19 100644 --- a/ili9341.c +++ b/ili9341.c @@ -524,7 +524,7 @@ void ili9341_clear_screen(void) { ili9341_fill(0, 0, ILI9341_WIDTH, ILI9341_HEIGHT, background_color); } - +#if 0 void ili9341_set_foreground(uint16_t fg) { foreground_color = fg; @@ -534,7 +534,7 @@ void ili9341_set_background(uint16_t bg) { background_color = bg; } - +#endif void ili9341_set_rotation(uint8_t r) { // static const uint8_t rotation_const[]={DISPLAY_ROTATION_0, DISPLAY_ROTATION_90, diff --git a/main.c b/main.c index 460245e..51a7e6e 100644 --- a/main.c +++ b/main.c @@ -813,10 +813,10 @@ static const trace_t def_trace[TRACES_MAX] = {//enable, type, channel, reserved, }; static const marker_t def_markers[MARKERS_MAX] = { - { 1, M_REFERENCE, 30, 0 }, - { 0, M_NORMAL, 40, 0 }, - { 0, M_NORMAL, 60, 0 }, - { 0, M_NORMAL, 80, 0 } + { M_TRACKING_ENABLED, M_REFERENCE, 30, 0 }, + { M_DISABLED, M_NORMAL, 40, 0 }, + { M_DISABLED, M_NORMAL, 60, 0 }, + { M_DISABLED, M_NORMAL, 80, 0 } }; // Load propeties default settings diff --git a/nanovna.h b/nanovna.h index fd34c62..a299dd5 100644 --- a/nanovna.h +++ b/nanovna.h @@ -316,7 +316,11 @@ float groupdelay_from_array(int i, float array[POINTS_COUNT][2]); #endif // marker enum { - M_REFERENCE, M_NORMAL, M_DELTA + M_REFERENCE, M_NORMAL, M_DELTA, M_TRACKING +}; + +enum { + M_DISABLED, M_ENABLED, M_TRACKING_ENABLED }; typedef struct { @@ -404,8 +408,13 @@ void ili9341_init(void); void ili9341_test(int mode); void ili9341_bulk(int x, int y, int w, int h); void ili9341_fill(int x, int y, int w, int h, int color); +#if 0 void ili9341_set_foreground(uint16_t fg); void ili9341_set_background(uint16_t fg); +#else +#define ili9341_set_foreground(fg) { foreground_color = fg; } +#define ili9341_set_background(bg) { background_color = bg;} +#endif void ili9341_clear_screen(void); void blit8BitWidthBitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *bitmap); void ili9341_drawchar(uint8_t ch, int x, int y); diff --git a/sa_core.c b/sa_core.c index 646bf2e..799002d 100644 --- a/sa_core.c +++ b/sa_core.c @@ -568,10 +568,16 @@ float perform(bool break_on_operation, int i, int32_t f, int tracking) return(RSSI); } +#define MAX_MAX 4 +#define MAX_NOISE 20 // 10dB +int16_t max_index[MAX_MAX]; +int16_t cur_max = 0; + // main loop for measurement static bool sweep(bool break_on_operation) { float RSSI; + int16_t downslope = true; palClearPad(GPIOC, GPIOC_LED); temppeakLevel = -150; float temp_min_level = 100; @@ -580,7 +586,6 @@ static bool sweep(bool break_on_operation) for (int i = 0; i < sweep_points; i++) { RSSI = perform(break_on_operation, i, frequencies[i], setting_tracking); -//START_PROFILE // back to toplevel to handle ui operation if (operation_requested && break_on_operation) return false; @@ -618,6 +623,54 @@ static bool sweep(bool break_on_operation) case AV_16: actual_t[i] = (actual_t[i]*3 + RSSI) / 16.0; break; } } +#if 1 +// START_PROFILE + if (i == 0) { + cur_max = 0; // Always at least one maximum + temppeakIndex = 0; + temppeakLevel = actual_t[i]; + max_index[i] = 0; + downslope = true; + } + if (downslope) { + if (temppeakLevel > actual_t[i]) { // Follow down + temppeakIndex = i; // Latest minimum + temppeakLevel = actual_t[i]; + } else if (temppeakLevel + MAX_NOISE < actual_t[i]) { // Local minimum found + temppeakIndex = i; // This is now the latest maximum + temppeakLevel = actual_t[i]; + downslope = false; + } + } else { + if (temppeakLevel < actual_t[i]) { // Follow up + temppeakIndex = i; + temppeakLevel = actual_t[i]; + } else if (temppeakLevel - MAX_NOISE > actual_t[i]) { // Local max found + + int j = 0; // Insertion index + while (j= temppeakLevel) // Find where to insert + j++; + if (j < MAX_MAX) { // Larger then one of the previous found + int k = MAX_MAX-1; + while (k > j) { // Shift to make room for max + max_index[k] = max_index[k-1]; +// maxlevel_index[k] = maxlevel_index[k-1]; // Only for debugging + k--; + } + max_index[j] = temppeakIndex; +// maxlevel_index[j] = actual_t[temppeakIndex]; // Only for debugging + if (cur_max < MAX_MAX) { + cur_max++; + } +//STOP_PROFILE + } + temppeakIndex = i; // Latest minimum + temppeakLevel = actual_t[i]; + + downslope = true; + } + } +#else if (frequencies[i] > 1000000) { if (temppeakLevel < actual_t[i]) { temppeakIndex = i; @@ -626,7 +679,7 @@ static bool sweep(bool break_on_operation) } if (temp_min_level > actual_t[i]) temp_min_level = actual_t[i]; -//STOP_PROFILE +#endif } // if (setting_spur == 1) { // setting_spur = -1; @@ -638,9 +691,37 @@ static bool sweep(bool break_on_operation) scandirty = false; draw_cal_status(); } - peakIndex = temppeakIndex; +#if 1 + int i = 0; + int m = 0; + while (i < cur_max) { // For all maxima found + while (m < MARKERS_MAX) { + if (markers[m].enabled == M_TRACKING_ENABLED) { // Available marker found + markers[m].index = max_index[i]; + markers[m].frequency = frequencies[markers[m].index]; + m++; + break; // Next maximum + } + m++; // Try next marker + } + i++; + } + while (m < MARKERS_MAX) { + if (markers[m].enabled == M_TRACKING_ENABLED ) { // More available markers found + markers[m].index = 0; // Enabled but no max + markers[m].frequency = frequencies[markers[m].index]; + } + m++; // Try next marker + } + peakIndex = max_index[0]; peakLevel = actual_t[peakIndex]; peakFreq = frequencies[peakIndex]; +#else + int peak_marker = 0; + markers[peak_marker].enabled = true; + markers[peak_marker].index = peakIndex; + markers[peak_marker].frequency = frequencies[markers[peak_marker].index]; +#endif min_level = temp_min_level; #if 0 // Auto ref level setting int scale = get_trace_scale(2); @@ -655,80 +736,12 @@ static bool sweep(bool break_on_operation) } #endif - int peak_marker = 0; - markers[peak_marker].enabled = true; - markers[peak_marker].index = peakIndex; - markers[peak_marker].frequency = frequencies[markers[peak_marker].index]; // redraw_marker(peak_marker, FALSE); palSetPad(GPIOC, GPIOC_LED); return true; } -#if 0 -void PeakSearch() -{ -#define PEAKSTACK 4 -#define PEAKDISTANCE 10 - int level = 0; - int searchLeft[PEAKSTACK]; - int peakIndex[PEAKSTACK]; - int peak_marker = 0; - searchLeft[level] = true; - peakIndex[level] = markers[peak_marker].index; - level++; - searchLeft[level] = true; - int peakFrom; - int peakTo; - while (peak_marker < 4){ - if (searchLeft[level]) - { - int fromLevel = level; - while (fromLevel > 0 && searchLeft[fromLevel]) - fromLevel-- - if(fromLevel == 0) { - peakFrom = PEAKDISTANCE; - } else { - peakFrom = peakIndex[fromLevel] + PEAKDISTANCE; - } - peakTo = peakIndex[level] - PEAKDISTANCE; - } else { - int toLevel = level; - while (toLevel > 0 && !searchLeft[toLevel]) - toLevel-- - if(toLevel == 0) { - peakTo = POINTS_COUNT - 1 - PEAKDISTANCE; - } else { - peakTo = peakIndex[fromLevel] - PEAKDISTANCE; - } - peakFrom = peakIndex[level] + PEAKDISTANCE; - } - float peakMax = actual_t[peakFrom]; - int peakIndex = peakFrom; - for (int i = peakFrom; i < peakTo; i++) { - if (peakMax < actual_t[i]) { - peakMax = actual_t[i]; - peakIndex = i; - } - } - - - peakIndex = temppeakIndex; - peakLevel = actual_t[peakIndex]; - peakFreq = frequencies[peakIndex]; - setting_spur = -setting_spur; - int peak_marker = 0; - markers[peak_marker].enabled = true; - markers[peak_marker].index = peakIndex; - markers[peak_marker].frequency = frequencies[markers[peak_marker].index]; -// redraw_marker(peak_marker, FALSE); - - -} - -} -#endif - const char *averageText[] = { "OFF", "MIN", "MAX", "2", "4", "8"}; const char *dBText[] = { "1dB/", "2dB/", "5dB/", "10dB/", "20dB/"}; const int refMHz[] = { 30, 15, 10, 4, 3, 2, 1 }; diff --git a/ui.c b/ui.c index 0f7de2f..3b9f54b 100644 --- a/ui.c +++ b/ui.c @@ -833,18 +833,18 @@ menu_marker_sel_cb(int item, uint8_t data) if (markers[item].enabled) { if (item == active_marker) { // disable if active trace is selected - markers[item].enabled = FALSE; + markers[item].enabled = M_DISABLED; active_marker_select(-1); } else { active_marker_select(item); } } else { - markers[item].enabled = TRUE; + markers[item].enabled = M_TRACKING_ENABLED; // default tracking enabled active_marker_select(item); } } else if (item == 4) { /* all off */ for (t = 0; t < MARKERS_MAX; t++) - markers[t].enabled = FALSE; + markers[t].enabled = M_DISABLED; previous_marker = -1; active_marker = -1; } else if (item == 5) { /* marker delta */ @@ -1334,8 +1334,8 @@ menu_is_multiline(const char *label, const char **l1, const char **l2); static void draw_numeric_area_frame(void) { - char *l1; - char *l2; + const char *l1; + const char *l2; ili9341_fill(0, 240-NUM_INPUT_HEIGHT, 320, NUM_INPUT_HEIGHT, config.menu_normal_color); ili9341_set_foreground(DEFAULT_MENU_TEXT_COLOR); ili9341_set_background(config.menu_normal_color); diff --git a/ui_sa.c b/ui_sa.c index 319b4ea..81631a0 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -216,9 +216,9 @@ void menu_autosettings_cb(int item, uint8_t data) active_marker = 0; for (int i = 1; i= 0 && markers[active_marker].enabled) { + if (item == 3 && markers[active_marker].enabled == M_TRACKING_ENABLED) + mark = true; + else if (item == markers[active_marker].mtype) + mark = true; } if (mark) { *bg = DEFAULT_MENU_TEXT_COLOR;