Refactor frequency rounding

This commit is contained in:
Krzysiek Egzmont
2023-10-24 01:58:40 +02:00
parent 8b41a1fa66
commit a510b89c6a
9 changed files with 33 additions and 75 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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