From 5581e03c7c087a7f051e003ecf3b87094cbbba9d Mon Sep 17 00:00:00 2001 From: Nunu Date: Sun, 31 Dec 2023 16:02:01 +0100 Subject: [PATCH] Refactor and introduce new shared helper function GetSLevelAttributes to unify S-level representations. --- app/spectrum.c | 2 -- misc.c | 22 ++++++++++++++++++++++ misc.h | 9 +++++++++ ui/main.c | 22 ++++++++-------------- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/spectrum.c b/app/spectrum.c index 35434d5..1863a7e 100644 --- a/app/spectrum.c +++ b/app/spectrum.c @@ -134,8 +134,6 @@ static uint8_t DBm2S(int dbm) { return i; } -static int Rssi2DBm(uint16_t rssi) { return (rssi >> 1) - 160; } - static uint16_t GetRegMenuValue(uint8_t st) { RegisterSpec s = registerSpecs[st]; return (BK4819_ReadRegister(s.num) >> s.offset) & s.mask; diff --git a/misc.c b/misc.c index 28b0b8d..2870c0e 100644 --- a/misc.c +++ b/misc.c @@ -293,4 +293,26 @@ bool IsValueInArray(int val, const int *arr, const int size) { return true; } return false; +} + +sLevelAttributes GetSLevelAttributes(const int16_t rssi, const uint32_t frequency) +{ + sLevelAttributes att; + // S0 .. base level + int16_t s0_dBm = -130; + + // adjust S-level for bands above HF + if(frequency > HF_FREQUENCY) + s0_dBm-=20; + + att.dBmRssi = Rssi2DBm(rssi); + att.sLevel = MIN(MAX((att.dBmRssi - s0_dBm) / 6, 0), 9); + att.over = MIN(MAX(att.dBmRssi - (s0_dBm + 9*6), 0), 99); + + return att; +} + +int Rssi2DBm(uint16_t rssi) +{ + return (rssi >> 1) - 160; } \ No newline at end of file diff --git a/misc.h b/misc.h index f6ec7b3..4d296af 100644 --- a/misc.h +++ b/misc.h @@ -205,6 +205,13 @@ extern ChannelFrequencyAttributes gMR_ChannelFrequencyAttributes[200]; extern ChannelAttributes_t gMR_ChannelAttributes[207]; +typedef struct +{ + uint8_t sLevel; + uint8_t over; + int dBmRssi; +} __attribute__((packed)) sLevelAttributes; + extern volatile uint16_t gBatterySaveCountdown_10ms; extern volatile bool gPowerSaveCountdownExpired; @@ -354,6 +361,8 @@ int32_t NUMBER_AddWithWraparound(int32_t Base, int32_t Add, int32_t LowerLimit, unsigned long StrToUL(const char * str); bool IsValueInArray(int val, const int *arr, const int size); +sLevelAttributes GetSLevelAttributes (const int16_t rssi, const uint32_t frequency); +int Rssi2DBm(uint16_t rssi); #endif diff --git a/ui/main.c b/ui/main.c index ca4a851..e5984cd 100644 --- a/ui/main.c +++ b/ui/main.c @@ -175,30 +175,24 @@ static void DisplayRSSIBar(const int16_t rssi, const bool now) if (now) memset(p_line, 0, LCD_WIDTH); + + sLevelAttributes sLevelAtt; - int16_t s0_dBm = -130; // S0 .. base level - - // adjust S-level for bands above HF - if(gRxVfo->freq_config_RX.Frequency > HF_FREQUENCY) - s0_dBm-=20; - - const int16_t rssi_dBm = (rssi / 2) - 160; - - const uint8_t s_level = MIN(MAX((rssi_dBm - s0_dBm) / 6, 0), 9); // S0 - S9 - uint8_t overS9dBm = MIN(MAX(rssi_dBm - (s0_dBm + 9*6), 0), 99); - uint8_t overS9Bars = MIN(overS9dBm/10, 4); + sLevelAtt = GetSLevelAttributes(rssi, gRxVfo->freq_config_RX.Frequency); + + uint8_t overS9Bars = MIN(sLevelAtt.over/10, 4); if(overS9Bars == 0) { - sprintf(str, "% 4d S%d", rssi_dBm, s_level); + sprintf(str, "% 4d S%d", sLevelAtt.dBmRssi, sLevelAtt.sLevel); } else { - sprintf(str, "% 4d %2d", rssi_dBm, overS9dBm); + sprintf(str, "% 4d %2d", sLevelAtt.dBmRssi, sLevelAtt.over); memcpy(p_line + 2 + 7*5, &plus, ARRAY_SIZE(plus)); } UI_PrintStringSmall(str, 2, 0, line); - DrawLevelBar(bar_x, line, s_level + overS9Bars); + DrawLevelBar(bar_x, line, sLevelAtt.sLevel + overS9Bars); } #else