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; gAnotherVoiceID = (VOICE_ID_t)Key;
#endif #endif
gRxVfo->Band = i; gRxVfo->Band = i;
Frequency += 75; Frequency = FREQUENCY_RoundToStep(Frequency, gRxVfo->StepFrequency);
Frequency = FREQUENCY_FloorToStep(Frequency, gRxVfo->StepFrequency, 0);
gRxVfo->freq_config_RX.Frequency = Frequency; gRxVfo->freq_config_RX.Frequency = Frequency;
gRxVfo->freq_config_TX.Frequency = Frequency; gRxVfo->freq_config_TX.Frequency = Frequency;
RADIO_ConfigureSquelchAndOutputPower(gRxVfo); RADIO_ConfigureSquelchAndOutputPower(gRxVfo);

View File

@@ -597,28 +597,14 @@ void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix)
gUpdateStatus = true; 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); uint32_t Frequency = FREQUENCY_RoundToStep(pInfo->freq_config_RX.Frequency + (direction * pInfo->StepFrequency), 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);
}
if (Frequency >= frequencyBandTable[pInfo->Band].upper) if (Frequency >= frequencyBandTable[pInfo->Band].upper)
Frequency = frequencyBandTable[pInfo->Band].lower; Frequency = frequencyBandTable[pInfo->Band].lower;
else else if (Frequency < frequencyBandTable[pInfo->Band].lower)
if (Frequency < frequencyBandTable[pInfo->Band].lower) Frequency = FREQUENCY_RoundToStep(frequencyBandTable[pInfo->Band].upper, pInfo->StepFrequency);
Frequency = FREQUENCY_FloorToStep(frequencyBandTable[pInfo->Band].upper, pInfo->StepFrequency, frequencyBandTable[pInfo->Band].lower);
return Frequency; return Frequency;
} }

View File

@@ -30,7 +30,7 @@ extern const uint8_t orig_pga;
void APP_EndTransmission(void); void APP_EndTransmission(void);
void APP_StartListening(FUNCTION_Type_t Function, const bool reset_am_fix); 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_Update(void);
void APP_TimeSlice10ms(void); void APP_TimeSlice10ms(void);
void APP_TimeSlice500ms(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) if (gEeprom.VFO_OPEN)
#endif #endif
{ {
uint8_t Channel;
if (IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) if (IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE))
{ // swap to frequency mode { // swap to frequency mode
gEeprom.ScreenChannel[Vfo] = gEeprom.FreqChannel[gEeprom.TX_VFO]; gEeprom.ScreenChannel[Vfo] = gEeprom.FreqChannel[gEeprom.TX_VFO];
@@ -202,7 +200,7 @@ static void processFKeyFunction(const KEY_Code_t Key, const bool beep)
break; 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) if (Channel != 0xFF)
{ // swap to channel mode { // swap to channel mode
gEeprom.ScreenChannel[Vfo] = Channel; 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); RADIO_ConfigureChannel(Vfo, VFO_CONFIGURE_RELOAD);
} }
// Frequency += 75; // is this meant to be rounding ? Frequency = FREQUENCY_RoundToStep(Frequency, gTxVfo->StepFrequency);
Frequency += gTxVfo->StepFrequency / 2; // no idea, but this is
Frequency = FREQUENCY_FloorToStep(Frequency, gTxVfo->StepFrequency, frequencyBandTable[gTxVfo->Band].lower);
if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower)
{ // clamp the frequency to the limit { // 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 #endif
Frequency = StrToUL(INPUTBOX_GetAscii())*100; Frequency = StrToUL(INPUTBOX_GetAscii())*100;
gSubMenuSelection = FREQUENCY_FloorToStep(Frequency + 75, gTxVfo->StepFrequency, 0); gSubMenuSelection = FREQUENCY_RoundToStep(Frequency, gTxVfo->StepFrequency);
gInputBoxIndex = 0; gInputBoxIndex = 0;
return; return;
@@ -1678,7 +1678,7 @@ static void MENU_Key_UP_DOWN(bool bKeyPressed, bool bKeyHeld, int8_t Direction)
else else
Offset = 0; Offset = 0;
gSubMenuSelection = FREQUENCY_FloorToStep(Offset, gTxVfo->StepFrequency, 0); gSubMenuSelection = FREQUENCY_RoundToStep(Offset, gTxVfo->StepFrequency);
gRequestDisplayScreen = DISPLAY_MENU; gRequestDisplayScreen = DISPLAY_MENU;
return; return;
} }

View File

