From a510b89c6a6743b1272b9da8d08bd41e09ec93e4 Mon Sep 17 00:00:00 2001 From: Krzysiek Egzmont Date: Tue, 24 Oct 2023 01:58:40 +0200 Subject: [PATCH] Refactor frequency rounding --- app/aircopy.c | 3 +-- app/app.c | 22 ++++------------------ app/app.h | 2 +- app/main.c | 9 ++------- app/menu.c | 4 ++-- app/scanner.c | 31 ++++++++----------------------- frequencies.c | 26 +++++++++++--------------- frequencies.h | 2 +- radio.c | 9 +++------ 9 files changed, 33 insertions(+), 75 deletions(-) diff --git a/app/aircopy.c b/app/aircopy.c index 1d6daf6..3dcbe82 100644 --- a/app/aircopy.c +++ b/app/aircopy.c @@ -141,8 +141,7 @@ static void AIRCOPY_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) gAnotherVoiceID = (VOICE_ID_t)Key; #endif gRxVfo->Band = i; - Frequency += 75; - Frequency = FREQUENCY_FloorToStep(Frequency, gRxVfo->StepFrequency, 0); + Frequency = FREQUENCY_RoundToStep(Frequency, gRxVfo->StepFrequency); gRxVfo->freq_config_RX.Frequency = Frequency; gRxVfo->freq_config_TX.Frequency = Frequency; RADIO_ConfigureSquelchAndOutputPower(gRxVfo); diff --git a/app/app.c b/app/app.c index 5f955ff..9277bed 100644 --- a/app/app.c +++ b/app/app.c @@ -597,28 +597,14 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix) gUpdateStatus = true; } -uint32_t APP_SetFrequencyByStep(VFO_Info_t *pInfo, int8_t Step) +uint32_t APP_SetFrequencyByStep(VFO_Info_t *pInfo, int8_t direction) { - uint32_t Frequency = pInfo->freq_config_RX.Frequency + (Step * pInfo->StepFrequency); - - if (pInfo->StepFrequency == 833) - { - const uint32_t Lower = frequencyBandTable[pInfo->Band].lower; - const uint32_t Delta = Frequency - Lower; - uint32_t Base = (Delta / 2500) * 2500; - const uint32_t Index = ((Delta - Base) % 2500) / 833; - - if (Index == 2) - Base++; - - Frequency = Lower + Base + (Index * 833); - } + uint32_t Frequency = FREQUENCY_RoundToStep(pInfo->freq_config_RX.Frequency + (direction * pInfo->StepFrequency), pInfo->StepFrequency); if (Frequency >= frequencyBandTable[pInfo->Band].upper) Frequency = frequencyBandTable[pInfo->Band].lower; - else - if (Frequency < frequencyBandTable[pInfo->Band].lower) - Frequency = FREQUENCY_FloorToStep(frequencyBandTable[pInfo->Band].upper, pInfo->StepFrequency, frequencyBandTable[pInfo->Band].lower); + else if (Frequency < frequencyBandTable[pInfo->Band].lower) + Frequency = FREQUENCY_RoundToStep(frequencyBandTable[pInfo->Band].upper, pInfo->StepFrequency); return Frequency; } diff --git a/app/app.h b/app/app.h index 9406b00..e0b2e43 100644 --- a/app/app.h +++ b/app/app.h @@ -30,7 +30,7 @@ extern const uint8_t orig_pga; void APP_EndTransmission(void); void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix); -uint32_t APP_SetFrequencyByStep(VFO_Info_t *pInfo, int8_t Step); +uint32_t APP_SetFrequencyByStep(VFO_Info_t *pInfo, int8_t direction); void APP_Update(void); void APP_TimeSlice10ms(void); void APP_TimeSlice500ms(void); diff --git a/app/main.c b/app/main.c index 2e8b9e7..3c76e04 100644 --- a/app/main.c +++ b/app/main.c @@ -189,8 +189,6 @@ static void processFKeyFunction(const KEY_Code_t Key, const bool beep) if (gEeprom.VFO_OPEN) #endif { - uint8_t Channel; - if (IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) { // swap to frequency mode gEeprom.ScreenChannel[Vfo] = gEeprom.FreqChannel[gEeprom.TX_VFO]; @@ -202,7 +200,7 @@ static void processFKeyFunction(const KEY_Code_t Key, const bool beep) break; } - Channel = RADIO_FindNextChannel(gEeprom.MrChannel[gEeprom.TX_VFO], 1, false, 0); + uint8_t Channel = RADIO_FindNextChannel(gEeprom.MrChannel[gEeprom.TX_VFO], 1, false, 0); if (Channel != 0xFF) { // swap to channel mode gEeprom.ScreenChannel[Vfo] = Channel; @@ -444,10 +442,7 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) RADIO_ConfigureChannel(Vfo, VFO_CONFIGURE_RELOAD); } -// Frequency += 75; // is this meant to be rounding ? - Frequency += gTxVfo->StepFrequency / 2; // no idea, but this is - - Frequency = FREQUENCY_FloorToStep(Frequency, gTxVfo->StepFrequency, frequencyBandTable[gTxVfo->Band].lower); + Frequency = FREQUENCY_RoundToStep(Frequency, gTxVfo->StepFrequency); if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) { // clamp the frequency to the limit diff --git a/app/menu.c b/app/menu.c index 125b6b5..f17fe1d 100644 --- a/app/menu.c +++ b/app/menu.c @@ -1268,7 +1268,7 @@ static void MENU_Key_0_to_9(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) #endif Frequency = StrToUL(INPUTBOX_GetAscii())*100; - gSubMenuSelection = FREQUENCY_FloorToStep(Frequency + 75, gTxVfo->StepFrequency, 0); + gSubMenuSelection = FREQUENCY_RoundToStep(Frequency, gTxVfo->StepFrequency); gInputBoxIndex = 0; return; @@ -1678,7 +1678,7 @@ static void MENU_Key_UP_DOWN(bool bKeyPressed, bool bKeyHeld, int8_t Direction) else Offset = 0; - gSubMenuSelection = FREQUENCY_FloorToStep(Offset, gTxVfo->StepFrequency, 0); + gSubMenuSelection = FREQUENCY_RoundToStep(Offset, gTxVfo->StepFrequency); gRequestDisplayScreen = DISPLAY_MENU; return; } diff --git a/app/scanner.c b/app/scanner.c index d717557..98defac 100644 --- a/app/scanner.c +++ b/app/scanner.c @@ -167,34 +167,19 @@ static void SCANNER_Key_MENU(bool bKeyPressed, bool bKeyHeld) case 0: if (!gScanSingleFrequency) { - int16_t Delta625; - uint32_t Freq250 = FREQUENCY_FloorToStep(gScanFrequency, 250, 0); - uint32_t Freq625 = FREQUENCY_FloorToStep(gScanFrequency, 625, 0); - int16_t Delta250 = (int16_t)gScanFrequency - (int16_t)Freq250; + uint32_t freq250 = FREQUENCY_RoundToStep(gScanFrequency, 250); + uint32_t freq625 = FREQUENCY_RoundToStep(gScanFrequency, 625); - if (125 < Delta250) - { - Delta250 = 250 - Delta250; - Freq250 += 250; - } + uint32_t diff250 = gScanFrequency > freq250 ? gScanFrequency - freq250 : freq250 - gScanFrequency; + uint32_t diff625 = gScanFrequency > freq625 ? gScanFrequency - freq625 : freq625 - gScanFrequency; - Delta625 = (int16_t)gScanFrequency - (int16_t)Freq625; - - if (312 < Delta625) - { - Delta625 = 625 - Delta625; - Freq625 += 625; - } - - if (Delta625 < Delta250) - { + if(diff250 > diff625) { gStepSetting = STEP_6_25kHz; - gScanFrequency = Freq625; + gScanFrequency = freq625; } - else - { + else { gStepSetting = STEP_2_5kHz; - gScanFrequency = Freq250; + gScanFrequency = freq250; } } diff --git a/frequencies.c b/frequencies.c index ca3fe86..e91ea18 100644 --- a/frequencies.c +++ b/frequencies.c @@ -100,25 +100,21 @@ uint8_t FREQUENCY_CalculateOutputPower(uint8_t TxpLow, uint8_t TxpMid, uint8_t T return TxpMid; } -uint32_t FREQUENCY_FloorToStep(uint32_t Upper, uint32_t Step, uint32_t Lower) +static int32_t rnd(int32_t freq, uint16_t step) { - uint32_t Index; +return (freq + (step + 1) / 2) / step * step; +} - if (Step == 833) - { - const uint32_t Delta = Upper - Lower; - uint32_t Base = (Delta / 2500) * 2500; - const uint32_t Index = ((Delta - Base) % 2500) / 833; - - if (Index == 2) - Base++; - - return Lower + Base + (Index * 833); +uint32_t FREQUENCY_RoundToStep(uint32_t freq, uint16_t step) +{ + if(step == 833) { + uint32_t base = freq/2500*2500; + int f = rnd(freq - base, step); + int x = f / step; + return base + f + (x == 3); } - Index = (Upper - Lower) / Step; - - return Lower + (Step * Index); + return rnd(freq, step); } int TX_freq_check(const uint32_t Frequency) diff --git a/frequencies.h b/frequencies.h index 5b38dd7..f8c54cc 100644 --- a/frequencies.h +++ b/frequencies.h @@ -76,7 +76,7 @@ extern const uint16_t StepFrequencyTable[7]; FREQUENCY_Band_t FREQUENCY_GetBand(uint32_t Frequency); uint8_t FREQUENCY_CalculateOutputPower(uint8_t TxpLow, uint8_t TxpMid, uint8_t TxpHigh, int32_t LowerLimit, int32_t Middle, int32_t UpperLimit, int32_t Frequency); -uint32_t FREQUENCY_FloorToStep(uint32_t Upper, uint32_t Step, uint32_t Lower); +uint32_t FREQUENCY_RoundToStep(uint32_t freq, uint16_t step); int TX_freq_check(const uint32_t Frequency); int RX_freq_check(const uint32_t Frequency); diff --git a/radio.c b/radio.c index 21aaaf9..c4c3399 100644 --- a/radio.c +++ b/radio.c @@ -361,10 +361,8 @@ void RADIO_ConfigureChannel(const unsigned int VFO, const unsigned int configure Frequency = pRadio->freq_config_RX.Frequency; -#if 1 // fix previously set incorrect band Band = FREQUENCY_GetBand(Frequency); -#endif if (Frequency < frequencyBandTable[Band].lower) Frequency = frequencyBandTable[Band].lower; @@ -373,15 +371,14 @@ void RADIO_ConfigureChannel(const unsigned int VFO, const unsigned int configure Frequency = frequencyBandTable[Band].upper; else if (Channel >= FREQ_CHANNEL_FIRST) - Frequency = FREQUENCY_FloorToStep(Frequency, gEeprom.VfoInfo[VFO].StepFrequency, frequencyBandTable[Band].lower); + Frequency = FREQUENCY_RoundToStep(Frequency, gEeprom.VfoInfo[VFO].StepFrequency); pRadio->freq_config_RX.Frequency = Frequency; if (Frequency >= 10800000 && Frequency < 13600000) gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY_DIRECTION = TX_OFFSET_FREQUENCY_DIRECTION_OFF; - else - if (Channel > MR_CHANNEL_LAST) - gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY = FREQUENCY_FloorToStep(gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY, gEeprom.VfoInfo[VFO].StepFrequency, 0); + else if (Channel > MR_CHANNEL_LAST) + gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY = FREQUENCY_RoundToStep(gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY, gEeprom.VfoInfo[VFO].StepFrequency); RADIO_ApplyOffset(pRadio);