Refactor and introduce new shared helper function GetSLevelAttributes to unify S-level representations.

This commit is contained in:
Nunu 2023-12-31 16:02:01 +01:00
parent 59412c9f42
commit 5581e03c7c
4 changed files with 39 additions and 16 deletions

View File

@ -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;

22
misc.c
View File

@ -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;
}

9
misc.h
View File

@ -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

View File

@ -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