From 82ddbcd3750eecd2d93a8e6490f6ba2d9b8750b8 Mon Sep 17 00:00:00 2001 From: Juan Antonio Date: Fri, 8 Dec 2023 23:15:54 +0100 Subject: [PATCH] Refactor stepFruencyTable & drop ARRAY_SIZE for frequencyBandTable --- app/aircopy.c | 2 +- app/main.c | 6 +++--- app/menu.c | 3 +-- app/spectrum.c | 3 ++- app/spectrum.h | 1 - frequencies.c | 41 +++++++++++++++++++++++++++++++++-------- frequencies.h | 14 ++++++++------ radio.c | 28 +++++++++++++--------------- settings.h | 2 +- 9 files changed, 62 insertions(+), 38 deletions(-) diff --git a/app/aircopy.c b/app/aircopy.c index 9f964fa68..a7340aa8e 100644 --- a/app/aircopy.c +++ b/app/aircopy.c @@ -133,7 +133,7 @@ static void AIRCOPY_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) gInputBoxIndex = 0; Frequency = StrToUL(INPUTBOX_GetAscii()) * 100; - for (i = 0; i < ARRAY_SIZE(frequencyBandTable); i++) + for (i = 0; i < BAND_N_ELEM; i++) { if (Frequency >= frequencyBandTable[i].lower && Frequency < frequencyBandTable[i].upper) { diff --git a/app/main.c b/app/main.c index 734edf493..67fda3851 100644 --- a/app/main.c +++ b/app/main.c @@ -152,7 +152,7 @@ static void processFKeyFunction(const KEY_Code_t Key, const bool beep) if (gTxVfo->Band == BAND5_350MHz && !gSetting_350EN) { // skip if not enabled gTxVfo->Band += 1; - } else if (gTxVfo->Band >= BAND_LAST_ELEMENT){ + } else if (gTxVfo->Band >= BAND_N_ELEM){ // go arround if overflowed gTxVfo->Band = BAND1_50MHz; } @@ -380,9 +380,9 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) Frequency = (Frequency < center) ? BX4819_band1.upper : BX4819_band2.lower; } else - if (Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) + if (Frequency > frequencyBandTable[BAND_N_ELEM - 1].upper) { - Frequency = frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper; + Frequency = frequencyBandTable[BAND_N_ELEM - 1].upper; } const FREQUENCY_Band_t band = FREQUENCY_GetBand(Frequency); diff --git a/app/menu.c b/app/menu.c index 02184ed3e..8e3a723bf 100644 --- a/app/menu.c +++ b/app/menu.c @@ -40,7 +40,6 @@ #endif #include "ui/inputbox.h" #include "ui/menu.h" -#include "ui/menu.h" #include "ui/ui.h" #ifndef ARRAY_SIZE @@ -123,7 +122,7 @@ int MENU_GetLimits(uint8_t menu_id, int32_t *pMin, int32_t *pMax) case MENU_STEP: *pMin = 0; - *pMax = ARRAY_SIZE(gStepFrequencyTable) - 1; + *pMax = STEP_N_ELEM - 1; break; case MENU_ABR: diff --git a/app/spectrum.c b/app/spectrum.c index 4efc9d768..f8326ce4e 100644 --- a/app/spectrum.c +++ b/app/spectrum.c @@ -18,6 +18,7 @@ #include "am_fix.h" #include "audio.h" #include "driver/backlight.h" +#include "frequencies.h" #include "ui/helper.h" #include "ui/main.h" @@ -28,7 +29,7 @@ struct FrequencyBandInfo { }; #define F_MIN frequencyBandTable[0].lower -#define F_MAX frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper +#define F_MAX frequencyBandTable[BAND_N_ELEM - 1].upper const uint16_t RSSI_MAX_VALUE = 65535; diff --git a/app/spectrum.h b/app/spectrum.h index 772e62b97..8c087296a 100644 --- a/app/spectrum.h +++ b/app/spectrum.h @@ -29,7 +29,6 @@ #include "../driver/systick.h" #include "../external/printf/printf.h" #include "../font.h" -#include "../frequencies.h" #include "../helper/battery.h" #include "../misc.h" #include "../radio.h" diff --git a/frequencies.c b/frequencies.c index 7c174c4ae..bbbd4ffb9 100644 --- a/frequencies.c +++ b/frequencies.c @@ -17,6 +17,7 @@ #include "frequencies.h" #include "misc.h" #include "settings.h" +#include // the BK4819 has 2 bands it covers, 18MHz ~ 630MHz and 760MHz ~ 1300MHz @@ -61,9 +62,31 @@ const freq_band_table_t frequencyBandTable[] = #endif +// this order of steps has to be preserved for backwards compatibility with other/stock firmwares const uint16_t gStepFrequencyTable[] = { - 250, 500, 625, 1000, 1250, 2500, 833, - 1, 5, 10, 25, 50, 100, 125, 1500, 3000, 5000, 10000, 12500, 25000, 50000 +// standard steps + [STEP_2_5kHz] = 250, + [STEP_5kHz] = 500, + [STEP_6_25kHz] = 625, + [STEP_10kHz] = 1000, + [STEP_12_5kHz] = 1250, + [STEP_25kHz] = 2500, + [STEP_8_33kHz] = 833, +// custom steps + [STEP_0_01kHz] = 1, + [STEP_0_05kHz] = 5, + [STEP_0_1kHz] = 10, + [STEP_0_25kHz] = 25, + [STEP_0_5kHz] = 50, + [STEP_1kHz] = 100, + [STEP_1_25kHz] = 125, + [STEP_15kHz] = 1500, + [STEP_30kHz] = 3000, + [STEP_50kHz] = 5000, + [STEP_100kHz] = 10000, + [STEP_125kHz] = 12500, + [STEP_250kHz] = 25000, + [STEP_500kHz] = 50000 }; @@ -86,15 +109,16 @@ uint32_t FREQUENCY_GetSortedIdxFromStepIdx(uint8_t stepIdx) return 0; } +static_assert(ARRAY_SIZE(gStepFrequencyTable) == STEP_N_ELEM); + + FREQUENCY_Band_t FREQUENCY_GetBand(uint32_t Frequency) { - for (int band = ARRAY_SIZE(frequencyBandTable) - 1; band >= 0; band--) + for (int32_t band = BAND_N_ELEM - 1; band >= 0; band--) if (Frequency >= frequencyBandTable[band].lower) -// if (Frequency < frequencyBandTable[band].upper) return (FREQUENCY_Band_t)band; return BAND1_50MHz; -// return BAND_NONE; } 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) @@ -124,6 +148,7 @@ uint32_t FREQUENCY_RoundToStep(uint32_t freq, uint16_t step) int chno = (freq - base) / 700; // convert entered aviation 8.33Khz channel number scheme to actual frequency. return base + (chno * 833) + (chno == 3); } + if(step == 1) return freq; if(step >= 1000) @@ -135,8 +160,8 @@ int32_t TX_freq_check(const uint32_t Frequency) { // return '0' if TX frequency is allowed // otherwise return '-1' - if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) - return -1; // not allowed outside this range + if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[BAND_N_ELEM - 1].upper) + return 1; // not allowed outside this range if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) return -1; // BX chip does not work in this range @@ -212,7 +237,7 @@ int32_t RX_freq_check(const uint32_t Frequency) { // return '0' if RX frequency is allowed // otherwise return '-1' - if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) + if (Frequency < frequencyBandTable[0].lower || Frequency > frequencyBandTable[BAND_N_ELEM - 1].upper) return -1; if (Frequency >= BX4819_band1.upper && Frequency < BX4819_band2.lower) diff --git a/frequencies.h b/frequencies.h index ddc46d6e5..06955b3e7 100644 --- a/frequencies.h +++ b/frequencies.h @@ -29,22 +29,22 @@ typedef struct { extern const freq_band_table_t BX4819_band1; extern const freq_band_table_t BX4819_band2; -extern const freq_band_table_t frequencyBandTable[7]; - typedef enum { - BAND_NONE = -1, - BAND1_50MHz = 0, + BAND_NONE = -1, + BAND1_50MHz = 0, BAND2_108MHz, BAND3_137MHz, BAND4_174MHz, BAND5_350MHz, BAND6_400MHz, BAND7_470MHz, - BAND_LAST_ELEMENT //keep this guard as last element + BAND_N_ELEM } FREQUENCY_Band_t; +extern const freq_band_table_t frequencyBandTable[]; typedef enum { +// standard steps STEP_2_5kHz, STEP_5kHz, STEP_6_25kHz, @@ -52,6 +52,7 @@ typedef enum { STEP_12_5kHz, STEP_25kHz, STEP_8_33kHz, +// custom steps STEP_0_01kHz, STEP_0_05kHz, STEP_0_1kHz, @@ -66,10 +67,11 @@ typedef enum { STEP_125kHz, STEP_250kHz, STEP_500kHz, + STEP_N_ELEM } STEP_Setting_t; -extern const uint16_t gStepFrequencyTable[21]; +extern const uint16_t gStepFrequencyTable[]; #ifdef ENABLE_NOAA extern const uint32_t NoaaFrequencyTable[10]; diff --git a/radio.c b/radio.c index a236777b2..7a6b7747e 100644 --- a/radio.c +++ b/radio.c @@ -46,7 +46,7 @@ const char gModulationStr[MODULATION_UKNOWN][4] = { [MODULATION_FM]="FM", [MODULATION_AM]="AM", [MODULATION_USB]="USB", - + #ifdef ENABLE_BYP_RAW_DEMODULATORS [MODULATION_BYP]="BYP", [MODULATION_RAW]="RAW" @@ -145,7 +145,7 @@ void RADIO_InitInfo(VFO_Info_t *pInfo, const uint8_t ChannelSave, const uint32_t pInfo->Modulation = MODULATION_AM; else pInfo->Modulation = MODULATION_FM; - + RADIO_ConfigureSquelchAndOutputPower(pInfo); } @@ -249,11 +249,11 @@ void RADIO_ConfigureChannel(const unsigned int VFO, const unsigned int configure pVfo->TX_OFFSET_FREQUENCY_DIRECTION = tmp; tmp = data[3] >> 4; if (tmp >= MODULATION_UKNOWN) - tmp = MODULATION_FM; + tmp = MODULATION_FM; pVfo->Modulation = tmp; tmp = data[6]; - if (tmp >= ARRAY_SIZE(gStepFrequencyTable)) + if (tmp >= STEP_N_ELEM) tmp = STEP_12_5kHz; pVfo->STEP_SETTING = tmp; pVfo->StepFrequency = gStepFrequencyTable[tmp]; @@ -412,13 +412,12 @@ void RADIO_ConfigureChannel(const unsigned int VFO, const unsigned int configure void RADIO_ConfigureSquelchAndOutputPower(VFO_Info_t *pInfo) { - uint8_t Txp[3]; - FREQUENCY_Band_t Band; + // ******************************* // squelch - - Band = FREQUENCY_GetBand(pInfo->pRX->Frequency); + + FREQUENCY_Band_t Band = FREQUENCY_GetBand(pInfo->pRX->Frequency); uint16_t Base = (Band < BAND4_174MHz) ? 0x1E60 : 0x1E00; if (gEeprom.SQUELCH_LEVEL == 0) @@ -498,12 +497,12 @@ void RADIO_ConfigureSquelchAndOutputPower(VFO_Info_t *pInfo) // ******************************* // output power - + Band = FREQUENCY_GetBand(pInfo->pTX->Frequency); + uint8_t Txp[3]; EEPROM_ReadBuffer(0x1ED0 + (Band * 16) + (pInfo->OUTPUT_POWER * 3), Txp, 3); - #ifdef ENABLE_REDUCE_LOW_MID_TX_POWER // make low and mid even lower if (pInfo->OUTPUT_POWER == OUTPUT_POWER_LOW) { @@ -548,16 +547,15 @@ void RADIO_ApplyOffset(VFO_Info_t *pInfo) if (Frequency < frequencyBandTable[0].lower) Frequency = frequencyBandTable[0].lower; - else - if (Frequency > frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper) - Frequency = frequencyBandTable[ARRAY_SIZE(frequencyBandTable) - 1].upper; + else if (Frequency > frequencyBandTable[BAND_N_ELEM - 1].upper) + Frequency = frequencyBandTable[BAND_N_ELEM - 1].upper; pInfo->freq_config_TX.Frequency = Frequency; } static void RADIO_SelectCurrentVfo(void) { - // if crossband is active and DW not the gCurrentVfo is gTxVfo (gTxVfo/TX_VFO is only ever changed by the user) + // if crossband is active and DW not the gCurrentVfo is gTxVfo (gTxVfo/TX_VFO is only ever changed by the user) // otherwise it is set to gRxVfo which is set to gTxVfo in RADIO_SelectVfos // so in the end gCurrentVfo is equal to gTxVfo unless dual watch changes it on incomming transmition (again, this can only happen when XB off) // note: it is called only in certain situations so could be not up-to-date @@ -1142,7 +1140,7 @@ void RADIO_SendEndOfTransmission(void) gEeprom.DTMF_HASH_CODE_PERSIST_TIME, gEeprom.DTMF_CODE_PERSIST_TIME, gEeprom.DTMF_CODE_INTERVAL_TIME); - + AUDIO_AudioPathOff(); gEnableSpeaker = false; } diff --git a/settings.h b/settings.h index 67a5ecb13..d9e078713 100644 --- a/settings.h +++ b/settings.h @@ -33,7 +33,7 @@ enum POWER_OnDisplayMode_t { }; typedef enum POWER_OnDisplayMode_t POWER_OnDisplayMode_t; -enum { +enum TxLockModes_t { F_LOCK_DEF, //all default frequencies + configurable F_LOCK_FCC, F_LOCK_CE,