Skip to content

Commit

Permalink
Refactor stepFruencyTable & drop ARRAY_SIZE for frequencyBandTable
Browse files Browse the repository at this point in the history
  • Loading branch information
JuantAldea authored and egzumer committed Dec 8, 2023
1 parent 335c2ec commit 82ddbcd
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/aircopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 3 additions & 3 deletions app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions app/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#endif
#include "ui/inputbox.h"
#include "ui/menu.h"
#include "ui/menu.h"
#include "ui/ui.h"

#ifndef ARRAY_SIZE
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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

Expand Down
1 change: 0 additions & 1 deletion app/spectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 33 additions & 8 deletions frequencies.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "frequencies.h"
#include "misc.h"
#include "settings.h"
#include <assert.h>

// the BK4819 has 2 bands it covers, 18MHz ~ 630MHz and 760MHz ~ 1300MHz

Expand Down Expand Up @@ -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
};


Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions frequencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,30 @@ 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,
STEP_10kHz,
STEP_12_5kHz,
STEP_25kHz,
STEP_8_33kHz,
// custom steps
STEP_0_01kHz,
STEP_0_05kHz,
STEP_0_1kHz,
Expand All @@ -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];
Expand Down
28 changes: 13 additions & 15 deletions radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 82ddbcd

Please sign in to comment.