diff --git a/main.c b/main.c index 0049333..9152c5f 100644 --- a/main.c +++ b/main.c @@ -194,9 +194,7 @@ static THD_FUNCTION(Thread1, arg) if (uistat.marker_tracking) { int i = marker_search_max(active_marker); if (i != -1 && active_marker != MARKER_INVALID) { - markers[active_marker].index = i; - markers[active_marker].frequency = getFrequency(i); - + set_marker_index(active_marker, i); redraw_request |= REDRAW_MARKER; } } @@ -1066,7 +1064,7 @@ VNA_SHELL_FUNCTION(cmd_hop) #endif static void -update_marker_index(void) +update_markers_index(void) { int m, idx; freq_t fstart = get_sweep_frequency(ST_START); @@ -1092,11 +1090,18 @@ update_marker_index(void) idx = r * (sweep_points-1); #endif } - markers[m].index = idx; - markers[m].frequency = getFrequency(idx); + set_marker_index(m, idx); } } +void +set_marker_index(int m, int16_t idx) +{ + if ((uint32_t)m >= MARKERS_MAX || (uint16_t)idx >= sweep_points) return; + markers[m].index = idx; + markers[m].frequency = getFrequency(idx); +} + void set_marker_frequency(int m, freq_t f) { if (m == MARKER_INVALID || !markers[m].enabled) @@ -1182,7 +1187,7 @@ update_frequencies(void) set_frequencies(start, stop, sweep_points); // operation_requested|= OP_FREQCHANGE; - update_marker_index(); + update_markers_index(); // set grid layout update_grid(); @@ -1491,8 +1496,7 @@ VNA_SHELL_FUNCTION(cmd_marker) case 2: markers[t].enabled = TRUE; active_marker = t; int i = marker_search_max(active_marker); if (i == -1) i = 0; - markers[active_marker].index = i; - markers[active_marker].frequency = getFrequency(i); + set_marker_index(active_marker, i); goto display_marker; default: // select active marker and move to index or frequency @@ -1502,10 +1506,8 @@ VNA_SHELL_FUNCTION(cmd_marker) active_marker = t; if (value > sweep_points) set_marker_frequency(active_marker, value); - else { - markers[t].index = value; - markers[t].frequency = getFrequency(value); - } + else + set_marker_index(t, value); return; } usage: diff --git a/nanovna.h b/nanovna.h index 95f66b8..bd4d319 100644 --- a/nanovna.h +++ b/nanovna.h @@ -205,6 +205,7 @@ void send_buffer(uint8_t * buf, int s); #endif void set_marker_frequency(int m, freq_t f); void set_marker_time(int m, float f); +void set_marker_index(int m, int16_t idx); void toggle_sweep(void); void toggle_mute(void); void toggle_pulse(void); diff --git a/sa_core.c b/sa_core.c index 4f32dbe..f9392c1 100644 --- a/sa_core.c +++ b/sa_core.c @@ -4478,8 +4478,7 @@ static volatile int dummy; } while (m < MARKERS_MAX) { // Insufficient maxima found if (markers[m].enabled && markers[m].mtype & M_TRACKING) { // More available markers found - markers[m].index = 0; // Enabled but no max so set to left most frequency - markers[m].frequency = getFrequency(0); + set_marker_index(m, 0); // Enabled but no max so set to left most frequency } m++; // Try next marker } @@ -4517,35 +4516,28 @@ static volatile int dummy; if (r < l) { l = markers[1].index; r = markers[0].index; - markers[0].index = l; - markers[1].index = r; } - freq_t lf = getFrequency(l); - freq_t rf = getFrequency(r); - markers[0].frequency = lf; - markers[1].frequency = rf; - + set_marker_index(0, l); + set_marker_index(1, r); + freq_t lf = markers[0].frequency; + freq_t rf = markers[1].frequency; markers[2].enabled = search_maximum(2, lf - (rf - lf), 12); markers[3].enabled = search_maximum(3, rf + (rf - lf), 12); } else if (setting.measurement == M_PHASE_NOISE && markers[0].index > 10) { // ------------Phase noise measurement - markers[1].index = markers[0].index + (setting.mode == M_LOW ? WIDTH/4 : -WIDTH/4); // Position phase noise marker at requested offset - markers[1].frequency = getFrequency(markers[1].index); + // Position phase noise marker at requested offset + set_marker_index(1, markers[0].index + (setting.mode == M_LOW ? WIDTH/4 : -WIDTH/4)); } else if ((setting.measurement == M_PASS_BAND || setting.measurement == M_FM) && markers[0].index > 10) { // ----------------Pass band measurement int t = 0; float v = actual_t[markers[0].index] - (in_selftest ? 6.0 : 3.0); while (t < markers[0].index && actual_t[t+1] < v) // Find left -3dB point t++; - if (t< markers[0].index) { - markers[1].index = t; - markers[1].frequency = getFrequency(t); - } + if (t< markers[0].index) + set_marker_index(1, t); t = setting._sweep_points-1;; while (t > markers[0].index && actual_t[t-1] < v) // find right -3dB point t--; - if (t > markers[0].index) { - markers[2].index = t; - markers[2].frequency = getFrequency(t); - } + if (t > markers[0].index) + set_marker_index(2, t); } else if (setting.measurement == M_AM) { // ----------------AM measurement if (S_IS_AUTO(setting.agc )) { #ifdef __SI4432__ diff --git a/ui_sa.c b/ui_sa.c index 1142b86..77767f9 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -1563,7 +1563,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_marker_select_acb) } markers[data-1].enabled = true; // interpolate_maximum(data-1); // possibly not a maximum - markers[data-1].frequency = getFrequency(markers[data-1].index); + set_marker_index(data-1, markers[data-1].index); active_marker_select(data-1); menu_push_submenu(menu_marker_modify); redraw_marker(active_marker); @@ -1649,7 +1649,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_marker_ref_select_acb) } markers[data-1].enabled = true; // interpolate_maximum(data-1); // possibly not a maximum - markers[data-1].frequency = getFrequency(markers[data-1].index); + set_marker_index(data-1, markers[data-1].index); markers[active_marker].ref = data-1; redraw_marker(active_marker); menu_move_back(false);