@@ -167,34 +167,19 @@ static void SCANNER_Key_MENU(bool bKeyPressed, bool bKeyHeld)
case 0: case 0:
if (!gScanSingleFrequency) if (!gScanSingleFrequency)
{ {
int16_t Delta625; uint32_t freq250 = FREQUENCY_RoundToStep(gScanFrequency, 250);
uint32_t Freq250 = FREQUENCY_FloorToStep(gScanFrequency, 250, 0); uint32_t freq625 = FREQUENCY_RoundToStep(gScanFrequency, 625);
uint32_t Freq625 = FREQUENCY_FloorToStep(gScanFrequency, 625, 0);
int16_t Delta250 = (int16_t)gScanFrequency - (int16_t)Freq250;
if (125 < Delta250) uint32_t diff250 = gScanFrequency > freq250 ? gScanFrequency - freq250 : freq250 - gScanFrequency;
{ uint32_t diff625 = gScanFrequency > freq625 ? gScanFrequency - freq625 : freq625 - gScanFrequency;
Delta250 = 250 - Delta250;
Freq250 += 250;
}
Delta625 = (int16_t)gScanFrequency - (int16_t)Freq625; if(diff250 > diff625) {
if (312 < Delta625)
{
Delta625 = 625 - Delta625;
Freq625 += 625;
}
if (Delta625 < Delta250)
{
gStepSetting = STEP_6_25kHz; gStepSetting = STEP_6_25kHz;
gScanFrequency = Freq625; gScanFrequency = freq625;
} }
else else {
{
gStepSetting = STEP_2_5kHz; gStepSetting = STEP_2_5kHz;
gScanFrequency = Freq250; gScanFrequency = freq250;
} }
} }

View File

@@ -100,25 +100,21 @@ uint8_t FREQUENCY_CalculateOutputPower(uint8_t TxpLow, uint8_t TxpMid, uint8_t T
return TxpMid; 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) uint32_t FREQUENCY_RoundToStep(uint32_t freq, uint16_t step)
{ {
const uint32_t Delta = Upper - Lower; if(step == 833) {
uint32_t Base = (Delta / 2500) * 2500; uint32_t base = freq/2500*2500;
const uint32_t Index = ((Delta - Base) % 2500) / 833; int f = rnd(freq - base, step);
int x = f / step;
if (Index == 2) return base + f + (x == 3);
Base++;
return Lower + Base + (Index * 833);
} }
Index = (Upper - Lower) / Step; return rnd(freq, step);
return Lower + (Step * Index);
} }
int TX_freq_check(const uint32_t Frequency) int TX_freq_check(const uint32_t Frequency)

View File

@@ -76,7 +76,7 @@ extern const uint16_t StepFrequencyTable[7];
FREQUENCY_Band_t FREQUENCY_GetBand(uint32_t Frequency); 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); 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 TX_freq_check(const uint32_t Frequency);
int RX_freq_check(const uint32_t Frequency); int RX_freq_check(const uint32_t Frequency);

View File

@@ -361,10 +361,8 @@ void RADIO_ConfigureChannel(const unsigned int VFO, const unsigned int configure
Frequency = pRadio->freq_config_RX.Frequency; Frequency = pRadio->freq_config_RX.Frequency;
#if 1
// fix previously set incorrect band // fix previously set incorrect band
Band = FREQUENCY_GetBand(Frequency); Band = FREQUENCY_GetBand(Frequency);
#endif
if (Frequency < frequencyBandTable[Band].lower) if (Frequency < frequencyBandTable[Band].lower)
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; Frequency = frequencyBandTable[Band].upper;
else else
if (Channel >= FREQ_CHANNEL_FIRST) 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; pRadio->freq_config_RX.Frequency = Frequency;
if (Frequency >= 10800000 && Frequency < 13600000) if (Frequency >= 10800000 && Frequency < 13600000)
gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY_DIRECTION = TX_OFFSET_FREQUENCY_DIRECTION_OFF; gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY_DIRECTION = TX_OFFSET_FREQUENCY_DIRECTION_OFF;
else else if (Channel > MR_CHANNEL_LAST)
if (Channel > MR_CHANNEL_LAST) gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY = FREQUENCY_RoundToStep(gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY, gEeprom.VfoInfo[VFO].StepFrequency);
gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY = FREQUENCY_FloorToStep(gEeprom.VfoInfo[VFO].TX_OFFSET_FREQUENCY, gEeprom.VfoInfo[VFO].StepFrequency, 0);
RADIO_ApplyOffset(pRadio); RADIO_ApplyOffset(pRadio);