From c0115c14b32e7fd0d2272d585da37312544c839d Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Tue, 26 Dec 2023 21:29:08 +0100 Subject: [PATCH] UI: removed use of floats when displaying VFO frequency, offset and step --- openrtx/src/ui/default/ui_main.c | 11 +++-- openrtx/src/ui/default/ui_menu.c | 84 ++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/openrtx/src/ui/default/ui_main.c b/openrtx/src/ui/default/ui_main.c index fdaf4cf4..ab1464fb 100644 --- a/openrtx/src/ui/default/ui_main.c +++ b/openrtx/src/ui/default/ui_main.c @@ -190,11 +190,16 @@ void _ui_drawModeInfo(ui_state_t* ui_state) void _ui_drawFrequency() { - unsigned long frequency = platform_getPttStatus() ? last_state.channel.tx_frequency - : last_state.channel.rx_frequency; + freq_t freq = platform_getPttStatus() ? last_state.channel.tx_frequency + : last_state.channel.rx_frequency; + // Print big numbers frequency + char freq_str[16] = {0}; + snprintf(freq_str, sizeof(freq_str), "%lu.%lu", (freq / 1000000), (freq % 1000000)); + stripTrailingZeroes(freq_str); + gfx_print(layout.line3_large_pos, layout.line3_large_font, TEXT_ALIGN_CENTER, - color_white, "%.7g", (float) frequency / 1000000.0f); + color_white, "%s", freq_str); } void _ui_drawVFOMiddleInput(ui_state_t* ui_state) diff --git a/openrtx/src/ui/default/ui_menu.c b/openrtx/src/ui/default/ui_menu.c index b4e2870d..fd904251 100644 --- a/openrtx/src/ui/default/ui_menu.c +++ b/openrtx/src/ui/default/ui_menu.c @@ -349,29 +349,61 @@ int _ui_getRadioValueName(char *buf, uint8_t max_len, uint8_t index) if(index >= settings_radio_num) return -1; - int32_t offset = 0; + // Only returning the sign + if(index == R_DIRECTION) + { + buf[0] = (last_state.channel.tx_frequency >= last_state.channel.rx_frequency) ? '+' : '-'; + buf[1] = '\0'; + + return 0; + } + + // Return an x.y string + uint32_t value = 0; switch(index) { case R_OFFSET: - offset = abs((int32_t)last_state.channel.tx_frequency - - (int32_t)last_state.channel.rx_frequency); - snprintf(buf, max_len, "%gMHz", (float) offset / 1000000.0f); - break; + { + uint32_t txFreq = last_state.channel.tx_frequency; + uint32_t rxFreq = last_state.channel.rx_frequency; - case R_DIRECTION: - buf[0] = (last_state.channel.tx_frequency >= last_state.channel.rx_frequency) ? '+' : '-'; - buf[1] = '\0'; + // Yes, we're basically reinventing the abs() here. The problem is + // that abs() works on signed integers and using it would mean + // casting values back and forth between signed and unsigned. + if(txFreq > rxFreq) + value = txFreq - rxFreq; + else + value = rxFreq - txFreq; + } break; case R_STEP: - // Print in kHz if it is smaller than 1MHz - if (freq_steps[last_state.step_index] < 1000000) - snprintf(buf, max_len, "%gkHz", (float) freq_steps[last_state.step_index] / 1000.0f); - else - snprintf(buf, max_len, "%gMHz", (float) freq_steps[last_state.step_index] / 1000000.0f); + value = freq_steps[last_state.step_index]; break; } + uint32_t div = 1; + char prefix = ' '; + + if(value >= 1000000) + { + prefix = 'M'; + div = 1000000; + } + else if(value >= 1000) + { + prefix = 'k'; + div = 1000; + } + + // NOTE: casts are there only to squelch -Wformat warnings on the + // snprintf. + char str[16]; + snprintf(str, sizeof(str), "%u.%u", (unsigned int)(value / div), + (unsigned int)(value % div)); + stripTrailingZeroes(str); + snprintf(buf, max_len, "%s%cHz", str, prefix); + return 0; } @@ -973,16 +1005,28 @@ void _ui_drawSettingsRadio(ui_state_t* ui_state) gfx_drawRect(rect_origin, rect_width, rect_height, color_white, false); // Print frequency with the most sensible unit - if (ui_state->new_offset < 1000) - snprintf(buf, 17, "%dHz", ui_state->new_offset); - else if (ui_state->new_offset < 1000000) - snprintf(buf, 17, "%gkHz", (float) ui_state->new_offset / 1000.0f); - else - snprintf(buf, 17, "%gMHz", (float) ui_state->new_offset / 1000000.0f); + char prefix = ' '; + uint32_t div = 1; + if(ui_state->new_offset >= 1000000) + { + prefix = 'M'; + div = 1000000; + } + else if(ui_state->new_offset >= 1000) + { + prefix = 'k'; + div = 1000; + } + + // NOTE: casts are there only to squelch -Wformat warnings on the + // snprintf. + snprintf(buf, sizeof(buf), "%u.%u", (unsigned int)(ui_state->new_offset / div), + (unsigned int)(ui_state->new_offset % div)); + stripTrailingZeroes(buf); gfx_printLine(1, 1, layout.top_h, CONFIG_SCREEN_HEIGHT - layout.bottom_h, layout.horizontal_pad, layout.input_font, - TEXT_ALIGN_CENTER, color_white, buf); + TEXT_ALIGN_CENTER, color_white, "%s%cHz", buf, prefix); } else {