From ddc278fa18e5f558abaf99607815427452524570 Mon Sep 17 00:00:00 2001 From: DiSlord Date: Mon, 10 Feb 2020 22:22:27 +0300 Subject: [PATCH] Huge rework chsnprintf function (basic functional more compact and faster): now support print float and Suffix if use example %.1F on 1.234e-3 print 1.234m, %F print 1.23m now support + flag %+d on 8 print +8, %+d on -8 print -8 now support freq output if use %q example %q on 1234567890 print 1.234 567 890 GHz, %.8q print 1.234567GHz fix rounding errors on print float example if print use %.2f on 2.199 print 2.20 (before 2.19) Use it in code - made more compact (save about 2k bytes) and easy display values (set output more digits after . for some values) Made some font glyph more compact, allow 3px glyph --- ChibiOS | 2 +- Font5x7.c | 75 ++++----- main.c | 6 +- nanovna.h | 2 +- plot.c | 446 +++++++++++++++++++----------------------------------- 5 files changed, 200 insertions(+), 331 deletions(-) diff --git a/ChibiOS b/ChibiOS index 669d4bb..2e8f986 160000 --- a/ChibiOS +++ b/ChibiOS @@ -1 +1 @@ -Subproject commit 669d4bbc8da1ee0e4ccdf93a472b06d183922320 +Subproject commit 2e8f986734014e47dde005a3aaab4e1e19ef614d diff --git a/Font5x7.c b/Font5x7.c index 63f66bd..0a3a828 100644 --- a/Font5x7.c +++ b/Font5x7.c @@ -10,14 +10,17 @@ */ #define FONT_GET_DATA(ch) (&x5x7_bits[ch*7]) -#define FONT_GET_WIDTH(ch) (7-x5x7_bits[ch*7]&3) +#define FONT_GET_WIDTH(ch) (8-x5x7_bits[ch*7]&7) #define FONT_GET_HEIGHT 7 -#define CHAR5x7_WIDTH_MASK 0x03 -#define CHAR5x7_WIDTH_4px 0x03 -#define CHAR5x7_WIDTH_5px 0x02 -#define CHAR5x7_WIDTH_6px 0x01 -#define CHAR5x7_WIDTH_7px 0x00 +#define CHAR5x7_WIDTH_1px 0x07 +#define CHAR5x7_WIDTH_2px 0x06 +#define CHAR5x7_WIDTH_3px 0x05 +#define CHAR5x7_WIDTH_4px 0x04 +#define CHAR5x7_WIDTH_5px 0x03 +#define CHAR5x7_WIDTH_6px 0x02 +#define CHAR5x7_WIDTH_7px 0x01 +#define CHAR5x7_WIDTH_8px 0x00 /* Font character bitmap data. */ const uint8_t x5x7_bits[127*7] = @@ -100,23 +103,23 @@ const uint8_t x5x7_bits[127*7] = 0b00100000, /* Character (0x04): - width=7 + width=6 +--------+ | | - | ** | - | ** | - | * * | - | * * | - |* * | - |****** | + | * | + | * | + | * * | + | * * | + |* * | + |***** | +--------+ */ - 0b00000000|CHAR5x7_WIDTH_7px, - 0b00110000, - 0b00110000, - 0b01001000, - 0b01001000, - 0b10000100, - 0b11111100, + 0b00000000|CHAR5x7_WIDTH_6px, + 0b00100000, + 0b00100000, + 0b01010000, + 0b01010000, + 0b10001000, + 0b11111000, /* Character (0x05): width=5 @@ -575,22 +578,22 @@ const uint8_t x5x7_bits[127*7] = 0b10011000, /* Character (0x1d): - width=7 + width=6 +--------+ | | | | - | * * | - | * * | - | ** ** | - | * * * | + |* * | + |* * | + |** ** | + |* * * | |* | +--------+ */ - 0b00000000|CHAR5x7_WIDTH_7px, + 0b00000000|CHAR5x7_WIDTH_6px, 0b00000000, - 0b01000100, - 0b01000100, - 0b01101100, - 0b01010100, + 0b10001000, + 0b10001000, + 0b11011000, + 0b10101000, 0b10000000, /* Character (0x1e): @@ -632,7 +635,7 @@ const uint8_t x5x7_bits[127*7] = 0b00000000, /* Character (0x20): ' ' - width=4 + width=3 +--------+ | | | | @@ -642,7 +645,7 @@ const uint8_t x5x7_bits[127*7] = | | | | +--------+ */ - 0b00000000|CHAR5x7_WIDTH_4px, + 0b00000000|CHAR5x7_WIDTH_3px, 0b00000000, 0b00000000, 0b00000000, @@ -651,7 +654,7 @@ const uint8_t x5x7_bits[127*7] = 0b00000000, /* Character (0x21): '!' - width=4 + width=3 +--------+ | * | | * | @@ -661,7 +664,7 @@ const uint8_t x5x7_bits[127*7] = | | | * | +--------+ */ - 0b01000000|CHAR5x7_WIDTH_4px, + 0b01000000|CHAR5x7_WIDTH_3px, 0b01000000, 0b01000000, 0b01000000, @@ -870,7 +873,7 @@ const uint8_t x5x7_bits[127*7] = | * | |* | +--------+ */ - 0b00000000|CHAR5x7_WIDTH_4px, + 0b00000000|CHAR5x7_WIDTH_3px, 0b00000000, 0b00000000, 0b00000000, @@ -908,7 +911,7 @@ const uint8_t x5x7_bits[127*7] = |** | |** | +--------+ */ - 0b00000000|CHAR5x7_WIDTH_4px, + 0b00000000|CHAR5x7_WIDTH_3px, 0b00000000, 0b00000000, 0b00000000, diff --git a/main.c b/main.c index 82d8fa5..61f9570 100644 --- a/main.c +++ b/main.c @@ -818,8 +818,8 @@ set_frequencies(uint32_t start, uint32_t stop, uint16_t points) uint32_t step = (points - 1); uint32_t span = stop - start; uint32_t delta = span / step; - uint32_t error = span - delta * step; - uint32_t f = start, df = step/2; + uint32_t error = span % step; + uint32_t f = start, df = step>>1; for (i = 0; i <= step; i++, f+=delta) { frequencies[i] = f; df+=error; @@ -993,7 +993,7 @@ static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[]) if (argc >=1) value0 = my_atoui(argv[0]); if (argc >=2) value1 = my_atoui(argv[1]); - // Parse {start|stop|center|span|cw} {freq(Hz)} + // Parse sweep {start|stop|center|span|cw} {freq(Hz)} if (argc == 2 && value0 == 0) { int type; if (strcmp(argv[0], "start") == 0) diff --git a/nanovna.h b/nanovna.h index 82e9d0a..a1b490c 100644 --- a/nanovna.h +++ b/nanovna.h @@ -152,7 +152,7 @@ extern int16_t area_height; extern const uint8_t x5x7_bits []; #define FONT_GET_DATA(ch) (&x5x7_bits[ch*7]) -#define FONT_GET_WIDTH(ch) (7-(x5x7_bits[ch*7]&3)) +#define FONT_GET_WIDTH(ch) (8-(x5x7_bits[ch*7]&7)) #define FONT_GET_HEIGHT 7 extern const uint16_t numfont16x22[]; diff --git a/plot.c b/plot.c index a238526..946297b 100644 --- a/plot.c +++ b/plot.c @@ -8,8 +8,6 @@ #define SWAP(x,y) do { int z=x; x = y; y = z; } while(0) static void cell_draw_marker_info(int m, int n, int w, int h); -void frequency_string(char *buf, size_t len, uint32_t freq, char *prefix); -void frequency_string_short(char *buf, size_t len, int32_t freq, char prefix); void markmap_all_markers(void); /* indicate dirty cells */ @@ -501,6 +499,22 @@ groupdelay_from_array(int i, float array[POINTS_COUNT][2]) return groupdelay(array[bottom], array[top], deltaf); } +static float +gamma2resistance(const float v[2]) +{ + float z0 = 50; + float d = z0 / ((1-v[0])*(1-v[0])+v[1]*v[1]); + return ((1+v[0])*(1-v[0]) - v[1]*v[1]) * d; +} + +static float +gamma2reactance(const float v[2]) +{ + float z0 = 50; + float d = z0 / ((1-v[0])*(1-v[0])+v[1]*v[1]); + return 2*v[1] * d; +} + uint32_t trace_into_index(int x, int t, int i, float array[POINTS_COUNT][2]) { @@ -550,48 +564,6 @@ trace_into_index(int x, int t, int i, float array[POINTS_COUNT][2]) return INDEX(x +CELLOFFSETX, y, i); } -// Prefixes for values bigger then 1000.0 -static char bigPrefix[]={'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'}; -// Prefixes for values less then 1.0 -static char smallPrefix[]={'m', S_MICRO[0], 'n', 'p', 'f', 'a', 'z', 'y'}; - -static int -string_value_with_prefix(char *buf, int len, float val, char unit) -{ - char prefix=0; - int n = 0; - if (val < 0) { - val = -val; - *buf = '-'; - n++; - len--; - } - if (val == INFINITY){ - prefix = S_INFINITY[0]; - } - else { - if (val > 1000) - for (uint32_t i=0; i 1000; i++, val/=1000) - prefix=bigPrefix[i]; - else if (val < 1) - for (uint32_t i=0; i= 0) buf[n++] = '+'; - string_value_with_prefix(buf+n, len-n, coeff[1], 'j'); + chsnprintf(buf, len, "%F%+Fj", coeff[0], coeff[1]); break; case MS_RX: - n = string_value_with_prefix(buf, len, zr, S_OHM[0]); - if (zi >= 0) - buf[n++] = ' '; - string_value_with_prefix(buf+n, len-n, zi, 'j'); + chsnprintf(buf, len, "%F"S_OHM"%+Fj", zr, zi); break; case MS_RLC: - n = string_value_with_prefix(buf, len, zr, S_OHM[0]); - buf[n++] = ' '; - - char prefix; - float value; if (zi < 0){// Capacity - prefix = 'F'; - value = -1 / (PI2 * frequency * zi); + prefix = 'F'; + value = -1 / (PI2 * frequency * zi); } else { - prefix = 'H'; - value = zi / (PI2 * frequency); + prefix = 'H'; + value = zi / (PI2 * frequency); } - string_value_with_prefix(buf+n, len-n, value, prefix); + chsnprintf(buf, len, "%F"S_OHM" %F%c", zr, value, prefix); break; } } -static void -gamma2resistance(char *buf, int len, const float coeff[2]) -{ - float z0 = 50; - float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]); - float zr = ((1+coeff[0])*(1-coeff[0]) - coeff[1]*coeff[1]) * d; - string_value_with_prefix(buf, len, zr, S_OHM[0]); -} - -static void -gamma2reactance(char *buf, int len, const float coeff[2]) -{ - float z0 = 50; - float d = z0 / ((1-coeff[0])*(1-coeff[0])+coeff[1]*coeff[1]); - float zi = 2*coeff[1] * d; - string_value_with_prefix(buf, len, zi, S_OHM[0]); -} - static void trace_get_value_string(int t, char *buf, int len, float array[POINTS_COUNT][2], int i) { float *coeff = array[i]; float v; + char *format; switch (trace[t].type) { case TRC_LOGMAG: + format = "%.2fdB"; v = logmag(coeff); - if (v == -INFINITY) - chsnprintf(buf, len, "-"S_INFINITY" dB"); - else - chsnprintf(buf, len, "%.2fdB", v); break; case TRC_PHASE: + format = "%.3f"S_DEGREE; v = phase(coeff); - chsnprintf(buf, len, "%.2f" S_DEGREE, v); break; case TRC_DELAY: + format = "%.2Fs"; v = groupdelay_from_array(i, array); - string_value_with_prefix(buf, len, v, 's'); break; case TRC_LINEAR: + format = "%.4f"; v = linear(coeff); - chsnprintf(buf, len, "%.2f", v); break; case TRC_SWR: + format = "%.4f"; v = swr(coeff); - if (v == INFINITY) - chsnprintf(buf, len, S_INFINITY); - else - chsnprintf(buf, len, "%.2f", v); - break; - case TRC_SMITH: - format_smith_value(buf, len, coeff, frequencies[i]); break; case TRC_REAL: - chsnprintf(buf, len, "%.2f", coeff[0]); + format = "%.4f"; + v = coeff[0]; break; case TRC_IMAG: - chsnprintf(buf, len, "%.2fj", coeff[1]); + format = "%.4fj"; + v = coeff[1]; break; case TRC_R: - gamma2resistance(buf, len, coeff); + format = "%.2F"S_OHM; + v = gamma2resistance(coeff); break; case TRC_X: - gamma2reactance(buf, len, coeff); + format = "%.2F"S_OHM; + v = gamma2reactance(coeff); break; - //case TRC_ADMIT: + case TRC_SMITH: + format_smith_value(buf, len, coeff, frequencies[i]); + return; + //case TRC_ADMIT: case TRC_POLAR: - chsnprintf(buf, len, "%.2f %.2fj", coeff[0], coeff[1]); - break; + chsnprintf(buf, len, "%.2f%+.2fj", coeff[0], coeff[1]); + default: + return; } + chsnprintf(buf, len, format, v); } static void @@ -728,78 +672,80 @@ trace_get_value_string_delta(int t, char *buf, int len, float array[POINTS_COUNT float *coeff = array[index]; float *coeff_ref = array[index_ref]; float v; + char *format; switch (trace[t].type) { case TRC_LOGMAG: + format = S_DELTA"%.2fdB"; v = logmag(coeff) - logmag(coeff_ref); - if (v == -INFINITY) - chsnprintf(buf, len, S_DELTA "-"S_INFINITY" dB"); - else - chsnprintf(buf, len, S_DELTA "%.2fdB", v); break; case TRC_PHASE: + format = S_DELTA"%.2f"S_DEGREE; v = phase(coeff) - phase(coeff_ref); - chsnprintf(buf, len, S_DELTA "%.2f" S_DEGREE, v); break; case TRC_DELAY: + format = "%.2Fs"; v = groupdelay_from_array(index, array) - groupdelay_from_array(index_ref, array); - string_value_with_prefix(buf, len, v, 's'); break; case TRC_LINEAR: + format = S_DELTA"%.3f"; v = linear(coeff) - linear(coeff_ref); - chsnprintf(buf, len, S_DELTA "%.2f", v); break; case TRC_SWR: - v = swr(coeff) - swr(coeff_ref); - chsnprintf(buf, len, S_DELTA "%.2f", v); + format = S_DELTA"%.3f"; + v = swr(coeff); + if (v!=INFINITY) + v-=swr(coeff_ref); break; case TRC_SMITH: format_smith_value(buf, len, coeff, frequencies[index]); - break; + return; case TRC_REAL: - chsnprintf(buf, len, S_DELTA "%.2f", coeff[0] - coeff_ref[0]); + format = S_DELTA"%.3f"; + v = coeff[0] - coeff_ref[0]; break; case TRC_IMAG: - chsnprintf(buf, len, S_DELTA "%.2fj", coeff[1] - coeff_ref[1]); + format = S_DELTA"%.3fj"; + v = coeff[1] - coeff_ref[1]; break; case TRC_R: - gamma2resistance(buf, len, coeff); + format = "%.2F"S_OHM; + v = gamma2resistance(coeff); break; case TRC_X: - gamma2reactance(buf, len, coeff); + format = "%.2F"S_OHM; + v = gamma2reactance(coeff); break; //case TRC_ADMIT: case TRC_POLAR: - chsnprintf(buf, len, "%.2f %.2fj", coeff[0], coeff[1]); - break; + chsnprintf(buf, len, "%.2f%+.2fj", coeff[0], coeff[1]); + return; + default: + return; } + chsnprintf(buf, len, format, v); } static int trace_get_info(int t, char *buf, int len) { - strcpy(buf, get_trace_typename(t)); - int n = strlen(buf); - char *p = buf + n; - len -= n; + const char *name = get_trace_typename(t); + float scale = get_trace_scale(t); switch (trace[t].type) { case TRC_LOGMAG: - n += chsnprintf(p, len, " %ddB/", (int)get_trace_scale(t)); - break; + return chsnprintf(buf, len, "%s %ddB/", name, (int)scale); case TRC_PHASE: - n += chsnprintf(p, len, " %d" S_DEGREE "/", (int)get_trace_scale(t)); - break; + return chsnprintf(buf, len, "%s %d" S_DEGREE "/", name, (int)scale); case TRC_SMITH: //case TRC_ADMIT: case TRC_POLAR: - if (get_trace_scale(t) != 1.0) - n += chsnprintf(p, len, " %.1fFS", get_trace_scale(t)); - break; + if (scale != 1.0) + return chsnprintf(buf, len, "%s %.1fFS", name, scale); + else + return chsnprintf(buf, len, name); default: - strcat(p, " "); - string_value_with_prefix(p+1, len-1 , get_trace_scale(t), '/'); - break; + return chsnprintf(buf, len, "%s %F/", name, scale); } - return n; + return 0; } static float time_of_index(int idx) { @@ -1567,43 +1513,32 @@ request_to_draw_cells_behind_numeric_input(void) int -cell_drawchar(int w, int h, uint8_t ch, int x, int y, int invert) -{ - uint8_t bits; - int c, r, ch_size; - const uint8_t *char_buf = FONT_GET_DATA(ch); - ch_size=FONT_GET_WIDTH(ch); - if (y <= -FONT_GET_HEIGHT || y >= h || x <= -ch_size || x >= w) - return ch_size; - for(c = 0; c < FONT_GET_HEIGHT; c++) { - bits = *char_buf++; - if ((y + c) < 0 || (y + c) >= h) - continue; - if (invert) - bits = ~bits; - for (r = 0; r < ch_size; r++) { - if ((x+r) >= 0 && (x+r) < w && (0x80 & bits)) - spi_buffer[(y+c)*w + (x+r)] = foreground_color; - bits <<= 1; - } - } - return ch_size; -} - -void -cell_drawstring(int w, int h, char *str, int x, int y) -{ - while (*str) { - x += cell_drawchar(w, h, *str, x, y, FALSE); - str++; +cell_drawchar(int w, int h, uint8_t ch, int x, int y) +{ + uint8_t bits; + int c, r, ch_size; + const uint8_t *char_buf = FONT_GET_DATA(ch); + ch_size=FONT_GET_WIDTH(ch); + if (y <= -FONT_GET_HEIGHT || y >= h || x <= -ch_size || x >= w) + return ch_size; + for(c = 0; c < FONT_GET_HEIGHT; c++) { + bits = *char_buf++; + if ((y + c) < 0 || (y + c) >= h) + continue; + for (r = 0; r < ch_size; r++) { + if ((x+r) >= 0 && (x+r) < w && (0x80 & bits)) + spi_buffer[(y+c)*w + (x+r)] = foreground_color; + bits <<= 1; + } } + return ch_size; } void -cell_drawstring_invert(int w, int h, char *str, int x, int y, int invert) +cell_drawstring(int w, int h, char *str, int x, int y) { while (*str) { - x += cell_drawchar(w, h, *str, x, y, invert); + x += cell_drawchar(w, h, *str, x, y); str++; } } @@ -1611,7 +1546,7 @@ cell_drawstring_invert(int w, int h, char *str, int x, int y, int invert) static void cell_draw_marker_info(int m, int n, int w, int h) { - char buf[24]; + char buf[32]; int t; if (n != 0) return; @@ -1629,27 +1564,27 @@ cell_draw_marker_info(int m, int n, int w, int h) int ypos = 1 + (j/2)*8; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; - - setForegroundColor(config.trace_color[t]); - if (mk == active_marker) - cell_drawstring(w, h, S_SARROW, xpos, ypos); - xpos += 5; + + setForegroundColor(config.trace_color[t]); + if (mk == active_marker) + cell_drawstring(w, h, S_SARROW, xpos, ypos); + xpos += 5; chsnprintf(buf, sizeof buf, "M%d", mk+1); cell_drawstring(w, h, buf, xpos, ypos); - - xpos += 13; + xpos += 13; //trace_get_info(t, buf, sizeof buf); - int32_t freq = frequencies[markers[mk].index]; + uint32_t freq = frequencies[markers[mk].index]; if (uistat.marker_delta && mk != active_marker) { - freq -= frequencies[markers[active_marker].index]; - frequency_string_short(buf, sizeof buf, freq, S_DELTA[0]); + uint32_t freq1 = frequencies[markers[active_marker].index]; + uint32_t delta = freq > freq1 ? freq - freq1 : freq1 - freq; + chsnprintf(buf, sizeof buf, S_DELTA"%.9qHz", delta); } else { - frequency_string_short(buf, sizeof buf, freq, 0); + chsnprintf(buf, sizeof buf, "%.10qHz", freq); } cell_drawstring(w, h, buf, xpos, ypos); - xpos += 64; + xpos += 67; if (uistat.marker_delta && mk != active_marker) - trace_get_value_string_delta(t, buf, sizeof buf, measured[trace[t].channel], markers[mk].index, markers[active_marker].index); + trace_get_value_string_delta(t, buf, sizeof buf, measured[trace[t].channel], markers[mk].index, markers[active_marker].index); else trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel], markers[mk].index); setForegroundColor(DEFAULT_FG_COLOR); @@ -1660,24 +1595,21 @@ cell_draw_marker_info(int m, int n, int w, int h) // draw marker delta if (!uistat.marker_delta && previous_marker >= 0 && active_marker != previous_marker && markers[previous_marker].enabled) { int idx0 = markers[previous_marker].index; - int xpos = 185; + int xpos = 180; int ypos = 1 + (j/2)*8; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; - strcpy(buf, S_DELTA "1-1:"); - buf[1] += active_marker; - buf[3] += previous_marker; + chsnprintf(buf, sizeof buf, S_DELTA"%d-%d", active_marker+1, previous_marker+1); setForegroundColor(DEFAULT_FG_COLOR); cell_drawstring(w, h, buf, xpos, ypos); - xpos += 29; + xpos += 24; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { - frequency_string_short(buf, sizeof buf, frequencies[idx] - frequencies[idx0], 0); + uint32_t freq = frequencies[idx]; + uint32_t freq1 = frequencies[idx0]; + uint32_t delta = freq > freq1 ? freq - freq1 : freq1 - freq; + chsnprintf(buf, sizeof buf, "%c%.13qHz", freq >= freq1 ? '+' : '-', delta); } else { - //chsnprintf(buf, sizeof buf, "%d ns %.1f m", (uint16_t)(time_of_index(idx) * 1e9 - time_of_index(idx0) * 1e9), - // distance_of_index(idx) - distance_of_index(idx0)); - int n = string_value_with_prefix(buf, sizeof buf, time_of_index(idx) - time_of_index(idx0), 's'); - buf[n++] = ' '; - string_value_with_prefix(&buf[n], sizeof buf - n, distance_of_index(idx) - distance_of_index(idx0), 'm'); + chsnprintf(buf, sizeof buf, "%Fs (%Fm)", time_of_index(idx) - time_of_index(idx0), distance_of_index(idx) - distance_of_index(idx0)); } cell_drawstring(w, h, buf, xpos, ypos); } @@ -1689,23 +1621,17 @@ cell_draw_marker_info(int m, int n, int w, int h) int ypos = 1 + (j/2)*8; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; -// setForegroundColor(config.trace_color[t]); -// strcpy(buf, "CH0"); -// buf[2] += trace[t].channel; - //chsnprintf(buf, sizeof buf, "CH%d", trace[t].channel); -// cell_drawstring_invert(w, h, buf, xpos, ypos, t == uistat.current_trace); -// xpos += 20; setForegroundColor(config.trace_color[t]); if (t == uistat.current_trace) - cell_drawstring(w, h, S_SARROW, xpos, ypos); + cell_drawstring(w, h, S_SARROW, xpos, ypos); xpos += 5; chsnprintf(buf, sizeof buf, "CH%d", trace[t].channel); cell_drawstring(w, h, buf, xpos, ypos); xpos += 19; - trace_get_info(t, buf, sizeof buf); + int n = trace_get_info(t, buf, sizeof buf); cell_drawstring(w, h, buf, xpos, ypos); - xpos += (strlen(buf) + 1) * 5; + xpos += n * 5 + 2; //xpos += 60; trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel], idx); setForegroundColor(DEFAULT_FG_COLOR); @@ -1719,27 +1645,18 @@ cell_draw_marker_info(int m, int n, int w, int h) xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; setForegroundColor(DEFAULT_FG_COLOR); -// strcpy(buf, "1:"); -// buf[0] += active_marker; -// xpos += 5; -// setForegroundColor(0xffff); -// cell_drawstring_invert(w, h, buf, xpos, ypos, uistat.lever_mode == LM_MARKER); -// xpos += 14; - setForegroundColor(DEFAULT_FG_COLOR); if (uistat.lever_mode == LM_MARKER) - cell_drawstring(w, h, S_SARROW, xpos, ypos); + cell_drawstring(w, h, S_SARROW, xpos, ypos); xpos += 5; chsnprintf(buf, sizeof buf, "M%d:", active_marker+1); cell_drawstring(w, h, buf, xpos, ypos); - xpos += 19; + xpos += 19; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { - frequency_string(buf, sizeof buf, frequencies[idx], ""); + //frequency_string(buf, sizeof buf, frequencies[idx], ""); + chsnprintf(buf, sizeof buf, "%16qHz", frequencies[idx]); } else { - //chsnprintf(buf, sizeof buf, "%d ns %.1f m", (uint16_t)(time_of_index(idx) * 1e9), distance_of_index(idx)); - int n = string_value_with_prefix(buf, sizeof buf, time_of_index(idx), 's'); - buf[n++] = ' '; - string_value_with_prefix(&buf[n], sizeof buf-n, distance_of_index(idx), 'm'); + chsnprintf(buf, sizeof buf, "%Fs (%Fm)", time_of_index(idx), distance_of_index(idx)); } cell_drawstring(w, h, buf, xpos, ypos); } @@ -1750,85 +1667,32 @@ cell_draw_marker_info(int m, int n, int w, int h) int ypos = 1 + ((j+1)/2)*8; xpos -= m * CELLWIDTH -CELLOFFSETX; ypos -= n * CELLHEIGHT; - chsnprintf(buf, sizeof buf, "Edelay"); - cell_drawstring(w, h, buf, xpos, ypos); - xpos += 7 * 5; - int n = string_value_with_prefix(buf, sizeof buf, electrical_delay * 1e-12, 's'); - cell_drawstring(w, h, buf, xpos, ypos); - xpos += n * 5 + 5; + float light_speed_ps = 299792458e-12; //(m/ps) - string_value_with_prefix(buf, sizeof buf, electrical_delay * light_speed_ps * velocity_factor, 'm'); + chsnprintf(buf, sizeof buf, "Edelay %Fs %Fm", electrical_delay * 1e-12, + electrical_delay * light_speed_ps * velocity_factor); cell_drawstring(w, h, buf, xpos, ypos); } } -void -frequency_string(char *buf, size_t len, uint32_t freq, char *prefix) -{ -/* if (freq < 0) { - freq = -freq; - *buf++ = '-'; - len -= 1; - }*/ - if (freq < 1000) { - chsnprintf(buf, len, "%s%d Hz", prefix, (int)freq); - } else if (freq < 1000000U) { - chsnprintf(buf, len, "%s%d.%03d kHz", prefix, - (freq / 1000U), - (freq % 1000U)); - } else { - chsnprintf(buf, len, "%s%d.%03d %03d MHz", prefix, - (freq / 1000000U), - ((freq / 1000U) % 1000U), - (freq % 1000U)); - } -} - -void -frequency_string_short(char *b, size_t len, int32_t freq, char prefix) -{ - char *buf = b; - if (prefix) { - *buf++ = prefix; - len -= 1; - } - if (freq < 0) { - freq = -freq; - *buf++ = '-'; - len -= 1; - } - if (freq < 1000) { - chsnprintf(buf, len, "%d Hz", (int)freq); - } else if (freq < 1000000) { - chsnprintf(buf, len, "%d.%03dkHz", - (int)(freq / 1000), - (int)(freq % 1000)); - } else { - chsnprintf(buf, len, "%d.%06d", - (int)(freq / 1000000), - (int)(freq % 1000000)); - strcpy(b+9, "MHz"); - } -} - void draw_frequencies(void) { - char buf1[24];buf1[0]=' '; - char buf2[24];buf2[0]=0; + char buf1[32]; + char buf2[32];buf2[0]=0; if ((domain_mode & DOMAIN_MODE) == DOMAIN_FREQ) { if (frequency0 < frequency1) { - frequency_string(buf1+1, sizeof(buf1)-1, frequency0, "START "); - frequency_string(buf2, sizeof buf2, frequency1, "STOP "); + chsnprintf(buf1, sizeof(buf1), " START %16qHz", frequency0); + chsnprintf(buf2, sizeof(buf2), "STOP %16qHz", frequency1); } else if (frequency0 > frequency1) { - frequency_string(buf1+1, sizeof(buf1)-1, frequency0/2 + frequency1/2, "CENTER "); - frequency_string(buf2, sizeof buf2, frequency0 - frequency1, "SPAN "); + chsnprintf(buf1, sizeof(buf1), " CENTER %16qHz", frequency0/2 + frequency1/2); + chsnprintf(buf2, sizeof(buf2), "SPAN %16qHz", frequency0 - frequency1); } else { - frequency_string(buf1+1, sizeof(buf1)-1, frequency0, "CW "); + chsnprintf(buf1, sizeof(buf1), " CW %16qHz", frequency0); } } else { - chsnprintf(buf1+1, sizeof(buf1)-1, "START 0s"); - chsnprintf(buf2, sizeof buf2, "%s%dns (%.2fm)", "STOP ", (uint16_t)(time_of_index(POINTS_COUNT-1) * 1e9), distance_of_index(POINTS_COUNT-1)); + chsnprintf(buf1, sizeof(buf1), " START 0s"); + chsnprintf(buf2, sizeof(buf2), "STOP %Fs (%Fm)", time_of_index(POINTS_COUNT-1), distance_of_index(POINTS_COUNT-1)); } setForegroundColor(DEFAULT_FG_COLOR); setBackgroundColor(DEFAULT_BG_COLOR); @@ -1836,7 +1700,7 @@ draw_frequencies(void) if (uistat.lever_mode == LM_SPAN || uistat.lever_mode == LM_CENTER) buf1[0] = S_SARROW[0]; ili9341_drawstring(buf1, OFFSETX, 232); - ili9341_drawstring(buf2, 205, 232); + ili9341_drawstring(buf2, 200, 232); } void @@ -1889,9 +1753,11 @@ draw_cal_status(void) void draw_battery_status(void) { + if (vbat<=0) + return; uint8_t string_buf[25]; - // Set battery color - setForegroundColor(vbat < BATTERY_WARNING_LEVEL ? DEFAULT_LOW_BAT_COLOR : DEFAULT_NORMAL_BAT_COLOR); + // Set battery color + setForegroundColor(vbat < BATTERY_WARNING_LEVEL ? DEFAULT_LOW_BAT_COLOR : DEFAULT_NORMAL_BAT_COLOR); setBackgroundColor(DEFAULT_BG_COLOR); // chsnprintf(string_buf, sizeof string_buf, "V:%d", vbat); // ili9341_drawstringV(string_buf, 1, 60); @@ -1902,15 +1768,15 @@ draw_battery_status(void) string_buf[x++] = 0b00100100; string_buf[x++] = 0b11111111; // string_buf[x++] = 0b10000001; - // Fill battery status - for (int power=BATTERY_TOP_LEVEL; power > BATTERY_BOTTOM_LEVEL; power-=100) - string_buf[x++] = (power > vbat) ? 0b10000001 : // Empty line - 0b11111111; // Full line - // Battery bottom -// string_buf[x++] = 0b10000001; - string_buf[x++] = 0b11111111; - // Draw battery - blit8BitWidthBitmap(0, 1, 8, x, string_buf); + // Fill battery status + for (int power=BATTERY_TOP_LEVEL; power > BATTERY_BOTTOM_LEVEL; power-=100) + string_buf[x++] = (power > vbat) ? 0b10000001 : // Empty line + 0b11111111; // Full line + // Battery bottom +// string_buf[x++] = 0b10000001; + string_buf[x++] = 0b11111111; + // Draw battery + blit8BitWidthBitmap(0, 1, 8, x, string_buf); } void