2023-08-11 03:37:33 +08:00
|
|
|
/* Copyright 2023 Dual Tachyon
|
|
|
|
* https://github.com/DualTachyon
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-08-27 19:32:50 +08:00
|
|
|
#include <string.h>
|
2023-09-03 04:27:49 +08:00
|
|
|
#include "app/action.h"
|
2023-08-29 07:15:44 +08:00
|
|
|
#include "app/fm.h"
|
2023-09-03 00:02:48 +08:00
|
|
|
#include "app/generic.h"
|
2023-08-29 07:37:06 +08:00
|
|
|
#include "audio.h"
|
2023-08-27 01:43:05 +08:00
|
|
|
#include "bsp/dp32g030/gpio.h"
|
|
|
|
#include "driver/bk1080.h"
|
2023-08-27 19:32:50 +08:00
|
|
|
#include "driver/eeprom.h"
|
2023-08-27 01:43:05 +08:00
|
|
|
#include "driver/gpio.h"
|
2023-09-03 00:32:00 +08:00
|
|
|
#include "functions.h"
|
2023-08-27 01:43:05 +08:00
|
|
|
#include "misc.h"
|
2023-08-11 00:51:57 +08:00
|
|
|
#include "settings.h"
|
2023-08-29 07:37:06 +08:00
|
|
|
#include "ui/inputbox.h"
|
|
|
|
#include "ui/ui.h"
|
|
|
|
|
2023-08-11 00:51:57 +08:00
|
|
|
uint16_t gFM_Channels[20];
|
2023-08-28 08:07:07 +08:00
|
|
|
bool gFmRadioMode;
|
2023-09-01 06:27:02 +08:00
|
|
|
uint8_t gFmRadioCountdown;
|
2023-09-04 19:19:19 +08:00
|
|
|
volatile uint16_t gFmPlayCountdown;
|
2023-09-04 22:26:28 +08:00
|
|
|
volatile int8_t gFM_ScanState;
|
2023-09-01 06:27:02 +08:00
|
|
|
bool gFM_AutoScan;
|
|
|
|
uint8_t gFM_ChannelPosition;
|
2023-09-03 01:15:37 +08:00
|
|
|
bool gFM_FoundFrequency;
|
2023-09-03 03:18:53 +08:00
|
|
|
bool gFM_AutoScan;
|
2023-09-03 05:22:13 +08:00
|
|
|
uint8_t gFM_ResumeCountdown;
|
2023-08-11 00:51:57 +08:00
|
|
|
|
|
|
|
bool FM_CheckValidChannel(uint8_t Channel)
|
|
|
|
{
|
2023-08-30 05:55:07 +08:00
|
|
|
if (Channel < 20 && (gFM_Channels[Channel] >= 760 && gFM_Channels[Channel] < 1080)) {
|
2023-08-11 00:51:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < 20; i++) {
|
2023-09-03 03:05:41 +08:00
|
|
|
Channel %= 20;
|
2023-08-11 00:51:57 +08:00
|
|
|
if (FM_CheckValidChannel(Channel)) {
|
|
|
|
return Channel;
|
|
|
|
}
|
|
|
|
Channel += Direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FM_ConfigureChannelState(void)
|
|
|
|
{
|
|
|
|
uint8_t Channel;
|
|
|
|
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_FrequencyPlaying = gEeprom.FM_SelectedFrequency;
|
|
|
|
if (gEeprom.FM_IsMrMode) {
|
|
|
|
Channel = FM_FindNextChannel(gEeprom.FM_SelectedChannel, FM_CHANNEL_UP);
|
2023-08-11 00:51:57 +08:00
|
|
|
if (Channel == 0xFF) {
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_IsMrMode = false;
|
2023-08-11 00:51:57 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_SelectedChannel = Channel;
|
|
|
|
gEeprom.FM_FrequencyPlaying = gFM_Channels[Channel];
|
2023-08-11 00:51:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-27 01:43:05 +08:00
|
|
|
void FM_TurnOff(void)
|
|
|
|
{
|
2023-08-28 08:07:07 +08:00
|
|
|
gFmRadioMode = false;
|
2023-09-04 22:26:28 +08:00
|
|
|
gFM_ScanState = FM_SCAN_OFF;
|
2023-08-27 01:43:05 +08:00
|
|
|
g_2000038E = 0;
|
|
|
|
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
|
2023-09-01 07:49:14 +08:00
|
|
|
gEnableSpeaker = false;
|
2023-08-27 01:43:05 +08:00
|
|
|
BK1080_Init(0, false);
|
2023-08-30 07:19:12 +08:00
|
|
|
gUpdateStatus = true;
|
2023-08-27 01:43:05 +08:00
|
|
|
}
|
|
|
|
|
2023-08-27 19:32:50 +08:00
|
|
|
void FM_EraseChannels(void)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
uint8_t Template[8];
|
|
|
|
|
|
|
|
memset(Template, 0xFF, sizeof(Template));
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
EEPROM_WriteBuffer(0x0E40 + (i * 8), Template);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(gFM_Channels, 0xFF, sizeof(gFM_Channels));
|
|
|
|
}
|
|
|
|
|
2023-08-29 07:37:06 +08:00
|
|
|
void FM_Tune(uint16_t Frequency, int8_t Step, bool bFlag)
|
|
|
|
{
|
|
|
|
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
|
2023-09-01 07:49:14 +08:00
|
|
|
gEnableSpeaker = false;
|
2023-09-04 22:26:28 +08:00
|
|
|
if (gFM_ScanState == FM_SCAN_OFF) {
|
2023-08-29 20:26:13 +08:00
|
|
|
gFmPlayCountdown = 120;
|
2023-08-29 07:37:06 +08:00
|
|
|
} else {
|
2023-08-29 20:26:13 +08:00
|
|
|
gFmPlayCountdown = 10;
|
2023-08-29 07:37:06 +08:00
|
|
|
}
|
2023-08-29 20:26:13 +08:00
|
|
|
gScheduleFM = false;
|
2023-09-03 01:15:37 +08:00
|
|
|
gFM_FoundFrequency = false;
|
2023-08-29 07:37:06 +08:00
|
|
|
gAskToSave = false;
|
|
|
|
gAskToDelete = false;
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_FrequencyPlaying = Frequency;
|
2023-08-29 07:37:06 +08:00
|
|
|
if (!bFlag) {
|
|
|
|
Frequency += Step;
|
|
|
|
if (Frequency < gEeprom.FM_LowerLimit) {
|
|
|
|
Frequency = gEeprom.FM_UpperLimit;
|
|
|
|
} else if (Frequency > gEeprom.FM_UpperLimit) {
|
|
|
|
Frequency = gEeprom.FM_LowerLimit;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_FrequencyPlaying = Frequency;
|
2023-08-29 07:37:06 +08:00
|
|
|
}
|
|
|
|
|
2023-09-04 22:26:28 +08:00
|
|
|
gFM_ScanState = Step;
|
2023-08-31 07:33:14 +08:00
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-29 07:37:06 +08:00
|
|
|
}
|
|
|
|
|
2023-09-03 00:32:00 +08:00
|
|
|
void FM_PlayAndUpdate(void)
|
2023-08-29 07:37:06 +08:00
|
|
|
{
|
2023-09-04 22:26:28 +08:00
|
|
|
gFM_ScanState = FM_SCAN_OFF;
|
2023-08-30 05:55:07 +08:00
|
|
|
if (gFM_AutoScan) {
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_IsMrMode = true;
|
|
|
|
gEeprom.FM_SelectedChannel = 0;
|
2023-08-29 07:37:06 +08:00
|
|
|
}
|
|
|
|
FM_ConfigureChannelState();
|
2023-08-31 07:33:14 +08:00
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-29 07:37:06 +08:00
|
|
|
SETTINGS_SaveFM();
|
2023-08-29 20:26:13 +08:00
|
|
|
gFmPlayCountdown = 0;
|
|
|
|
gScheduleFM = false;
|
2023-08-29 07:37:06 +08:00
|
|
|
gAskToSave = false;
|
|
|
|
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
|
2023-09-01 07:49:14 +08:00
|
|
|
gEnableSpeaker = true;
|
2023-08-29 07:37:06 +08:00
|
|
|
}
|
|
|
|
|
2023-08-30 06:00:24 +08:00
|
|
|
int FM_CheckFrequencyLock(uint16_t Frequency, uint16_t LowerLimit)
|
|
|
|
{
|
2023-09-04 23:50:54 +08:00
|
|
|
uint16_t Test2;
|
|
|
|
uint16_t Deviation;
|
2023-08-30 06:00:24 +08:00
|
|
|
int ret = -1;
|
|
|
|
|
2023-09-04 23:50:54 +08:00
|
|
|
Test2 = BK1080_ReadRegister(BK1080_REG_07);
|
|
|
|
// This is supposed to be a signed value, but above function is unsigned
|
|
|
|
Deviation = BK1080_REG_07_GET_FREQD(Test2);
|
|
|
|
|
|
|
|
if (BK1080_REG_07_GET_SNR(Test2) >= 2) {
|
|
|
|
uint16_t Status;
|
|
|
|
|
|
|
|
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;
|
2023-08-30 06:00:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Bail:
|
2023-09-01 07:49:14 +08:00
|
|
|
BK1080_FrequencyDeviation = Deviation;
|
|
|
|
BK1080_BaseFrequency = Frequency;
|
2023-08-30 06:00:24 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:02:48 +08:00
|
|
|
static void FM_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
|
2023-08-29 19:46:53 +08:00
|
|
|
{
|
2023-09-01 07:49:14 +08:00
|
|
|
#define STATE_FREQ_MODE 0
|
|
|
|
#define STATE_MR_MODE 1
|
|
|
|
#define STATE_SAVE 2
|
|
|
|
|
2023-08-29 19:46:53 +08:00
|
|
|
if (!bKeyHeld && bKeyPressed) {
|
|
|
|
if (!gWasFKeyPressed) {
|
2023-09-01 07:49:14 +08:00
|
|
|
uint8_t State;
|
2023-08-29 19:46:53 +08:00
|
|
|
|
|
|
|
if (gAskToDelete) {
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gAskToSave) {
|
2023-09-01 07:49:14 +08:00
|
|
|
State = STATE_SAVE;
|
2023-08-29 19:46:53 +08:00
|
|
|
} else {
|
2023-09-04 22:26:28 +08:00
|
|
|
if (gFM_ScanState != FM_SCAN_OFF) {
|
2023-08-29 19:46:53 +08:00
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
if (gEeprom.FM_IsMrMode) {
|
2023-09-01 07:49:14 +08:00
|
|
|
State = STATE_MR_MODE;
|
2023-08-29 19:46:53 +08:00
|
|
|
} else {
|
2023-09-01 07:49:14 +08:00
|
|
|
State = STATE_FREQ_MODE;
|
2023-08-29 19:46:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
INPUTBOX_Append(Key);
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
2023-09-01 07:49:14 +08:00
|
|
|
if (State == STATE_FREQ_MODE) {
|
2023-08-29 19:46:53 +08:00
|
|
|
if (gInputBoxIndex == 1) {
|
2023-09-01 07:49:14 +08:00
|
|
|
if (gInputBox[0] > 1) {
|
2023-08-29 19:46:53 +08:00
|
|
|
gInputBox[1] = gInputBox[0];
|
|
|
|
gInputBox[0] = 0;
|
|
|
|
gInputBoxIndex = 2;
|
|
|
|
}
|
2023-09-01 07:49:14 +08:00
|
|
|
} else if (gInputBoxIndex > 3) {
|
2023-08-29 19:46:53 +08:00
|
|
|
uint32_t Frequency;
|
|
|
|
|
|
|
|
gInputBoxIndex = 0;
|
|
|
|
NUMBER_Get(gInputBox, &Frequency);
|
|
|
|
Frequency = Frequency / 10000;
|
|
|
|
if (Frequency < gEeprom.FM_LowerLimit || gEeprom.FM_UpperLimit < Frequency) {
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_SelectedFrequency = (uint16_t)Frequency;
|
2023-08-29 19:46:53 +08:00
|
|
|
gAnotherVoiceID = (VOICE_ID_t)Key;
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_FrequencyPlaying = gEeprom.FM_SelectedFrequency;
|
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-29 19:46:53 +08:00
|
|
|
gRequestSaveFM = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (gInputBoxIndex == 2) {
|
|
|
|
uint8_t Channel;
|
|
|
|
|
|
|
|
gInputBoxIndex = 0;
|
|
|
|
Channel = ((gInputBox[0] * 10) + gInputBox[1]) - 1;
|
2023-09-01 07:49:14 +08:00
|
|
|
if (State == STATE_MR_MODE) {
|
2023-08-29 19:46:53 +08:00
|
|
|
if (FM_CheckValidChannel(Channel)) {
|
|
|
|
gAnotherVoiceID = (VOICE_ID_t)Key;
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_SelectedChannel = Channel;
|
|
|
|
gEeprom.FM_FrequencyPlaying = gFM_Channels[Channel];
|
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-29 19:46:53 +08:00
|
|
|
gRequestSaveFM = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (Channel < 20) {
|
|
|
|
gAnotherVoiceID = (VOICE_ID_t)Key;
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
gInputBoxIndex = 0;
|
2023-08-31 07:05:25 +08:00
|
|
|
gFM_ChannelPosition = Channel;
|
2023-08-29 19:46:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gAnotherVoiceID = (VOICE_ID_t)Key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL;
|
|
|
|
gWasFKeyPressed = false;
|
2023-08-30 07:19:12 +08:00
|
|
|
gUpdateStatus = true;
|
2023-08-29 19:46:53 +08:00
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
switch (Key) {
|
|
|
|
case KEY_0:
|
2023-09-03 04:27:49 +08:00
|
|
|
ACTION_FM();
|
2023-08-29 19:46:53 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_1:
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_IsMrMode = !gEeprom.FM_IsMrMode;
|
2023-08-29 19:46:53 +08:00
|
|
|
if (!FM_ConfigureChannelState()) {
|
2023-08-31 07:33:14 +08:00
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-29 19:46:53 +08:00
|
|
|
gRequestSaveFM = true;
|
2023-09-04 23:50:54 +08:00
|
|
|
} else {
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
2023-08-29 19:46:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_2:
|
2023-09-03 04:27:49 +08:00
|
|
|
ACTION_Scan(true);
|
2023-08-29 19:46:53 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_3:
|
2023-09-03 04:27:49 +08:00
|
|
|
ACTION_Scan(false);
|
2023-08-29 19:46:53 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:02:48 +08:00
|
|
|
static void FM_Key_EXIT(bool bKeyPressed, bool bKeyHeld)
|
2023-08-29 07:37:06 +08:00
|
|
|
{
|
|
|
|
if (bKeyHeld) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!bKeyPressed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL;
|
2023-09-04 22:26:28 +08:00
|
|
|
if (gFM_ScanState == FM_SCAN_OFF) {
|
2023-08-29 07:37:06 +08:00
|
|
|
if (gInputBoxIndex == 0) {
|
|
|
|
if (!gAskToSave && !gAskToDelete) {
|
2023-09-03 04:27:49 +08:00
|
|
|
ACTION_FM();
|
2023-08-29 07:37:06 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gAskToSave = false;
|
|
|
|
gAskToDelete = false;
|
|
|
|
} else {
|
|
|
|
gInputBoxIndex--;
|
|
|
|
gInputBox[gInputBoxIndex] = 10;
|
|
|
|
if (gInputBoxIndex) {
|
|
|
|
if (gInputBoxIndex != 1) {
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gInputBox[0] != 0) {
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gInputBoxIndex = 0;
|
|
|
|
}
|
|
|
|
gAnotherVoiceID = VOICE_ID_CANCEL;
|
|
|
|
} else {
|
2023-09-03 00:32:00 +08:00
|
|
|
FM_PlayAndUpdate();
|
2023-08-29 07:37:06 +08:00
|
|
|
gAnotherVoiceID = VOICE_ID_SCANNING_STOP;
|
|
|
|
}
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:02:48 +08:00
|
|
|
static void FM_Key_MENU(bool bKeyPressed, bool bKeyHeld)
|
2023-08-31 07:07:23 +08:00
|
|
|
{
|
|
|
|
if (bKeyHeld) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!bKeyPressed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL;
|
|
|
|
|
2023-09-04 22:26:28 +08:00
|
|
|
if (gFM_ScanState == FM_SCAN_OFF) {
|
2023-08-31 07:33:14 +08:00
|
|
|
if (!gEeprom.FM_IsMrMode) {
|
2023-08-31 07:07:23 +08:00
|
|
|
if (gAskToSave) {
|
2023-08-31 07:33:14 +08:00
|
|
|
gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying;
|
2023-08-31 07:07:23 +08:00
|
|
|
gAskToSave = false;
|
|
|
|
gRequestSaveFM = true;
|
|
|
|
} else {
|
|
|
|
gAskToSave = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gAskToDelete) {
|
2023-08-31 07:33:14 +08:00
|
|
|
gFM_Channels[gEeprom.FM_SelectedChannel] = 0xFFFF;
|
2023-08-31 07:07:23 +08:00
|
|
|
FM_ConfigureChannelState();
|
2023-08-31 07:33:14 +08:00
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-31 07:07:23 +08:00
|
|
|
gRequestSaveFM = true;
|
|
|
|
gAskToDelete = false;
|
|
|
|
} else {
|
|
|
|
gAskToDelete = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-09-03 01:15:37 +08:00
|
|
|
if (gFM_AutoScan || !gFM_FoundFrequency) {
|
2023-08-31 07:07:23 +08:00
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
gInputBoxIndex = 0;
|
|
|
|
return;
|
|
|
|
} else if (gAskToSave) {
|
2023-08-31 07:33:14 +08:00
|
|
|
gFM_Channels[gFM_ChannelPosition] = gEeprom.FM_FrequencyPlaying;
|
2023-08-31 07:07:23 +08:00
|
|
|
gAskToSave = false;
|
|
|
|
gRequestSaveFM = true;
|
|
|
|
} else {
|
|
|
|
gAskToSave = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:02:48 +08:00
|
|
|
static void FM_Key_UP_DOWN(bool bKeyPressed, bool bKeyHeld, int8_t Step)
|
2023-08-29 07:37:06 +08:00
|
|
|
{
|
|
|
|
if (bKeyHeld || !bKeyPressed) {
|
|
|
|
if (gInputBoxIndex) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!bKeyPressed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gInputBoxIndex) {
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL;
|
|
|
|
}
|
|
|
|
if (gAskToSave) {
|
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
2023-08-31 07:05:25 +08:00
|
|
|
gFM_ChannelPosition = NUMBER_AddWithWraparound(gFM_ChannelPosition, Step, 0, 19);
|
2023-08-29 07:37:06 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-09-04 22:26:28 +08:00
|
|
|
if (gFM_ScanState != FM_SCAN_OFF) {
|
2023-08-30 05:55:07 +08:00
|
|
|
if (gFM_AutoScan) {
|
2023-08-29 07:37:06 +08:00
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
FM_Tune(gEeprom.FM_FrequencyPlaying, Step, false);
|
2023-08-29 07:37:06 +08:00
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
if (gEeprom.FM_IsMrMode) {
|
2023-08-29 07:37:06 +08:00
|
|
|
uint8_t Channel;
|
|
|
|
|
2023-08-31 07:33:14 +08:00
|
|
|
Channel = FM_FindNextChannel(gEeprom.FM_SelectedChannel + Step, Step);
|
|
|
|
if (Channel == 0xFF || gEeprom.FM_SelectedChannel == Channel) {
|
2023-08-29 07:37:06 +08:00
|
|
|
goto Bail;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_SelectedChannel = Channel;
|
|
|
|
gEeprom.FM_FrequencyPlaying = gFM_Channels[Channel];
|
2023-08-29 07:37:06 +08:00
|
|
|
} else {
|
|
|
|
uint16_t Frequency;
|
|
|
|
|
2023-08-31 07:33:14 +08:00
|
|
|
Frequency = gEeprom.FM_SelectedFrequency + Step;
|
2023-08-29 07:37:06 +08:00
|
|
|
if (Frequency < gEeprom.FM_LowerLimit) {
|
|
|
|
Frequency = gEeprom.FM_UpperLimit;
|
|
|
|
} else if (Frequency > gEeprom.FM_UpperLimit) {
|
|
|
|
Frequency = gEeprom.FM_LowerLimit;
|
|
|
|
}
|
2023-08-31 07:33:14 +08:00
|
|
|
gEeprom.FM_FrequencyPlaying = Frequency;
|
|
|
|
gEeprom.FM_SelectedFrequency = gEeprom.FM_FrequencyPlaying;
|
2023-08-29 07:37:06 +08:00
|
|
|
}
|
|
|
|
gRequestSaveFM = true;
|
|
|
|
|
|
|
|
Bail:
|
2023-08-31 07:33:14 +08:00
|
|
|
BK1080_SetFrequency(gEeprom.FM_FrequencyPlaying);
|
2023-08-29 07:37:06 +08:00
|
|
|
gRequestDisplayScreen = DISPLAY_FM;
|
|
|
|
}
|
|
|
|
|
2023-09-03 00:20:22 +08:00
|
|
|
void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
|
2023-09-03 00:02:48 +08:00
|
|
|
{
|
|
|
|
switch (Key) {
|
|
|
|
case KEY_0: case KEY_1: case KEY_2: case KEY_3:
|
|
|
|
case KEY_4: case KEY_5: case KEY_6: case KEY_7:
|
|
|
|
case KEY_8: case KEY_9:
|
|
|
|
FM_Key_DIGITS(Key, bKeyPressed, bKeyHeld);
|
|
|
|
break;
|
|
|
|
case KEY_MENU:
|
|
|
|
FM_Key_MENU(bKeyPressed, bKeyHeld);
|
|
|
|
return;
|
|
|
|
case KEY_UP:
|
|
|
|
FM_Key_UP_DOWN(bKeyPressed, bKeyHeld, 1);
|
|
|
|
break;
|
|
|
|
case KEY_DOWN:
|
|
|
|
FM_Key_UP_DOWN(bKeyPressed, bKeyHeld, -1);
|
|
|
|
break;;
|
|
|
|
case KEY_EXIT:
|
|
|
|
FM_Key_EXIT(bKeyPressed, bKeyHeld);
|
|
|
|
break;
|
|
|
|
case KEY_F:
|
|
|
|
GENERIC_Key_F(bKeyPressed, bKeyHeld);
|
|
|
|
break;
|
|
|
|
case KEY_PTT:
|
|
|
|
GENERIC_Key_PTT(bKeyPressed);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!bKeyHeld && bKeyPressed) {
|
|
|
|
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-09-03 00:32:00 +08:00
|
|
|
|
|
|
|
void FM_Play(void)
|
|
|
|
{
|
|
|
|
if (!FM_CheckFrequencyLock(gEeprom.FM_FrequencyPlaying, gEeprom.FM_LowerLimit)) {
|
|
|
|
if (!gFM_AutoScan) {
|
|
|
|
gFmPlayCountdown = 0;
|
2023-09-03 01:15:37 +08:00
|
|
|
gFM_FoundFrequency = true;
|
2023-09-03 00:32:00 +08:00
|
|
|
if (!gEeprom.FM_IsMrMode) {
|
|
|
|
gEeprom.FM_SelectedFrequency = gEeprom.FM_FrequencyPlaying;
|
|
|
|
}
|
|
|
|
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
|
|
|
|
gEnableSpeaker = true;
|
2023-09-04 06:41:44 +08:00
|
|
|
GUI_SelectNextDisplay(DISPLAY_FM);
|
|
|
|
return;
|
2023-09-03 00:32:00 +08:00
|
|
|
}
|
2023-09-04 06:41:44 +08:00
|
|
|
if (gFM_ChannelPosition < 20) {
|
|
|
|
gFM_Channels[gFM_ChannelPosition++] = gEeprom.FM_FrequencyPlaying;
|
|
|
|
}
|
|
|
|
if (gFM_ChannelPosition >= 20) {
|
2023-09-03 00:32:00 +08:00
|
|
|
FM_PlayAndUpdate();
|
2023-09-04 06:41:44 +08:00
|
|
|
GUI_SelectNextDisplay(DISPLAY_FM);
|
|
|
|
return;
|
2023-09-03 00:32:00 +08:00
|
|
|
}
|
2023-09-04 06:41:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gFM_AutoScan && gEeprom.FM_FrequencyPlaying >= gEeprom.FM_UpperLimit) {
|
|
|
|
FM_PlayAndUpdate();
|
2023-09-03 00:32:00 +08:00
|
|
|
} else {
|
2023-09-04 22:26:28 +08:00
|
|
|
FM_Tune(gEeprom.FM_FrequencyPlaying, gFM_ScanState, false);
|
2023-09-03 00:32:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GUI_SelectNextDisplay(DISPLAY_FM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FM_Start(void)
|
|
|
|
{
|
|
|
|
gFmRadioMode = true;
|
2023-09-04 22:26:28 +08:00
|
|
|
gFM_ScanState = FM_SCAN_OFF;
|
2023-09-03 00:32:00 +08:00
|
|
|
g_2000038E = 0;
|
|
|
|
BK1080_Init(gEeprom.FM_FrequencyPlaying, true);
|
|
|
|
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
|
|
|
|
gEnableSpeaker = true;
|
|
|
|
gUpdateStatus = true;
|
|
|
|
}
|
|
|
|
|