FM Radio: Simplify logic

This commit is contained in:
Juan Antonio
2023-12-08 13:08:11 +01:00
committed by Krzysiek Egzmont
parent 82ddbcd375
commit b6a49db31d
2 changed files with 42 additions and 57 deletions

View File

@@ -62,7 +62,7 @@ static void Key_FUNC(KEY_Code_t Key, uint8_t state);
bool FM_CheckValidChannel(uint8_t Channel) 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) uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction)
@@ -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 // This is supposed to be a signed value, but above function is unsigned
const uint16_t Deviation = BK1080_REG_07_GET_FREQD(Test2); const uint16_t Deviation = BK1080_REG_07_GET_FREQD(Test2);
if (BK1080_REG_07_GET_SNR(Test2) >= 2) if (BK1080_REG_07_GET_SNR(Test2) <= 2){
{ goto Bail;
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 (Frequency > LowerLimit && (Frequency - BK1080_BaseFrequency) == 1)
{
if (BK1080_FrequencyDeviation & 0x800)
goto Bail;
if (BK1080_FrequencyDeviation < 20)
goto Bail;
}
// not BLT(less than)
if (Frequency >= LowerLimit && (BK1080_BaseFrequency - Frequency) == 1)
{
if ((BK1080_FrequencyDeviation & 0x800) == 0)
goto Bail;
// if (BK1080_FrequencyDeviation > -21)
if (BK1080_FrequencyDeviation > 4075)
goto Bail;
}
ret = 0;
}
}
} }
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) {
goto Bail;
}
//if (Deviation > -281 && Deviation < 280)
if (Deviation >= 280 && Deviation <= 3815) {
goto Bail;
}
// not BLE(less than or equal)
if (Frequency > LowerLimit && (Frequency - BK1080_BaseFrequency) == 1) {
if (BK1080_FrequencyDeviation & 0x800 || (BK1080_FrequencyDeviation < 20))
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;
Bail: Bail:
BK1080_FrequencyDeviation = Deviation; BK1080_FrequencyDeviation = Deviation;
BK1080_BaseFrequency = Frequency; BK1080_BaseFrequency = Frequency;
@@ -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) 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); bool autoScan = gWasFKeyPressed || (state == BUTTON_EVENT_HELD);
@@ -446,11 +441,9 @@ static void Key_MENU(uint8_t state)
if (gAskToSave) if (gAskToSave)
{ {
gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying; gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying;
gAskToSave = false; gRequestSaveFM = true;
gRequestSaveFM = true;
} }
else gAskToSave = !gAskToSave;
gAskToSave = true;
} }
else else
{ {
@@ -462,10 +455,9 @@ static void Key_MENU(uint8_t state)
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying); BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
gRequestSaveFM = true; gRequestSaveFM = true;
gAskToDelete = false;
} }
else
gAskToDelete = true; gAskToDelete = !gAskToDelete;
} }
} }
else else
@@ -480,27 +472,23 @@ static void Key_MENU(uint8_t state)
if (gAskToSave) if (gAskToSave)
{ {
gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying; gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying;
gAskToSave = false;
gRequestSaveFM = true; gRequestSaveFM = true;
} }
else gAskToSave = !gAskToSave;
gAskToSave = true;
} }
} }
static void Key_UP_DOWN(uint8_t state, int8_t Step) static void Key_UP_DOWN(uint8_t state, int8_t Step)
{ {
if (state == BUTTON_EVENT_PRESSED) { if (state == BUTTON_EVENT_PRESSED) {
if (gInputBoxIndex) { if (gInputBoxIndex) {
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
return; return;
} }
gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL;
} } else if (gInputBoxIndex || state!=BUTTON_EVENT_HELD) {
else { return;
if (gInputBoxIndex || state!=BUTTON_EVENT_HELD)
return;
} }
if (gAskToSave) { if (gAskToSave) {
@@ -530,6 +518,7 @@ static void Key_UP_DOWN(uint8_t state, int8_t Step)
} }
else { else {
uint16_t Frequency = gEeprom.FM_SelectedFrequency + Step; uint16_t Frequency = gEeprom.FM_SelectedFrequency + Step;
if (Frequency < gEeprom.FM_LowerLimit) if (Frequency < gEeprom.FM_LowerLimit)
Frequency = gEeprom.FM_UpperLimit; Frequency = gEeprom.FM_UpperLimit;
else else
@@ -552,8 +541,6 @@ void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
{ {
uint8_t state = bKeyPressed + 2 * bKeyHeld; uint8_t state = bKeyPressed + 2 * bKeyHeld;
switch (Key) switch (Key)
{ {
case KEY_0: case KEY_0:
@@ -570,10 +557,10 @@ void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
break; break;
case KEY_STAR: case KEY_STAR:
Key_FUNC(Key, state); Key_FUNC(Key, state);
break; break;
case KEY_MENU: case KEY_MENU:
Key_MENU(state); Key_MENU(state);
return; break;
case KEY_UP: case KEY_UP:
Key_UP_DOWN(state, 1); Key_UP_DOWN(state, 1);
break; break;

View File

@@ -38,7 +38,6 @@ extern uint8_t gFM_ChannelPosition;
// Doubts about whether this should be signed or not // Doubts about whether this should be signed or not
extern uint16_t gFM_FrequencyDeviation; extern uint16_t gFM_FrequencyDeviation;
extern bool gFM_FoundFrequency; extern bool gFM_FoundFrequency;
extern bool gFM_AutoScan;
extern uint16_t gFM_RestoreCountdown_10ms; extern uint16_t gFM_RestoreCountdown_10ms;
bool FM_CheckValidChannel(uint8_t Channel); bool FM_CheckValidChannel(uint8_t Channel);
@@ -59,4 +58,3 @@ void FM_Start(void);
#endif #endif
#endif #endif