From d5ebaab76d9d34c8c7bff2b4af2829f0f9210edd Mon Sep 17 00:00:00 2001 From: Wouter van Gulik Date: Sat, 11 Jul 2020 21:45:59 +0200 Subject: [PATCH] Fix two bugs regarding the shown marker frequency: The function frequency_string_short did not append 'Mhz' properly for 1-9Mhz cell_draw_marker_info stored frequency in a int32_t instead of freqHz_t, which caused sign conversion after 2.1Ghz, making marker reading beyond that point bogus. Note that this is only visible with multiple markers. --- plot.cpp | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/plot.cpp b/plot.cpp index e7cd74d..f43aaca 100644 --- a/plot.cpp +++ b/plot.cpp @@ -16,7 +16,7 @@ static void cell_draw_marker_info(int x0, int y0); void frequency_string(char *buf, size_t len, freqHz_t freq); -void frequency_string_short(char *buf, size_t len, freqHz_t freq, char prefix); +int frequency_string_short(char *buf, size_t len, freqHz_t freq, char prefix); void markmap_all_markers(void); //#define GRID_COLOR 0x0863 @@ -97,7 +97,7 @@ void update_grid(void) fspan = -frequency1; fstart = frequency0 - fspan/2; } - + while (gdigit > 100) { grid = 5 * gdigit; if (fspan / grid >= 4) @@ -229,7 +229,7 @@ smith_grid2(int x, int y, float scale) // offset to center x -= P_CENTER_X; y -= P_CENTER_Y; - + // outer circle d = circle_inout(x, y, P_RADIUS); if (d < 0) @@ -325,7 +325,7 @@ smith_grid3(int x, int y) // offset to center x -= P_CENTER_X; y -= P_CENTER_Y; - + // outer circle d = circle_inout(x, y, P_RADIUS); if (d < 0) @@ -1595,7 +1595,7 @@ cell_draw_marker_info(int x0, int y0) cell_drawstring(buf, xpos, ypos); xpos += 3*FONT_WIDTH + 3; //trace_get_info(t, buf, sizeof buf); - int32_t freq = freqAt(markers[mk].index); + freqHz_t freq = freqAt(markers[mk].index); if (uistat.marker_delta && mk != active_marker) { freq -= freqAt(markers[active_marker].index); frequency_string_short(buf, sizeof buf, freq, S_DELTA[0]); @@ -1691,6 +1691,11 @@ cell_draw_marker_info(int x0, int y0) } } +/* Prints a full frequency: + * 500.000 Khz + * 10.000 000 Mhz + * 3000.000 000 Mhz + */ void frequency_string(char *buf, size_t len, freqHz_t freq) { @@ -1713,7 +1718,12 @@ frequency_string(char *buf, size_t len, freqHz_t freq) } } -void +/* Prints a shorter/compacter frequency: + * 500.000KHz + * 10.000000Mhz + * 3000.0000Mhz + */ +int frequency_string_short(char *b, size_t len, freqHz_t freq, char prefix) { char *buf = b; @@ -1727,17 +1737,29 @@ frequency_string_short(char *b, size_t len, freqHz_t freq, char prefix) len -= 1; } if (freq < 1000) { - chsnprintf(buf, len, "%d Hz", (int)freq); + return chsnprintf(buf, len, "%d Hz", (int)freq); } else if (freq < 1000000) { - chsnprintf(buf, len, "%d.%03dkHz", + return 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"); } + size_t wr = chsnprintf(buf, len, "%d.%06d", + (int)(freq / 1000000), + (int)(freq % 1000000)); + + // Do not add 'Mhz' if it would overwrite all characters + if(len < 4) + return len - 1; + + // Make sure we have space left for 'Mhz' + if(wr > len - 4) { + wr = len - 4; + } + // Overwrite last digits, if > 10Mhz (aka 9 digits) + if (wr > 9 && len > 9 + 4) + wr = 9; + strcpy(b + wr, "MHz"); + return wr + 3; } void padString(char* s, int len, char c = ' ') {