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

728 lines
16 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.
*/
#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"
2023-10-30 18:02:27 +00:00
#include "driver/bk4819.h"
2023-09-09 08:03:56 +01:00
#include "driver/eeprom.h"
#include "driver/gpio.h"
2023-10-27 10:52:32 +01:00
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
#include "driver/uart.h"
#endif
#include "frequencies.h"
2023-09-09 08:03:56 +01:00
#include "functions.h"
#include "misc.h"
#include "settings.h"
#include "ui/inputbox.h"
#include "ui/ui.h"
2023-10-11 17:55:34 +01:00
#define STATE_FREQ_MODE 0
#define STATE_USER_MODE 1
#define STATE_SAVE 2
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
uint16_t g_fm_channels[20];
bool g_fm_radio_mode;
fm_scan_state_dir_t g_fm_scan_state_dir;
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_tick_500ms;
uint16_t g_fm_restore_tick_10ms;
2023-10-30 16:28:41 +00:00
uint8_t g_fm_radio_tick_500ms;
volatile uint16_t g_fm_play_tick_10ms;
volatile bool g_fm_schedule;
2023-10-30 14:12:27 +00:00
bool FM_check_valid_channel(const unsigned int Channel)
2023-09-09 08:03:56 +01:00
{
2023-10-30 11:23:56 +00:00
return (Channel < ARRAY_SIZE(g_fm_channels) && (g_fm_channels[Channel] >= BK1080_freq_lower && g_fm_channels[Channel] < BK1080_freq_upper)) ? true : false;
2023-09-09 08:03:56 +01:00
}
2023-10-30 14:12:27 +00:00
unsigned int FM_find_next_channel(unsigned int Channel, const fm_scan_state_dir_t scan_state_dir)
2023-09-09 08:03:56 +01:00
{
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
{
2023-10-30 14:12:27 +00:00
if (Channel > ARRAY_SIZE(g_fm_channels))
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-10-11 17:55:34 +01:00
2023-10-30 14:12:27 +00:00
if (FM_check_valid_channel(Channel))
2023-09-09 08:03:56 +01:00
return Channel;
2023-10-11 17:55:34 +01:00
2023-10-30 14:12:27 +00:00
Channel += scan_state_dir;
2023-09-09 08:03:56 +01:00
}
return 0xFF;
}
2023-10-30 14:12:27 +00:00
int FM_configure_channel_state(void)
2023-09-09 08:03:56 +01:00
{
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-27 14:34:52 +01:00
if (g_eeprom.fm_channel_mode)
2023-09-11 00:02:57 +01:00
{
2023-10-30 14:12:27 +00:00
const uint8_t Channel = FM_find_next_channel(g_eeprom.fm_selected_channel, FM_CHANNEL_UP);
2023-09-11 00:02:57 +01:00
if (Channel == 0xFF)
{
2023-10-27 14:34:52 +01:00
g_eeprom.fm_channel_mode = false;
2023-09-09 08:03:56 +01:00
return -1;
}
2023-10-11 17:55:34 +01:00
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;
}
2023-10-30 14:12:27 +00:00
void FM_erase_channels(void)
2023-09-09 08:03:56 +01:00
{
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++)
EEPROM_WriteBuffer8(0x0E40 + (i * 8), Template);
2023-09-09 08:03:56 +01:00
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-30 14:12:27 +00:00
void FM_tune(uint16_t frequency, const fm_scan_state_dir_t scan_state_dir, const bool flag)
2023-09-09 08:03:56 +01:00
{
2023-10-29 22:33:38 +00:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
g_fm_play_tick_10ms = (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF) ? fm_play_noscan_10ms : fm_play_scan_10ms;
2023-09-11 00:02:57 +01:00
2023-10-30 16:28:41 +00:00
g_fm_schedule = false;
2023-10-08 20:23:37 +01:00
g_fm_found_frequency = false;
g_ask_to_save = false;
g_ask_to_delete = false;
2023-10-30 14:12:27 +00: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-10-30 14:12:27 +00:00
{ // wrap-a-around
frequency += scan_state_dir;
if (frequency < BK1080_freq_lower)
frequency = BK1080_freq_upper - 1u;
2023-09-11 00:02:57 +01:00
else
2023-10-30 14:12:27 +00:00
if (frequency >= BK1080_freq_upper)
frequency = BK1080_freq_lower;
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
g_eeprom.fm_frequency_playing = frequency;
2023-09-09 08:03:56 +01:00
}
2023-10-30 14:12:27 +00:00
g_fm_scan_state_dir = scan_state_dir;
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
}
2023-10-30 14:12:27 +00:00
void FM_stop_scan(void)
2023-09-09 08:03:56 +01:00
{
2023-10-30 14:12:27 +00:00
// stop scanning
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
2023-09-11 00:02:57 +01:00
2023-10-08 20:23:37 +01:00
if (g_fm_auto_scan)
2023-10-30 14:12:27 +00:00
{ // switch to channel mode
g_eeprom.fm_channel_mode = true;
2023-10-08 17:14:13 +01:00
g_eeprom.fm_selected_channel = 0;
2023-09-09 08:03:56 +01:00
}
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
FM_configure_channel_state();
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-10-30 14:12:27 +00:00
2023-10-19 14:21:37 +01:00
SETTINGS_save_fm();
2023-09-11 00:02:57 +01:00
g_fm_play_tick_10ms = 0;
2023-10-30 16:28:41 +00:00
g_fm_schedule = false;
2023-10-29 22:33:38 +00:00
g_ask_to_save = false;
2023-09-11 00:02:57 +01:00
2023-10-18 11:31:30 +01:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
2023-09-11 00:02:57 +01:00
2023-10-29 22:33:38 +00:00
g_update_display = true;
2023-09-09 08:03:56 +01:00
}
2023-10-30 14:12:27 +00:00
int FM_check_frequency_lock(uint16_t Frequency, uint16_t LowerLimit)
2023-09-09 08:03:56 +01:00
{
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)
2023-09-11 00:02:57 +01:00
{
//if (Deviation > -281 && Deviation < 280)
2023-09-11 00:02:57 +01:00
if (Deviation < 280 || Deviation > 3815)
{
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
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-30 14:12:27 +00:00
void FM_scan(void)
2023-09-09 08:03:56 +01:00
{
2023-10-30 14:12:27 +00:00
if (!FM_check_frequency_lock(g_eeprom.fm_frequency_playing, BK1080_freq_lower))
{
if (!g_fm_auto_scan)
{
g_fm_play_tick_10ms = 0;
g_fm_found_frequency = true;
2023-09-09 08:03:56 +01:00
2023-10-30 14:12:27 +00:00
if (!g_eeprom.fm_channel_mode)
g_eeprom.fm_selected_frequency = g_eeprom.fm_frequency_playing;
2023-10-11 19:07:25 +01:00
2023-10-30 14:12:27 +00:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
GUI_SelectNextDisplay(DISPLAY_FM);
return;
}
if (g_fm_channel_position < ARRAY_SIZE(g_fm_channels))
g_fm_channels[g_fm_channel_position++] = g_eeprom.fm_frequency_playing;
if (g_fm_channel_position >= ARRAY_SIZE(g_fm_channels))
{
FM_stop_scan();
GUI_SelectNextDisplay(DISPLAY_FM);
return;
}
}
if (g_fm_auto_scan && g_eeprom.fm_frequency_playing >= (BK1080_freq_upper - 1u))
FM_stop_scan();
else
FM_tune(g_eeprom.fm_frequency_playing, g_fm_scan_state_dir, false);
GUI_SelectNextDisplay(DISPLAY_FM);
}
void FM_turn_on(void)
{
2023-10-30 18:02:27 +00:00
BK4819_SetAF(BK4819_AF_MUTE);
2023-10-30 14:12:27 +00:00
g_fm_radio_mode = true;
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
g_fm_restore_tick_10ms = 0;
// enable the FM radio chip
BK1080_Init(g_eeprom.fm_frequency_playing, true);
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
g_update_display = true;
g_update_status = true;
}
void FM_turn_off(void)
{
g_fm_radio_mode = false;
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
g_fm_restore_tick_10ms = 0;
if (!g_squelch_open && !g_monitor_enabled)
2023-10-30 14:12:27 +00:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
// disable the FM chip
BK1080_Init(0, false);
g_update_display = true;
g_update_status = true;
}
2023-10-11 19:07:25 +01:00
2023-10-30 14:12:27 +00:00
// ***************************************
static void FM_Key_DIGITS(const key_code_t Key, const bool key_pressed, const bool key_held)
{
// beeps cause bad audio clicks anf audio breaks
// so don't use them
2023-10-11 19:07:25 +01:00
g_key_input_count_down = key_input_timeout_500ms;
2023-10-11 19:07:25 +01:00
if (key_held && !key_pressed)
return; // key just released after long press
2023-10-11 19:07:25 +01:00
if (!key_held && key_pressed)
{ // key just pressed
return;
}
2023-10-11 19:07:25 +01:00
2023-10-30 14:12:27 +00:00
// long press key or short key release
if (!g_fkey_pressed && !key_held)
{ // short key release
uint8_t State;
2023-10-11 19:07:25 +01:00
if (g_ask_to_delete)
return;
2023-10-11 19:07:25 +01:00
if (g_ask_to_save)
{
State = STATE_SAVE;
}
else
{
2023-10-30 14:12:27 +00:00
if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
2023-09-09 08:03:56 +01:00
return;
2023-10-11 19:07:25 +01:00
2023-10-27 14:34:52 +01:00
State = g_eeprom.fm_channel_mode ? STATE_USER_MODE : STATE_FREQ_MODE;
}
2023-10-11 19:07:25 +01:00
2023-10-20 18:00:36 +01:00
INPUTBOX_append(Key);
2023-10-11 19:07:25 +01:00
g_request_display_screen = DISPLAY_FM;
2023-10-11 19:07:25 +01:00
if (State == STATE_FREQ_MODE)
2023-10-11 19:07:25 +01:00
{ // frequency mode
if (g_input_box_index == 1)
2023-09-11 00:02:57 +01:00
{
if (g_input_box[0] > 1)
{
g_input_box[1] = g_input_box[0];
g_input_box[0] = 0;
g_input_box_index = 2;
}
2023-09-11 00:02:57 +01:00
}
else
if (g_input_box_index > 3)
2023-09-11 00:02:57 +01:00
{
uint32_t Frequency;
2023-10-11 19:07:25 +01:00
g_input_box_index = 0;
2023-10-11 18:32:10 +01:00
NUMBER_Get(g_input_box, &Frequency);
2023-10-11 18:32:10 +01:00
Frequency /= 10000;
2023-10-11 18:32:10 +01:00
2023-10-30 11:23:56 +00:00
if (Frequency < BK1080_freq_lower)
Frequency = BK1080_freq_lower;
2023-10-30 14:12:27 +00:00
if (Frequency >= BK1080_freq_upper)
2023-10-30 11:23:56 +00:00
Frequency = BK1080_freq_upper - 1u;
2023-10-11 19:07:25 +01:00
g_eeprom.fm_selected_frequency = (uint16_t)Frequency;
2023-10-11 19:07:25 +01:00
#ifdef ENABLE_VOICE
g_another_voice_id = (voice_id_t)Key;
#endif
2023-10-11 19:07:25 +01:00
g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency;
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-10-11 17:55:34 +01:00
2023-10-30 14:12:27 +00:00
g_request_save_fm = true;
return;
2023-09-09 08:03:56 +01:00
}
}
else
if (g_input_box_index == 2)
2023-10-11 19:07:25 +01:00
{ // channel mode
uint8_t Channel;
2023-10-11 19:07:25 +01:00
g_input_box_index = 0;
Channel = ((g_input_box[0] * 10) + g_input_box[1]) - 1;
2023-10-11 19:07:25 +01:00
if (State == STATE_USER_MODE)
2023-09-11 00:02:57 +01:00
{
2023-10-30 14:12:27 +00:00
if (FM_check_valid_channel(Channel))
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-10-30 14:12:27 +00:00
g_eeprom.fm_selected_channel = Channel;
g_eeprom.fm_frequency_playing = g_fm_channels[Channel];
2023-10-30 14:12:27 +00:00
2023-10-08 17:14:13 +01:00
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
2023-10-30 14:12:27 +00:00
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-30 14:12:27 +00:00
if (Channel < ARRAY_SIZE(g_fm_channels))
2023-09-11 00:02:57 +01:00
{
#ifdef ENABLE_VOICE
g_another_voice_id = (voice_id_t)Key;
#endif
2023-10-30 14:12:27 +00:00
g_request_display_screen = DISPLAY_FM;
2023-10-30 14:12:27 +00:00
g_input_box_index = 0;
g_fm_channel_position = Channel;
2023-09-09 08:03:56 +01:00
return;
}
2023-10-11 19:07:25 +01:00
2023-09-09 08:03:56 +01:00
return;
}
2023-10-11 19:07:25 +01:00
#ifdef ENABLE_VOICE
g_another_voice_id = (voice_id_t)Key;
#endif
2023-10-11 19:07:25 +01:00
return;
}
// with f-key, or long press
2023-10-11 19:07:25 +01:00
g_fkey_pressed = false;
g_update_status = true;
g_request_display_screen = DISPLAY_FM;
2023-10-11 19:07:25 +01:00
switch (Key)
{
case KEY_0:
ACTION_FM();
break;
2023-10-11 19:07:25 +01:00
case KEY_3:
2023-10-27 14:34:52 +01:00
g_eeprom.fm_channel_mode = !g_eeprom.fm_channel_mode;
2023-10-11 19:07:25 +01:00
2023-10-30 14:12:27 +00:00
if (!FM_configure_channel_state())
{
BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true;
}
break;
2023-10-11 19:07:25 +01:00
default:
break;
}
}
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
static void FM_Key_STAR(const bool key_pressed, const bool key_held)
{
2023-10-11 20:44:35 +01:00
g_key_input_count_down = key_input_timeout_500ms;
2023-10-11 17:55:34 +01:00
if (!key_held && !key_pressed)
{
ACTION_Scan(false); // short key press just released .. frequency scan without store
}
2023-10-11 17:55:34 +01:00
else
if (key_held && key_pressed)
{
ACTION_Scan(true); // long key press still pressed .. frequency scan and store
}
g_fkey_pressed = false;
g_update_status = true;
2023-09-09 08:03:56 +01:00
}
2023-10-30 14:12:27 +00:00
static void FM_Key_EXIT(const bool key_pressed, const bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-10-29 22:33:38 +00:00
(void)key_held;
2023-10-11 20:44:35 +01:00
g_key_input_count_down = key_input_timeout_500ms;
2023-10-29 22:33:38 +00:00
// if (key_held || key_pressed)
if (key_pressed)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_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
{
2023-10-30 14:12:27 +00:00
FM_stop_scan();
2023-10-11 17:55:34 +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_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-30 14:12:27 +00:00
static void FM_Key_MENU(const bool key_pressed, const bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-10-29 22:33:38 +00:00
(void)key_held;
2023-10-30 14:12:27 +00:00
unsigned int i;
int channel = -1;
2023-10-11 19:07:25 +01:00
2023-10-11 20:44:35 +01:00
g_key_input_count_down = key_input_timeout_500ms;
2023-10-29 22:33:38 +00:00
// if (key_held || key_pressed)
if (key_pressed)
2023-10-11 23:07:58 +01:00
return; // key still pressed
2023-09-09 08:03:56 +01:00
// see if the frequency is already stored in a channel
for (i = 0; i < ARRAY_SIZE(g_fm_channels) && channel < 0; i++)
if (g_fm_channels[i] == g_eeprom.fm_frequency_playing)
channel = i; // found it in the channel list
2023-10-11 19:07:25 +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-11 23:07:58 +01:00
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
//UART_SendText("fm menu 1\r\n");
#endif
2023-10-30 14:12:27 +00:00
if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF)
{ // not scanning
2023-10-27 14:34:52 +01:00
if (!g_eeprom.fm_channel_mode)
{ // frequency mode
2023-10-11 19:07:25 +01:00
2023-10-08 20:23:37 +01:00
if (g_ask_to_save)
2023-09-11 00:02:57 +01:00
{
if (channel < 0)
{
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
if (channel < 0)
2023-10-08 20:23:37 +01:00
g_ask_to_save = true;
2023-09-11 00:02:57 +01:00
}
else
{ // channel mode
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-10-30 14:12:27 +00:00
FM_configure_channel_state();
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-10-29 22:33:38 +00:00
2023-10-11 23:07:58 +01:00
return;
2023-09-11 00:02:57 +01:00
}
2023-10-11 23:07:58 +01:00
// scanning
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
//UART_SendText("fm menu 2\r\n");
#endif
if (g_fm_auto_scan || !g_fm_found_frequency)
{
g_input_box_index = 0;
return;
}
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
//UART_SendText("fm menu 3\r\n");
#endif
if (g_ask_to_save)
{
g_fm_channels[g_fm_channel_position] = g_eeprom.fm_frequency_playing;
g_ask_to_save = false;
g_request_save_fm = true;
return;
2023-09-09 08:03:56 +01:00
}
2023-10-11 23:07:58 +01:00
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
//UART_SendText("fm menu 4\r\n");
#endif
if (channel < 0)
g_ask_to_save = true;
2023-09-09 08:03:56 +01:00
}
2023-10-30 14:12:27 +00:00
static void FM_Key_UP_DOWN(const bool key_pressed, const bool key_held, const fm_scan_state_dir_t scan_state_dir)
2023-09-09 08:03:56 +01:00
{
2023-10-11 20:44:35 +01:00
g_key_input_count_down = key_input_timeout_500ms;
2023-10-08 20:23:37 +01:00
if (key_held || !key_pressed)
2023-09-11 00:02:57 +01:00
{
2023-10-11 17:55:34 +01:00
if (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 (!key_pressed)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
}
else
2023-10-11 17:55:34 +01:00
if (g_input_box_index > 0)
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_request_display_screen = DISPLAY_FM;
2023-10-30 14:12:27 +00:00
g_fm_channel_position = NUMBER_AddWithWraparound(g_fm_channel_position, scan_state_dir, 0, 19);
2023-09-09 08:03:56 +01:00
return;
}
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
{ // scanning
2023-10-08 20:23:37 +01:00
if (g_fm_auto_scan)
2023-09-09 08:03:56 +01:00
return;
2023-09-11 00:02:57 +01:00
2023-10-30 14:12:27 +00:00
FM_tune(g_eeprom.fm_frequency_playing, scan_state_dir, 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-27 14:34:52 +01:00
if (g_eeprom.fm_channel_mode)
2023-10-11 23:42:50 +01:00
{ // we're in channel mode
2023-10-30 14:12:27 +00:00
const uint8_t Channel = FM_find_next_channel(g_eeprom.fm_selected_channel + scan_state_dir, scan_state_dir);
2023-10-08 17:14:13 +01:00
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-11 23:42:50 +01:00
{ // no, frequency mode
2023-10-30 14:12:27 +00:00
uint16_t Frequency = g_eeprom.fm_selected_frequency + scan_state_dir;
2023-10-30 11:23:56 +00:00
if (Frequency < BK1080_freq_lower)
Frequency = BK1080_freq_upper - 1u;
2023-09-11 00:02:57 +01:00
else
2023-10-30 14:12:27 +00:00
if (Frequency >= BK1080_freq_upper)
2023-10-30 11:23:56 +00:00
Frequency = BK1080_freq_lower;
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
}
void FM_process_key(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-30 14:12:27 +00:00
FM_Key_UP_DOWN(key_pressed, key_held, FM_SCAN_STATE_DIR_UP);
2023-09-11 00:02:57 +01:00
break;
case KEY_DOWN:
2023-10-30 14:12:27 +00:00
FM_Key_UP_DOWN(key_pressed, key_held, FM_SCAN_STATE_DIR_DOWN);
2023-09-11 00:02:57 +01:00
break;;
case KEY_STAR:
FM_Key_STAR(key_pressed, key_held);
break;
2023-09-11 00:02:57 +01:00
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:
break;
2023-09-09 08:03:56 +01:00
}
}