Skip to content

Commit

Permalink
FM Radio: Simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JuantAldea authored and egzumer committed Dec 8, 2023
1 parent 82ddbcd commit b6a49db
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
85 changes: 36 additions & 49 deletions app/fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void Key_FUNC(KEY_Code_t Key, uint8_t state);

bool FM_CheckValidChannel(uint8_t Channel)
{
return (Channel < ARRAY_SIZE(gFM_Channels) && (gFM_Channels[Channel] >= 760 && gFM_Channels[Channel] < 1080)) ? true : false;
return (Channel < ARRAY_SIZE(gFM_Channels) && (gFM_Channels[Channel] >= 760 && gFM_Channels[Channel] < 1080));
}

uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction)
Expand Down Expand Up @@ -193,42 +193,37 @@ int FM_CheckFrequencyLock(uint16_t Frequency, uint16_t LowerLimit)
// This is supposed to be a signed value, but above function is unsigned
const uint16_t Deviation = BK1080_REG_07_GET_FREQD(Test2);

if (BK1080_REG_07_GET_SNR(Test2) >= 2)
{
const uint16_t Status = BK1080_ReadRegister(BK1080_REG_10);
if ((Status & BK1080_REG_10_MASK_AFCRL) == BK1080_REG_10_AFCRL_NOT_RAILED && BK1080_REG_10_GET_RSSI(Status) >= 10)
{
//if (Deviation > -281 && Deviation < 280)
if (Deviation < 280 || Deviation > 3815)
{
// not BLE(less than or equal)
if (BK1080_REG_07_GET_SNR(Test2) <= 2){
goto Bail;
}

if (Frequency > LowerLimit && (Frequency - BK1080_BaseFrequency) == 1)
{
if (BK1080_FrequencyDeviation & 0x800)
goto Bail;
const uint16_t Status = BK1080_ReadRegister(BK1080_REG_10);

if (BK1080_FrequencyDeviation < 20)
goto Bail;
}
if ((Status & BK1080_REG_10_MASK_AFCRL) != BK1080_REG_10_AFCRL_NOT_RAILED || BK1080_REG_10_GET_RSSI(Status) < 10) {
goto Bail;
}

// not BLT(less than)
//if (Deviation > -281 && Deviation < 280)
if (Deviation >= 280 && Deviation <= 3815) {
goto Bail;
}

if (Frequency >= LowerLimit && (BK1080_BaseFrequency - Frequency) == 1)
{
if ((BK1080_FrequencyDeviation & 0x800) == 0)
goto Bail;
// not BLE(less than or equal)
if (Frequency > LowerLimit && (Frequency - BK1080_BaseFrequency) == 1) {
if (BK1080_FrequencyDeviation & 0x800 || (BK1080_FrequencyDeviation < 20))
goto Bail;
}

// if (BK1080_FrequencyDeviation > -21)
if (BK1080_FrequencyDeviation > 4075)
goto Bail;
}
// not BLT(less than)

if (Frequency >= LowerLimit && (BK1080_BaseFrequency - Frequency) == 1) {
if ((BK1080_FrequencyDeviation & 0x800) == 0 || (BK1080_FrequencyDeviation > 4075))
goto Bail;

ret = 0;
}
}
}

ret = 0;

Bail:
BK1080_FrequencyDeviation = Deviation;
BK1080_BaseFrequency = Frequency;
Expand Down Expand Up @@ -336,7 +331,7 @@ static void Key_DIGITS(KEY_Code_t Key, uint8_t state)

static void Key_FUNC(KEY_Code_t Key, uint8_t state)
{
if (state == BUTTON_EVENT_SHORT || state == BUTTON_EVENT_HELD)
if (state == BUTTON_EVENT_SHORT || state == BUTTON_EVENT_HELD)
{
bool autoScan = gWasFKeyPressed || (state == BUTTON_EVENT_HELD);

Expand Down Expand Up @@ -446,11 +441,9 @@ static void Key_MENU(uint8_t state)
if (gAskToSave)
{
gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying;
gAskToSave = false;
gRequestSaveFM = true;
gRequestSaveFM = true;
}
else
gAskToSave = true;
gAskToSave = !gAskToSave;
}
else
{
Expand All @@ -462,10 +455,9 @@ static void Key_MENU(uint8_t state)
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);

gRequestSaveFM = true;
gAskToDelete = false;
}
else
gAskToDelete = true;

gAskToDelete = !gAskToDelete;
}
}
else
Expand All @@ -480,27 +472,23 @@ static void Key_MENU(uint8_t state)
if (gAskToSave)
{
gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying;
gAskToSave = false;
gRequestSaveFM = true;
}
else
gAskToSave = true;
gAskToSave = !gAskToSave;
}
}

static void Key_UP_DOWN(uint8_t state, int8_t Step)
{
if (state == BUTTON_EVENT_PRESSED) {
if (state == BUTTON_EVENT_PRESSED) {
if (gInputBoxIndex) {
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
return;
}

gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL;
}
else {
if (gInputBoxIndex || state!=BUTTON_EVENT_HELD)
return;
} else if (gInputBoxIndex || state!=BUTTON_EVENT_HELD) {
return;
}

if (gAskToSave) {
Expand Down Expand Up @@ -530,6 +518,7 @@ static void Key_UP_DOWN(uint8_t state, int8_t Step)
}
else {
uint16_t Frequency = gEeprom.FM_SelectedFrequency + Step;

if (Frequency < gEeprom.FM_LowerLimit)
Frequency = gEeprom.FM_UpperLimit;
else
Expand All @@ -552,8 +541,6 @@ void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
{
uint8_t state = bKeyPressed + 2 * bKeyHeld;



switch (Key)
{
case KEY_0:
Expand All @@ -570,10 +557,10 @@ void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
break;
case KEY_STAR:
Key_FUNC(Key, state);
break;
break;
case KEY_MENU:
Key_MENU(state);
return;
break;
case KEY_UP:
Key_UP_DOWN(state, 1);
break;
Expand Down
2 changes: 0 additions & 2 deletions app/fm.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ extern uint8_t gFM_ChannelPosition;
// Doubts about whether this should be signed or not
extern uint16_t gFM_FrequencyDeviation;
extern bool gFM_FoundFrequency;
extern bool gFM_AutoScan;
extern uint16_t gFM_RestoreCountdown_10ms;

bool FM_CheckValidChannel(uint8_t Channel);
Expand All @@ -59,4 +58,3 @@ void FM_Start(void);
#endif

#endif

0 comments on commit b6a49db

Please sign in to comment.