0
mirror of https://github.com/OneOfEleven/uv-k5-firmware-custom.git synced 2025-04-28 22:31:25 +03:00

671 lines
14 KiB
C
Raw Normal View History

2023-09-09 08:03:56 +01: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-09-14 09:56:30 +01:00
#ifdef ENABLE_FMRADIO
2023-09-09 08:03:56 +01:00
#include <string.h>
#include "app/action.h"
#include "app/fm.h"
#include "app/generic.h"
#include "audio.h"
#include "bsp/dp32g030/gpio.h"
#include "driver/bk1080.h"
#include "driver/eeprom.h"
#include "driver/gpio.h"
#include "functions.h"
#include "misc.h"
#include "settings.h"
#include "ui/inputbox.h"
#include "ui/ui.h"
2023-09-11 00:02:57 +01:00
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
#endif
2023-10-08 20:23:37 +01:00
uint16_t g_fm_channels[20];
bool g_fm_radio_mode;
uint8_t g_fm_radio_count_down_500ms;
volatile uint16_t g_fm_play_count_down_10ms;
volatile int8_t g_fm_scan_state;
bool g_fm_auto_scan;
uint8_t g_fm_channel_position;
bool g_fm_found_frequency;
bool g_fm_auto_scan;
uint8_t g_fm_resume_count_down_500ms;
uint16_t g_fm_restore_count_down_10ms;
2023-09-09 08:03:56 +01:00
bool FM_CheckValidChannel(uint8_t Channel)
{
2023-10-08 20:23:37 +01:00
return (Channel < ARRAY_SIZE(g_fm_channels) && (g_fm_channels[Channel] >= 760 && g_fm_channels[Channel] < 1080)) ? true : false;
2023-09-09 08:03:56 +01:00
}
uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction)
{
2023-09-11 00:02:57 +01:00
unsigned int i;
2023-09-09 08:03:56 +01:00
2023-10-08 20:23:37 +01:00
for (i = 0; i < ARRAY_SIZE(g_fm_channels); i++)
2023-09-11 00:02:57 +01:00
{
if (Channel == 0xFF)
2023-10-08 20:23:37 +01:00
Channel = ARRAY_SIZE(g_fm_channels) - 1;
else
2023-10-08 20:23:37 +01:00
if (Channel >= ARRAY_SIZE(g_fm_channels))
Channel = 0;
2023-09-11 00:02:57 +01:00
if (FM_CheckValidChannel(Channel))
2023-09-09 08:03:56 +01:00
return Channel;
Channel += Direction;
}
return 0xFF;
}
int FM_ConfigureChannelState(void)
{
2023-10-08 17:14:13 +01:00
g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
if (g_eeprom.fm_is_channel_mode)
2023-09-11 00:02:57 +01:00
{
2023-10-08 17:14:13 +01:00
const uint8_t Channel = FM_FindNextChannel(g_eeprom.fm_selected_channel, FM_CHANNEL_UP);
2023-09-11 00:02:57 +01:00
if (Channel == 0xFF)
{
2023-10-08 17:14:13 +01:00
g_eeprom.fm_is_channel_mode = false;
2023-09-09 08:03:56 +01:00
return -1;
}
2023-10-08 17:14:13 +01:00
g_eeprom.fm_selected_channel = Channel;
2023-10-08 20:23:37 +01:00
g_eeprom.fm_frequency_playing = g_fm_channels[Channel];
2023-09-09 08:03:56 +01:00
}
return 0;
}
void FM_TurnOff(void)
{
2023-10-08 20:23:37 +01:00
g_fm_radio_mode = false;
g_fm_scan_state = FM_SCAN_OFF;
g_fm_restore_count_down_10ms = 0;
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_enable_speaker = false;
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
BK1080_Init(0, false);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_update_status = true;
2023-09-09 08:03:56 +01:00
}
void FM_EraseChannels(void)
{
2023-09-11 00:02:57 +01:00
unsigned int i;
uint8_t Template[8];
2023-09-09 08:03:56 +01:00
memset(Template, 0xFF, sizeof(Template));
2023-09-11 00:02:57 +01:00
for (i = 0; i < 5; i++)
2023-09-09 08:03:56 +01:00
EEPROM_WriteBuffer(0x0E40 + (i * 8), Template);
2023-10-08 20:23:37 +01:00
memset(g_fm_channels, 0xFF, sizeof(g_fm_channels));
2023-09-09 08:03:56 +01:00
}
2023-10-08 20:23:37 +01:00
void FM_Tune(uint16_t Frequency, int8_t Step, bool flag)
2023-09-09 08:03:56 +01:00
{
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_enable_speaker = false;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_fm_play_count_down_10ms = (g_fm_scan_state == FM_SCAN_OFF) ? fm_play_countdown_noscan_10ms : fm_play_countdown_scan_10ms;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_schedule_fm = false;
g_fm_found_frequency = false;
g_ask_to_save = false;
g_ask_to_delete = false;
2023-10-08 17:14:13 +01:00
g_eeprom.fm_frequency_playing = Frequency;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (!flag)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
Frequency += Step;
2023-10-08 17:14:13 +01:00
if (Frequency < g_eeprom.fm_lower_limit)
Frequency = g_eeprom.fm_upper_limit;
2023-09-11 00:02:57 +01:00
else
2023-10-08 17:14:13 +01:00
if (Frequency > g_eeprom.fm_upper_limit)
Frequency = g_eeprom.fm_lower_limit;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
g_eeprom.fm_frequency_playing = Frequency;
2023-09-09 08:03:56 +01:00
}
2023-10-08 20:23:37 +01:00
g_fm_scan_state = Step;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-09-09 08:03:56 +01:00
}
void FM_PlayAndUpdate(void)
{
2023-10-08 20:23:37 +01:00
g_fm_scan_state = FM_SCAN_OFF;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_auto_scan)
2023-09-11 00:02:57 +01:00
{
2023-10-08 17:14:13 +01:00
g_eeprom.fm_is_channel_mode = true;
g_eeprom.fm_selected_channel = 0;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
FM_ConfigureChannelState();
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-09-09 08:03:56 +01:00
SETTINGS_SaveFM();
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_fm_play_count_down_10ms = 0;
g_schedule_fm = false;
g_ask_to_save = false;
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_enable_speaker = true;
2023-09-09 08:03:56 +01:00
}
int FM_CheckFrequencyLock(uint16_t Frequency, uint16_t LowerLimit)
{
int ret = -1;
2023-09-11 00:02:57 +01:00
const uint16_t Test2 = BK1080_ReadRegister(BK1080_REG_07);
2023-09-09 08:03:56 +01:00
2023-09-11 00:02:57 +01:00
// This is supposed to be a signed value, but above function is unsigned
const uint16_t Deviation = BK1080_REG_07_GET_FREQD(Test2);
2023-09-09 08:03:56 +01:00
2023-09-11 00:02:57 +01:00
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)
2023-09-11 00:02:57 +01:00
if (Deviation < 280 || Deviation > 3815)
{
2023-09-09 08:03:56 +01:00
// not BLE(less than or equal)
2023-09-11 00:02:57 +01:00
if (Frequency > LowerLimit && (Frequency - BK1080_BaseFrequency) == 1)
{
if (BK1080_FrequencyDeviation & 0x800)
2023-09-09 08:03:56 +01:00
goto Bail;
2023-09-11 00:02:57 +01:00
if (BK1080_FrequencyDeviation < 20)
2023-09-09 08:03:56 +01:00
goto Bail;
}
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
// not BLT(less than)
2023-09-11 00:02:57 +01:00
if (Frequency >= LowerLimit && (BK1080_BaseFrequency - Frequency) == 1)
{
if ((BK1080_FrequencyDeviation & 0x800) == 0)
2023-09-09 08:03:56 +01:00
goto Bail;
2023-09-11 00:02:57 +01:00
// if (BK1080_FrequencyDeviation > -21)
if (BK1080_FrequencyDeviation > 4075)
2023-09-09 08:03:56 +01:00
goto Bail;
}
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
ret = 0;
}
}
}
Bail:
BK1080_FrequencyDeviation = Deviation;
2023-09-11 00:02:57 +01:00
BK1080_BaseFrequency = Frequency;
2023-09-09 08:03:56 +01:00
return ret;
}
2023-10-08 20:23:37 +01:00
static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-09-11 00:02:57 +01:00
#define STATE_FREQ_MODE 0
2023-10-08 17:14:13 +01:00
#define STATE_USER_MODE 1
2023-09-11 00:02:57 +01:00
#define STATE_SAVE 2
2023-09-09 08:03:56 +01:00
2023-10-08 20:23:37 +01:00
if (!key_held && key_pressed)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (!g_f_key_was_pressed)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
uint8_t State;
2023-10-08 20:23:37 +01:00
if (g_ask_to_delete)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_ask_to_save)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
State = STATE_SAVE;
2023-09-11 00:02:57 +01:00
}
else
{
2023-10-08 20:23:37 +01:00
if (g_fm_scan_state != FM_SCAN_OFF)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
State = g_eeprom.fm_is_channel_mode ? STATE_USER_MODE : STATE_FREQ_MODE;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
INPUTBOX_Append(Key);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-11 00:02:57 +01:00
if (State == STATE_FREQ_MODE)
{
2023-10-08 20:23:37 +01:00
if (g_input_box_index == 1)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_input_box[0] > 1)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_input_box[1] = g_input_box[0];
g_input_box[0] = 0;
g_input_box_index = 2;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
}
else
2023-10-08 20:23:37 +01:00
if (g_input_box_index > 3)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
uint32_t Frequency;
2023-10-08 20:23:37 +01:00
g_input_box_index = 0;
NUMBER_Get(g_input_box, &Frequency);
2023-09-11 00:02:57 +01:00
Frequency /= 10000;
2023-10-08 17:14:13 +01:00
if (Frequency < g_eeprom.fm_lower_limit || g_eeprom.fm_upper_limit < Frequency)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
g_eeprom.fm_selected_frequency = (uint16_t)Frequency;
2023-09-11 00:02:57 +01:00
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
2023-10-08 17:14:13 +01:00
g_another_voice_id = (voice_id_t)Key;
2023-09-09 08:03:56 +01:00
#endif
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency;
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-10-08 20:23:37 +01:00
g_request_save_fm = true;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
}
else
2023-10-08 20:23:37 +01:00
if (g_input_box_index == 2)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
uint8_t Channel;
2023-10-08 20:23:37 +01:00
g_input_box_index = 0;
Channel = ((g_input_box[0] * 10) + g_input_box[1]) - 1;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
if (State == STATE_USER_MODE)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
if (FM_CheckValidChannel(Channel))
{
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
2023-10-08 17:14:13 +01:00
g_another_voice_id = (voice_id_t)Key;
2023-09-09 08:03:56 +01:00
#endif
2023-10-08 17:14:13 +01:00
g_eeprom.fm_selected_channel = Channel;
2023-10-08 20:23:37 +01:00
g_eeprom.fm_frequency_playing = g_fm_channels[Channel];
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-10-08 20:23:37 +01:00
g_request_save_fm = true;
2023-09-09 08:03:56 +01:00
return;
}
}
else
if (Channel < 20)
{
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
2023-10-08 17:14:13 +01:00
g_another_voice_id = (voice_id_t)Key;
2023-09-09 08:03:56 +01:00
#endif
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
g_input_box_index = 0;
g_fm_channel_position = Channel;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
2023-10-08 17:14:13 +01:00
g_another_voice_id = (voice_id_t)Key;
2023-09-09 08:03:56 +01:00
#endif
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_1KHZ_60MS_OPTIONAL;
g_f_key_was_pressed = false;
g_update_status = true;
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
2023-09-11 00:02:57 +01:00
switch (Key)
{
case KEY_0:
ACTION_FM();
break;
2023-09-09 08:03:56 +01:00
2023-09-11 00:02:57 +01:00
case KEY_1:
2023-10-08 17:14:13 +01:00
g_eeprom.fm_is_channel_mode = !g_eeprom.fm_is_channel_mode;
2023-09-09 08:03:56 +01:00
2023-09-11 00:02:57 +01:00
if (!FM_ConfigureChannelState())
{
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-10-08 20:23:37 +01:00
g_request_save_fm = true;
2023-09-11 00:02:57 +01:00
}
else
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-11 00:02:57 +01:00
break;
2023-09-09 08:03:56 +01:00
2023-09-11 00:02:57 +01:00
case KEY_2:
ACTION_Scan(true);
break;
case KEY_3:
ACTION_Scan(false);
break;
default:
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-11 00:02:57 +01:00
break;
2023-09-09 08:03:56 +01:00
}
}
}
2023-10-08 20:23:37 +01:00
static void FM_Key_EXIT(bool key_pressed, bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-10-08 20:23:37 +01:00
if (key_held)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (!key_pressed)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_1KHZ_60MS_OPTIONAL;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_scan_state == FM_SCAN_OFF)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_input_box_index == 0)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (!g_ask_to_save && !g_ask_to_delete)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
ACTION_FM();
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_ask_to_save = false;
g_ask_to_delete = false;
2023-09-11 00:02:57 +01:00
}
else
{
2023-10-08 20:23:37 +01:00
g_input_box[--g_input_box_index] = 10;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_input_box_index)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_input_box_index != 1)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_input_box[0] != 0)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
return;
}
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_input_box_index = 0;
2023-09-09 08:03:56 +01:00
}
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
2023-10-08 17:14:13 +01:00
g_another_voice_id = VOICE_ID_CANCEL;
2023-09-09 08:03:56 +01:00
#endif
}
else
{
FM_PlayAndUpdate();
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
2023-10-08 17:14:13 +01:00
g_another_voice_id = VOICE_ID_SCANNING_STOP;
2023-09-09 08:03:56 +01:00
#endif
}
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
}
2023-10-08 20:23:37 +01:00
static void FM_Key_MENU(bool key_pressed, bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-10-08 20:23:37 +01:00
if (key_held)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (!key_pressed)
2023-09-09 08:03:56 +01:00
return;
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
g_beep_to_play = BEEP_1KHZ_60MS_OPTIONAL;
2023-09-09 08:03:56 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_scan_state == FM_SCAN_OFF)
2023-09-11 00:02:57 +01:00
{
2023-10-08 17:14:13 +01:00
if (!g_eeprom.fm_is_channel_mode)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_ask_to_save)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_fm_channels[g_fm_channel_position] = g_eeprom.fm_frequency_playing;
g_ask_to_save = false;
g_request_save_fm = true;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
else
2023-10-08 20:23:37 +01:00
g_ask_to_save = true;
2023-09-11 00:02:57 +01:00
}
else
{
2023-10-08 20:23:37 +01:00
if (g_ask_to_delete)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_fm_channels[g_eeprom.fm_selected_channel] = 0xFFFF;
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
FM_ConfigureChannelState();
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_request_save_fm = true;
g_ask_to_delete = false;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
else
2023-10-08 20:23:37 +01:00
g_ask_to_delete = true;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
}
else
{
2023-10-08 20:23:37 +01:00
if (g_fm_auto_scan || !g_fm_found_frequency)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
g_input_box_index = 0;
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
}
2023-10-08 20:23:37 +01:00
if (g_ask_to_save)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_fm_channels[g_fm_channel_position] = g_eeprom.fm_frequency_playing;
g_ask_to_save = false;
g_request_save_fm = true;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
else
2023-10-08 20:23:37 +01:00
g_ask_to_save = true;
2023-09-09 08:03:56 +01:00
}
}
2023-10-08 20:23:37 +01:00
static void FM_Key_UP_DOWN(bool key_pressed, bool key_held, int8_t Step)
2023-09-09 08:03:56 +01:00
{
2023-10-08 20:23:37 +01:00
if (key_held || !key_pressed)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_input_box_index)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (!key_pressed)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
}
else
{
2023-10-08 20:23:37 +01:00
if (g_input_box_index)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_1KHZ_60MS_OPTIONAL;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_ask_to_save)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
g_fm_channel_position = NUMBER_AddWithWraparound(g_fm_channel_position, Step, 0, 19);
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_scan_state != FM_SCAN_OFF)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_fm_auto_scan)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
FM_Tune(g_eeprom.fm_frequency_playing, Step, false);
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
return;
}
2023-10-08 17:14:13 +01:00
if (g_eeprom.fm_is_channel_mode)
2023-09-11 00:02:57 +01:00
{
2023-10-08 17:14:13 +01:00
const uint8_t Channel = FM_FindNextChannel(g_eeprom.fm_selected_channel + Step, Step);
if (Channel == 0xFF || g_eeprom.fm_selected_channel == Channel)
2023-09-09 08:03:56 +01:00
goto Bail;
2023-10-08 17:14:13 +01:00
g_eeprom.fm_selected_channel = Channel;
2023-10-08 20:23:37 +01:00
g_eeprom.fm_frequency_playing = g_fm_channels[Channel];
2023-09-11 00:02:57 +01:00
}
else
{
2023-10-08 17:14:13 +01:00
uint16_t Frequency = g_eeprom.fm_selected_frequency + Step;
if (Frequency < g_eeprom.fm_lower_limit)
Frequency = g_eeprom.fm_upper_limit;
2023-09-11 00:02:57 +01:00
else
2023-10-08 17:14:13 +01:00
if (Frequency > g_eeprom.fm_upper_limit)
Frequency = g_eeprom.fm_lower_limit;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
g_eeprom.fm_frequency_playing = Frequency;
g_eeprom.fm_selected_frequency = g_eeprom.fm_frequency_playing;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_request_save_fm = true;
2023-09-09 08:03:56 +01:00
Bail:
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-09 08:03:56 +01:00
}
2023-10-08 20:23:37 +01:00
void FM_ProcessKeys(key_code_t Key, bool key_pressed, bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-09-11 00:02:57 +01: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:
2023-10-08 20:23:37 +01:00
FM_Key_DIGITS(Key, key_pressed, key_held);
2023-09-11 00:02:57 +01:00
break;
case KEY_MENU:
2023-10-08 20:23:37 +01:00
FM_Key_MENU(key_pressed, key_held);
2023-09-11 00:02:57 +01:00
return;
case KEY_UP:
2023-10-08 20:23:37 +01:00
FM_Key_UP_DOWN(key_pressed, key_held, 1);
2023-09-11 00:02:57 +01:00
break;
case KEY_DOWN:
2023-10-08 20:23:37 +01:00
FM_Key_UP_DOWN(key_pressed, key_held, -1);
2023-09-11 00:02:57 +01:00
break;;
case KEY_EXIT:
2023-10-08 20:23:37 +01:00
FM_Key_EXIT(key_pressed, key_held);
2023-09-11 00:02:57 +01:00
break;
case KEY_F:
2023-10-08 20:23:37 +01:00
GENERIC_Key_F(key_pressed, key_held);
2023-09-11 00:02:57 +01:00
break;
case KEY_PTT:
2023-10-08 20:23:37 +01:00
GENERIC_Key_PTT(key_pressed);
2023-09-11 00:02:57 +01:00
break;
default:
2023-10-08 20:23:37 +01:00
if (!key_held && key_pressed)
g_beep_to_play = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
2023-09-11 00:02:57 +01:00
break;
2023-09-09 08:03:56 +01:00
}
}
void FM_Play(void)
{
2023-10-08 17:14:13 +01:00
if (!FM_CheckFrequencyLock(g_eeprom.fm_frequency_playing, g_eeprom.fm_lower_limit))
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
if (!g_fm_auto_scan)
2023-09-11 00:02:57 +01:00
{
2023-10-08 20:23:37 +01:00
g_fm_play_count_down_10ms = 0;
g_fm_found_frequency = true;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
if (!g_eeprom.fm_is_channel_mode)
g_eeprom.fm_selected_frequency = g_eeprom.fm_frequency_playing;
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
2023-10-08 20:23:37 +01:00
g_enable_speaker = true;
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
GUI_SelectNextDisplay(DISPLAY_FM);
return;
}
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_channel_position < 20)
g_fm_channels[g_fm_channel_position++] = g_eeprom.fm_frequency_playing;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_channel_position >= 20)
2023-09-11 00:02:57 +01:00
{
2023-09-09 08:03:56 +01:00
FM_PlayAndUpdate();
GUI_SelectNextDisplay(DISPLAY_FM);
return;
}
}
2023-10-08 20:23:37 +01:00
if (g_fm_auto_scan && g_eeprom.fm_frequency_playing >= g_eeprom.fm_upper_limit)
2023-09-09 08:03:56 +01:00
FM_PlayAndUpdate();
2023-09-11 00:02:57 +01:00
else
2023-10-08 20:23:37 +01:00
FM_Tune(g_eeprom.fm_frequency_playing, g_fm_scan_state, false);
2023-09-09 08:03:56 +01:00
GUI_SelectNextDisplay(DISPLAY_FM);
}
void FM_Start(void)
{
2023-10-08 20:23:37 +01:00
g_fm_radio_mode = true;
g_fm_scan_state = FM_SCAN_OFF;
g_fm_restore_count_down_10ms = 0;
2023-09-11 00:02:57 +01:00
2023-10-08 17:14:13 +01:00
BK1080_Init(g_eeprom.fm_frequency_playing, true);
2023-09-11 00:02:57 +01:00
2023-09-09 08:03:56 +01:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
2023-10-08 20:23:37 +01:00
g_enable_speaker = true;
g_update_status = true;
2023-09-11 00:02:57 +01:00
}
2023-09-14 09:56:30 +01:00
#endif