From 043972e6caf7c2e5376f6245d03455f06be65022 Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 17 Sep 2017 18:52:02 +0900 Subject: [PATCH] fix segv on active trace 4, apply error term inside sweep loop, show active trace on marker info --- main.c | 47 +++++++++++++++++++++++++++++++++++++++++---- nanovna.h | 18 ++++++++++++++---- plot.c | 57 ++++++++++++++++++++++++++++++++++++------------------- ui.c | 53 ++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 133 insertions(+), 42 deletions(-) diff --git a/main.c b/main.c index 1b4feb3..89a5035 100644 --- a/main.c +++ b/main.c @@ -34,6 +34,8 @@ #define ENABLED_DUMP static void apply_error_term(void); +static void apply_error_term_at(int i); + void sweep(void); static MUTEX_DECL(mutex); @@ -44,6 +46,7 @@ uint8_t drive_strength = SI5351_CLK_DRIVE_STRENGTH_2MA; int8_t frequency_updated = FALSE; int8_t sweep_enabled = TRUE; int8_t cal_auto_interpolate = TRUE; +int8_t redraw_requested = FALSE; static THD_WORKING_AREA(waThread1, 768); static THD_FUNCTION(Thread1, arg) @@ -474,15 +477,22 @@ void sweep(void) // blink LED while scanning palSetPad(GPIOC, GPIOC_LED); - ui_process(); + if (cal_status & CALSTAT_APPLY) + apply_error_term_at(i); + + redraw_requested = FALSE; + ui_process(); + if (redraw_requested) + return; // return to redraw screen asap. + if (frequency_updated) goto rewind; } set_frequency(frequencies[0]); - if (cal_status & CALSTAT_APPLY) - apply_error_term(); + //if (cal_status & CALSTAT_APPLY) + // apply_error_term(); } void @@ -830,6 +840,35 @@ void apply_error_term(void) } } +void apply_error_term_at(int i) +{ + // S11m' = S11m - Ed + // S11a = S11m' / (Er + Es S11m') + float s11mr = measured[0][i][0] - cal_data[ETERM_ED][i][0]; + float s11mi = measured[0][i][1] - cal_data[ETERM_ED][i][1]; + float err = cal_data[ETERM_ER][i][0] + s11mr * cal_data[ETERM_ES][i][0] - s11mi * cal_data[ETERM_ES][i][1]; + float eri = cal_data[ETERM_ER][i][1] + s11mr * cal_data[ETERM_ES][i][1] + s11mi * cal_data[ETERM_ES][i][0]; + float sq = err*err + eri*eri; + float s11ar = (s11mr * err + s11mi * eri) / sq; + float s11ai = (s11mi * err - s11mr * eri) / sq; + measured[0][i][0] = s11ar; + measured[0][i][1] = s11ai; + + // CAUTION: Et is inversed for efficiency + // S21m' = S21m - Ex + // S21a = S21m' (1-EsS11a)Et + float s21mr = measured[1][i][0] - cal_data[ETERM_EX][i][0]; + float s21mi = measured[1][i][1] - cal_data[ETERM_EX][i][1]; + float esr = 1 - (cal_data[ETERM_ES][i][0] * s11ar - cal_data[ETERM_ES][i][1] * s11ai); + float esi = - (cal_data[ETERM_ES][i][1] * s11ar + cal_data[ETERM_ES][i][0] * s11ai); + float etr = esr * cal_data[ETERM_ET][i][0] - esi * cal_data[ETERM_ET][i][1]; + float eti = esr * cal_data[ETERM_ET][i][1] + esi * cal_data[ETERM_ET][i][0]; + float s21ar = s21mr * etr - s21mi * eti; + float s21ai = s21mi * etr + s21mr * eti; + measured[1][i][0] = s21ar; + measured[1][i][1] = s21ai; +} + void cal_collect(int type) { @@ -1567,7 +1606,7 @@ int main(void) if (config.default_loadcal >= 0) caldata_recall(config.default_loadcal); - redraw(); + redraw_frame(); /* * I2S Initialize diff --git a/nanovna.h b/nanovna.h index cef3b6b..d56950a 100644 --- a/nanovna.h +++ b/nanovna.h @@ -205,10 +205,10 @@ typedef struct { void plot_init(void); void update_grid(void); -void clear_screen(void); -void redraw(void); -void redraw_all(void); -void force_draw_cells(void); +void request_to_redraw_grid(void); +void redraw_frame(void); +//void redraw_all(void); +void request_to_draw_cells_behind_menu(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]); @@ -222,6 +222,8 @@ void markmap_all_markers(void); void marker_position(int m, int t, int *x, int *y); int search_nearest_index(int x, int y, int t); +extern int8_t redraw_requested; + /* * ili9341.c */ @@ -301,6 +303,14 @@ void clear_all_config_prop_data(void); /* * ui.c */ + +typedef struct { + int digit; /* 0~5 */ + int current_trace; /* 0..3 */ +} uistat_t; + +extern uistat_t uistat; + void ui_init(void); void ui_show(void); void ui_hide(void); diff --git a/plot.c b/plot.c index bf8d823..f24db8a 100644 --- a/plot.c +++ b/plot.c @@ -627,22 +627,18 @@ trace_get_info(int t, char *buf, int len) const char *type = trc_type_name[trace[t].type]; switch (trace[t].type) { case TRC_LOGMAG: - chsnprintf(buf, len, "CH%d %s %ddB/", - trace[t].channel, type, (int)(trace[t].scale*10)); + chsnprintf(buf, len, "%s %ddB/", type, (int)(trace[t].scale*10)); break; case TRC_PHASE: - chsnprintf(buf, len, "CH%d %s %d" S_DEGREE "/", - trace[t].channel, type, (int)(trace[t].scale*90)); + chsnprintf(buf, len, "%s %d" S_DEGREE "/", type, (int)(trace[t].scale*90)); break; case TRC_SMITH: //case TRC_ADMIT: case TRC_POLAR: - chsnprintf(buf, len, "CH%d %s %.1fFS", - trace[t].channel, type, trace[t].scale); + chsnprintf(buf, len, "%s %.1fFS", type, trace[t].scale); break; default: - chsnprintf(buf, len, "CH%d %s %.1f/", - trace[t].channel, type, trace[t].scale); + chsnprintf(buf, len, "%s %.1f/", type, trace[t].scale); break; } } @@ -1193,17 +1189,19 @@ redraw_marker(int marker, int update_info) } void -force_draw_cells(void) +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); + //draw_cell(m, n); + mark_map(m, n); + redraw_requested = TRUE; } void -cell_drawchar_5x7(int w, int h, uint8_t ch, int x, int y, uint16_t fg) +cell_drawchar_5x7(int w, int h, uint8_t ch, int x, int y, uint16_t fg, int invert) { uint16_t bits; int c, r; @@ -1213,6 +1211,8 @@ cell_drawchar_5x7(int w, int h, uint8_t ch, int x, int y, uint16_t fg) if ((y + c) < 0 || (y + c) >= h) continue; bits = x5x7_bits[(ch * 7) + c]; + if (invert) + bits = ~bits; for (r = 0; r < 5; r++) { if ((x+r) >= 0 && (x+r) < w && (0x8000 & bits)) spi_buffer[(y+c)*w + (x+r)] = fg; @@ -1225,7 +1225,17 @@ void cell_drawstring_5x7(int w, int h, char *str, int x, int y, uint16_t fg) { while (*str) { - cell_drawchar_5x7(w, h, *str, x, y, fg); + cell_drawchar_5x7(w, h, *str, x, y, fg, FALSE); + x += 5; + str++; + } +} + +void +cell_drawstring_invert_5x7(int w, int h, char *str, int x, int y, uint16_t fg, int invert) +{ + while (*str) { + cell_drawchar_5x7(w, h, *str, x, y, fg, invert); x += 5; str++; } @@ -1249,14 +1259,18 @@ cell_draw_marker_info(int m, int n, int w, int h) int ypos = 1 + (j/2)*7; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; + chsnprintf(buf, sizeof buf, "CH%d", trace[t].channel); + cell_drawstring_invert_5x7(w, h, buf, xpos, ypos, config.trace_color[t], t == uistat.current_trace); + xpos += 20; trace_get_info(t, buf, sizeof buf); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); - xpos += 84; + xpos += 64; trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel][idx], frequencies[idx]); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); j++; } + // draw marker frequency int xpos = 192; int ypos = 1 + (j/2)*7; xpos -= m * CELLWIDTH -CELLOFFSETX; @@ -1267,6 +1281,7 @@ cell_draw_marker_info(int m, int n, int w, int h) frequency_string(buf, sizeof buf, frequencies[idx]); cell_drawstring_5x7(w, h, buf, xpos, ypos, 0xffff); + // draw marker delta if (active_marker != previous_marker && markers[previous_marker].enabled) { int idx0 = markers[previous_marker].index; xpos = 192; @@ -1311,18 +1326,22 @@ draw_frequencies(void) int stop = frequency1; strcpy(buf, "START "); frequency_string(buf+6, 24-6, start); + strcat(buf, " "); ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000); strcpy(buf, "STOP "); frequency_string(buf+5, 24-5, stop); + strcat(buf, " "); ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000); } else if (frequency1 < 0) { int fcenter = frequency0; int fspan = -frequency1; strcpy(buf, "CENTER "); frequency_string(buf+7, 24-7, fcenter); + strcat(buf, " "); ili9341_drawstring_5x7(buf, OFFSETX, 233, 0xffff, 0x0000); strcpy(buf, "SPAN "); frequency_string(buf+5, 24-5, fspan); + strcat(buf, " "); ili9341_drawstring_5x7(buf, 205, 233, 0xffff, 0x0000); } else { int fcenter = frequency0; @@ -1377,28 +1396,28 @@ draw_cal_status(void) } } - void -clear_screen(void) +request_to_redraw_grid(void) { - ili9341_fill(0, 0, 320, 240, 0); + force_set_markmap(); + redraw_requested = TRUE; } void -redraw(void) +redraw_frame(void) { ili9341_fill(0, 0, 320, 240, 0); draw_frequencies(); draw_cal_status(); } -void +/*void redraw_all(void) { redraw(); force_set_markmap(); draw_all_cells(); -} + }*/ void plot_init(void) diff --git a/ui.c b/ui.c index f44e25c..daa6dae 100644 --- a/ui.c +++ b/ui.c @@ -25,10 +25,7 @@ #include -struct { - int digit; /* 0~5 */ - int current_trace; /* 0..3 */ -} uistat; +uistat_t uistat; @@ -466,6 +463,7 @@ menu_trace_cb(int item) trace[item].enabled = TRUE; uistat.current_trace = item; menu_move_back(); + request_to_redraw_grid(); ui_mode_normal(); //redraw_all(); } @@ -492,6 +490,7 @@ menu_format_cb(int item) break; } + request_to_redraw_grid(); ui_mode_normal(); //redraw_all(); } @@ -508,6 +507,7 @@ menu_format2_cb(int item) break; } + request_to_redraw_grid(); ui_mode_normal(); } @@ -565,6 +565,7 @@ menu_trace_op_cb(int item) break; } menu_move_back(); + request_to_redraw_grid(); ui_mode_normal(); //redraw_all(); } @@ -596,29 +597,50 @@ menu_stimulus_cb(int item) } } + +static int32_t +get_marker_frequency(int marker) +{ + if (marker < 0 || marker >= 4) + return -1; + if (!markers[marker].enabled) + return -1; + return frequencies[markers[marker].index]; +} + static void menu_marker_op_cb(int item) { - if (active_marker < 0) - return; - int idx = markers[active_marker].index; - int32_t marker_freq = frequencies[idx]; + int32_t freq = get_marker_frequency(active_marker); + if (freq < 0) + return; // no active marker switch (item) { case 1: /* MARKER->START */ - set_sweep_frequency(ST_START, marker_freq); + set_sweep_frequency(ST_START, freq); break; case 2: /* MARKER->STOP */ - set_sweep_frequency(ST_STOP, marker_freq); + set_sweep_frequency(ST_STOP, freq); break; case 3: /* MARKER->CENTER */ - set_sweep_frequency(ST_CENTER, marker_freq); + set_sweep_frequency(ST_CENTER, freq); break; - case 4: /* MARKER->SPAN */ + case 4: /* MARKERS->SPAN */ { - int32_t span = (frequency0 - marker_freq) * 2; + 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); +#if 0 + int32_t span = (freq - freq2) * 2; if (span < 0) span = -span; set_sweep_frequency(ST_SPAN, span); +#endif } break; } @@ -1139,7 +1161,7 @@ ui_mode_normal(void) area_width = AREA_WIDTH_NORMAL; area_height = HEIGHT; erase_menu_buttons(); - force_draw_cells(); + request_to_draw_cells_behind_menu(); } void @@ -1325,7 +1347,8 @@ ui_process_keypad(void) } } - clear_screen(); + redraw_frame(); + request_to_redraw_grid(); ui_mode_normal(); //redraw_all(); touch_start_watchdog();