From 492fbbed7fb66978a92d214566540287964bc5bb Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 9 Jun 2020 10:10:09 +0200 Subject: [PATCH] Scaling linear units implemented --- nanovna.h | 14 +++++++++----- numfont20x22.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- plot.c | 26 ++++++++++++++++++-------- sa_core.c | 46 ++++++++++++++++++++++++++++++---------------- ui.c | 14 +++++++++----- ui_sa.c | 32 ++++++++++++++++++++------------ 6 files changed, 133 insertions(+), 47 deletions(-) diff --git a/nanovna.h b/nanovna.h index f9221f9..232bc2a 100644 --- a/nanovna.h +++ b/nanovna.h @@ -202,8 +202,8 @@ void toggle_waterfall(void); void set_mode(int); int GetMode(void); void set_reflevel(float); -#define REFLEVEL_MAX 999999.0 -#define REFLEVEL_MIN 0.0010 +#define REFLEVEL_MAX 9999.0 +#define REFLEVEL_MIN 0.00000000001 void set_scale(float); void AllDirty(void); void MenuDirty(void); @@ -345,10 +345,10 @@ enum trace_type { // Phase enum unit_type { - U_DBM=0, U_DBMV, U_DBUV, U_MVOLT, U_UVOLT, U_MWATT, U_UWATT + U_DBM=0, U_DBMV, U_DBUV, U_VOLT, U_MVOLT, U_UVOLT, U_WATT, U_MWATT, U_UWATT }; -#define UNIT_IS_LINEAR(T) ( T >= U_MVOLT ? true : false) -#define UNIT_IS_LOG(T) ( T >= U_MVOLT ? false : true) +#define UNIT_IS_LINEAR(T) ( T >= U_VOLT ? true : false) +#define UNIT_IS_LOG(T) ( T >= U_VOLT ? false : true) float value(float); @@ -583,6 +583,8 @@ typedef struct setting float actual_sweep_time; int test_argument; int auto_IF; + unsigned int unit_scale_index; + float unit_scale; uint32_t checksum; }setting_t; @@ -600,6 +602,8 @@ enum { S_OFF=0, S_ON=1, S_AUTO_OFF=2, S_AUTO_ON=3 }; #define MEASURE_TIME 175.0 // Time per vbwstep without stepdelay in uS extern uint32_t frequencies[POINTS_COUNT]; +extern const float unit_scale_value[]; +extern const char *unit_scale_text[]; #if 1 #define SAVEAREA_MAX 9 diff --git a/numfont20x22.c b/numfont20x22.c index 85498ec..9affa97 100644 --- a/numfont20x22.c +++ b/numfont20x22.c @@ -507,6 +507,7 @@ const uint16_t numfont16x22[] = { 0b0000000000000000, 0b0000000000000000, + 0b0000000000000000, // m 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, @@ -514,6 +515,51 @@ const uint16_t numfont16x22[] = { 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, + 0b1111001100110000, + 0b1111011111111100, + 0b1111111111111110, + 0b1111111111111110, + 0b1111111111011111, + 0b1111101111001111, + 0b1111001111001111, + 0b1111001111001111, + 0b1111001111001111, + 0b1111001111001111, + 0b1111001111001111, + 0b1111001111001111, + 0b1111001111001111, + 0b1111001111001111, + + 0b0000000000000000, // u + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000001111, + 0b1111000000011111, + 0b1111100000111111, + 0b1111110001111111, + 0b0111111111111111, + 0b0011111111101111, + 0b0001111111001111, + + 0b0000000000000000, // n + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, + 0b0000000000000000, 0b0000000000000000, 0b1111000111110000, 0b1111011111111100, @@ -530,7 +576,7 @@ const uint16_t numfont16x22[] = { 0b1111000000001111, 0b1111000000001111, - 0b0000000000000000, + 0b0000000000000000, // p 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, diff --git a/plot.c b/plot.c index 6fb8ed1..4fb5032 100644 --- a/plot.c +++ b/plot.c @@ -117,7 +117,7 @@ void update_grid(void) uint32_t grid; if (fspan == 0) { - fspan = setting.actual_sweep_time; // Time in uS + fspan = setting.actual_sweep_time; // Time in mS fspan *= 1000; fstart = 0; } @@ -468,12 +468,19 @@ value(const float v) case U_DBUV: return v + 90.0 + 20.0*log10(sqrt(50.0)); break; + case U_MVOLT: return pow(10, (v-30.0)/20.0) * sqrt(50.0) * 1000.0; break; + case U_VOLT: + return pow(10, (v-30.0)/20.0) * sqrt(50.0); + break; case U_UVOLT: return pow(10, (v-30.0)/20.0) * sqrt(50.0) * 1000000.0; break; + case U_WATT: + return pow(10, v/10.0)/1000.0; + break; case U_MWATT: return pow(10, v/10.0); break; @@ -497,12 +504,18 @@ to_dBm(const float v) case U_DBUV: return v - 90.0 - 20.0*log10(sqrt(50.0)); break; + case U_VOLT: + return log10( v / (sqrt(50.0))) * 20.0 + 30.0 ; + break; case U_MVOLT: return log10( v / (sqrt(50.0) * 1000.0)) * 20.0 + 30.0 ; break; case U_UVOLT: return log10(v / (sqrt(50.0) * 1000000.0))*20.0 + 30.0; break; + case U_WATT: + return log10(v*1000.0)*10.0; + break; case U_MWATT: return log10(v)*10.0; break; @@ -906,16 +919,13 @@ static void trace_get_value_string( else { v = v - rlevel; if (UNIT_IS_LINEAR(setting.unit)) { - if (v < 1000000) - plot_printf(buf3, sizeof(buf3), "%6f", v); - else - strcpy(buf3,"******"); - buf3[6] = 0; + plot_printf(buf3, sizeof(buf3), "%4f", v/setting.unit_scale); + buf3[5] = 0; } else { - plot_printf(buf3, sizeof(buf3), "%5.1f", v); + plot_printf(buf3, sizeof(buf3), "%.1f", v); buf3[5] = 0; } - plot_printf(buf, len, "%s %s%s%s", buf2, buf3,unit_string[setting.unit],(mtype & M_NOISE?"/Hz":"")); + plot_printf(buf, len, "%s %s%s%s%s", buf2, buf3, unit_scale_text[setting.unit_scale_index], unit_string[setting.unit],(mtype & M_NOISE?"/Hz":"")); } } diff --git a/sa_core.c b/sa_core.c index 34b9f7a..7f09b4a 100644 --- a/sa_core.c +++ b/sa_core.c @@ -56,6 +56,8 @@ void reset_settings(int m) trace[TRACE_STORED].enabled = false; trace[TRACE_TEMP].enabled = false; setting.refer = -1; + setting.unit_scale_index = 0; + setting.unit_scale = 0; #ifdef __SPUR__ setting.spur = 0; #endif @@ -564,10 +566,10 @@ void set_reflevel(float level) void set_scale(float t) { if (UNIT_IS_LINEAR(setting.unit)) { - if (t < REFLEVEL_MIN/10) - t = REFLEVEL_MIN/10; - if (t > REFLEVEL_MAX/10) - t = REFLEVEL_MAX/10; + if (t < REFLEVEL_MIN/10.0) + t = REFLEVEL_MIN/10.0; + if (t > REFLEVEL_MAX/10.0) + t = REFLEVEL_MAX/10.0; } else { if (t > 20.0) t = 20.0; @@ -1141,7 +1143,7 @@ float perform(bool break_on_operation, int i, uint32_t f, int tracking) if (setting.mode == M_LOW && S_IS_AUTO(setting.agc) && UNIT_IS_LOG(setting.unit)) { unsigned char v; static unsigned char old_v; - if (f < 1500000) + if (f < 500000) v = 0x50; // Disable AGC and enable LNA else v = 0x60; // Enable AGC and disable LNA @@ -1815,11 +1817,15 @@ float my_round(float v) return v; } -const char *unit_string[] = { "dBm", "dBmV", "dBuV", "mV", "uV", "mW", "uW" }; +const char *unit_string[] = { "dBm", "dBmV", "dBuV", "V", "mV", "uV", "W", "mW", "uW" }; static const float scale_value[]={50000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20,10,5,2,1,0.5,0.2,0.1,0.05,0.02,0.01,0.005,0.002, 0.001,0.0005,0.0002, 0.0001}; static const char *scale_vtext[]= {"50000", "20000", "10000", "5000", "2000", "1000", "500", "200", "100", "50", "20","10","5","2","1","0.5","0.2","0.1","0.05","0.02","0.01", "0.005","0.002","0.001", "0.0005","0.0002","0.0001"}; +const float unit_scale_value[]={1,0.001,0.000001,0.000000001,0.000000000001}; +const char *unit_scale_text[]= {"","m", "u", "n", "p"}; + + void draw_cal_status(void) { #define BLEN 10 @@ -1829,6 +1835,8 @@ void draw_cal_status(void) int y = OFFSETY; unsigned int color; int rounding = false; + setting.unit_scale_index = 0; + setting.unit_scale = 1.0; if (!UNIT_IS_LINEAR(setting.unit)) rounding = true; const char *unit = unit_string[setting.unit]; @@ -1848,11 +1856,17 @@ void draw_cal_status(void) ili9341_set_background(DEFAULT_BG_COLOR); float yMax = setting.reflevel; + while (UNIT_IS_LINEAR(setting.unit) && setting.unit_scale_index < sizeof(unit_scale_value)/sizeof(float) - 1) { + if (yMax > unit_scale_value[setting.unit_scale_index]) + break; + setting.unit_scale_index++; + } + setting.unit_scale = unit_scale_value[setting.unit_scale_index]; if (rounding) - plot_printf(buf, BLEN, "%6d", (int)yMax); + plot_printf(buf, BLEN, "%4d", (int)yMax); else - plot_printf(buf, BLEN, "%6f", yMax+0.00005); - buf[6]=0; + plot_printf(buf, BLEN, "%4f", (yMax/setting.unit_scale)+0.00005); + buf[5]=0; if (level_is_calibrated()) { if (setting.auto_reflevel) color = DEFAULT_FG_COLOR; @@ -1873,7 +1887,7 @@ void draw_cal_status(void) } #endif y += YSTEP + YSTEP/2 ; - plot_printf(buf, BLEN, "%s",unit); + plot_printf(buf, BLEN, "%s%s",unit_scale_text[setting.unit_scale_index], unit); ili9341_drawstring(buf, x, y); color = DEFAULT_FG_COLOR; @@ -1881,7 +1895,7 @@ void draw_cal_status(void) y += YSTEP + YSTEP/2; unsigned int i = 0; while (i < sizeof(scale_value)/sizeof(float)) { - float t = setting.scale / scale_value[i];; + float t = (setting.scale/setting.unit_scale) / scale_value[i];; if (t > 0.9 && t < 1.1){ plot_printf(buf, BLEN, "%s/",scale_vtext[i]); break; @@ -2029,9 +2043,9 @@ void draw_cal_status(void) y += YSTEP; if (rounding) - plot_printf(buf, BLEN, "%6d", (int)value(setting.trigger_level)); + plot_printf(buf, BLEN, "%4f", value(setting.trigger_level)); else - plot_printf(buf, BLEN, "%6f", value(setting.trigger_level)); + plot_printf(buf, BLEN, "%4f", value(setting.trigger_level)/setting.unit_scale); buf[6]=0; ili9341_drawstring(buf, x, y); } @@ -2087,10 +2101,10 @@ void draw_cal_status(void) y = HEIGHT-7 + OFFSETY; if (rounding) - plot_printf(buf, BLEN, "%6d", (int)(yMax - setting.scale * NGRIDY)); + plot_printf(buf, BLEN, "%4d", (int)(yMax - setting.scale * NGRIDY)); else - plot_printf(buf, BLEN, "%6f", (yMax - setting.scale * NGRIDY)+0.00005); - buf[6]=0; + plot_printf(buf, BLEN, "%4f", ((yMax - setting.scale * NGRIDY)/setting.unit_scale)+0.0005); + buf[5]=0; if (level_is_calibrated()) if (setting.auto_reflevel) color = DEFAULT_FG_COLOR; diff --git a/ui.c b/ui.c index a07f9fe..6b6a9fe 100644 --- a/ui.c +++ b/ui.c @@ -2067,14 +2067,18 @@ static int keypad_click(int key) { int c = keypads[key].c; - if ((c >= KP_X1 && c <= KP_G) || c == KP_N || c == KP_P) { - int32_t scale = 1; + if ((c >= KP_X1 && c <= KP_G) || c == KP_m || c == KP_u) { + float scale = 1.0; if (c >= KP_X1 && c <= KP_G) { int n = c - KP_X1; while (n-- > 0) - scale *= 1000; - } else if (c == KP_N) { - scale *= 1000; + scale *= 1000.0; + } else if (c == KP_m) { + scale /= 1000.0; + } else if (c == KP_u) { + scale /= 1000000.0; + } else if (c == KP_n) { + scale /= 1000000000.0; } /* numeric input done */ double value = my_atof(kp_buf) * scale; diff --git a/ui_sa.c b/ui_sa.c index e395565..db3f1a9 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -300,8 +300,9 @@ enum { #define KP_DB 18 #define KP_PLUSMINUS 19 #define KP_KEYPAD 20 -#define KP_N 21 -#define KP_P 22 +#define KP_m 21 +#define KP_u 22 +#define KP_n 23 typedef struct { @@ -347,6 +348,9 @@ static const keypads_t keypads_scale[] = { { 0, 0, 7 }, { 1, 0, 8 }, { 2, 0, 9 }, + { 3, 0, KP_m }, + { 3, 1, KP_u }, + { 3, 2, KP_n }, { 3, 3, KP_X1 }, { 2, 3, KP_BS }, { 0, 0, -1 } @@ -364,6 +368,8 @@ static const keypads_t keypads_level[] = { { 0, 0, 7 }, { 1, 0, 8 }, { 2, 0, 9 }, + { 3, 0, KP_m }, + { 3, 1, KP_u }, { 3, 2, KP_MINUS }, { 3, 3, KP_X1 }, { 2, 3, KP_BS }, @@ -382,8 +388,8 @@ static const keypads_t keypads_time[] = { { 0, 0, 7 }, { 1, 0, 8 }, { 2, 0, 9 }, - { 3, 1, KP_N }, - { 3, 2, KP_P }, + { 3, 1, KP_m }, + { 3, 2, KP_u }, { 3, 3, KP_MINUS }, { 2, 3, KP_BS }, { 0, 0, -1 } @@ -1388,13 +1394,13 @@ static const menuitem_t menu_displayhigh[] = { static const menuitem_t menu_unit[] = { - { MT_CALLBACK,U_DBM, "dBm", menu_unit_cb}, - { MT_CALLBACK,U_DBMV, "dBmV", menu_unit_cb}, - { MT_CALLBACK,U_DBUV, "dBuV", menu_unit_cb}, - { MT_CALLBACK,U_MVOLT, "mVolt", menu_unit_cb}, - { MT_CALLBACK,U_UVOLT, "uVolt", menu_unit_cb}, - { MT_CALLBACK,U_MWATT, "mWatt", menu_unit_cb}, - { MT_CALLBACK,U_UWATT, "uWatt", menu_unit_cb}, + { MT_CALLBACK,U_DBM, "dBm", menu_unit_cb}, + { MT_CALLBACK,U_DBMV, "dBmV", menu_unit_cb}, + { MT_CALLBACK,U_DBUV, "dBuV", menu_unit_cb}, + { MT_CALLBACK,U_VOLT, "Volt", menu_unit_cb}, +// { MT_CALLBACK,U_UVOLT, "uVolt", menu_unit_cb}, + { MT_CALLBACK,U_WATT, "Watt", menu_unit_cb}, +// { MT_CALLBACK,U_UWATT, "uWatt", menu_unit_cb}, { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; @@ -1803,10 +1809,12 @@ set_numeric_value(void) if (UNIT_IS_LINEAR(setting.unit)) set_auto_reflevel(false); set_scale(uistat.value); + if (UNIT_IS_LINEAR(setting.unit) && setting.reflevel < setting.scale*NGRIDY) + set_reflevel(setting.scale*NGRIDY); break; case KM_REFLEVEL: set_auto_reflevel(false); - if (uistat.value < setting.scale*NGRIDY) { + if (UNIT_IS_LINEAR(setting.unit) && uistat.value < setting.scale*NGRIDY) { set_scale(uistat.value/NGRIDY); set_reflevel(setting.scale*NGRIDY); } else