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

more renaming to ease the pain

This commit is contained in:
OneOfEleven 2023-10-30 14:12:27 +00:00
parent 9a7e03ced5
commit 1517cfc655
13 changed files with 241 additions and 221 deletions

View File

@ -138,7 +138,7 @@ void ACTION_Monitor(void)
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (g_fm_radio_mode) if (g_fm_radio_mode)
{ {
FM_Start(); FM_turn_on();
g_request_display_screen = DISPLAY_FM; g_request_display_screen = DISPLAY_FM;
} }
else else
@ -157,12 +157,10 @@ void ACTION_Scan(bool bRestart)
{ {
GUI_SelectNextDisplay(DISPLAY_FM); GUI_SelectNextDisplay(DISPLAY_FM);
// g_monitor_enabled = false; if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
{ // already scanning - stop
if (g_fm_scan_state != FM_SCAN_OFF) FM_stop_scan();
{ // already scanning
FM_PlayAndUpdate();
#ifdef ENABLE_VOICE #ifdef ENABLE_VOICE
g_another_voice_id = VOICE_ID_SCANNING_STOP; g_another_voice_id = VOICE_ID_SCANNING_STOP;
@ -174,21 +172,20 @@ void ACTION_Scan(bool bRestart)
if (bRestart) if (bRestart)
{ // scan with auto store { // scan with auto store
FM_EraseChannels(); FM_erase_channels();
g_fm_auto_scan = true; g_fm_auto_scan = true;
g_fm_channel_position = 0;
Frequency = BK1080_freq_lower; Frequency = BK1080_freq_lower;
} }
else else
{ // scan without auto store { // scan without auto store
g_fm_auto_scan = false; g_fm_auto_scan = false;
g_fm_channel_position = 0;
Frequency = g_eeprom.fm_frequency_playing; Frequency = g_eeprom.fm_frequency_playing;
} }
g_fm_channel_position = 0;
BK1080_GetFrequencyDeviation(Frequency); BK1080_GetFrequencyDeviation(Frequency);
FM_Tune(Frequency, 1, bRestart); FM_tune(Frequency, FM_SCAN_STATE_DIR_UP, bRestart);
#ifdef ENABLE_VOICE #ifdef ENABLE_VOICE
g_another_voice_id = VOICE_ID_SCANNING_BEGIN; g_another_voice_id = VOICE_ID_SCANNING_BEGIN;
@ -348,7 +345,7 @@ void ACTION_Scan(bool bRestart)
if (g_fm_radio_mode) if (g_fm_radio_mode)
{ // return normal service { // return normal service
FM_TurnOff(); FM_turn_off();
g_input_box_index = 0; g_input_box_index = 0;
#ifdef ENABLE_VOX #ifdef ENABLE_VOX
@ -367,7 +364,7 @@ void ACTION_Scan(bool bRestart)
RADIO_select_vfos(); RADIO_select_vfos();
RADIO_setup_registers(true); RADIO_setup_registers(true);
FM_Start(); FM_turn_on();
g_input_box_index = 0; g_input_box_index = 0;

View File

@ -1909,7 +1909,7 @@ void APP_time_slice_500ms(void)
if (g_fm_radio_tick_500ms > 0) if (g_fm_radio_tick_500ms > 0)
g_fm_radio_tick_500ms--; g_fm_radio_tick_500ms--;
if (g_fm_radio_mode && g_current_display_screen == DISPLAY_FM && g_fm_scan_state != FM_SCAN_OFF) if (g_fm_radio_mode && g_current_display_screen == DISPLAY_FM && g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
g_update_display = true; // can't do this if not FM scanning, it causes audio clicks g_update_display = true; // can't do this if not FM scanning, it causes audio clicks
#endif #endif
@ -1962,7 +1962,7 @@ void APP_time_slice_500ms(void)
} }
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (g_fm_scan_state == FM_SCAN_OFF || g_ask_to_save) if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF || g_ask_to_save)
#endif #endif
{ {
#ifdef ENABLE_AIRCOPY #ifdef ENABLE_AIRCOPY
@ -2122,7 +2122,7 @@ void APP_time_slice_500ms(void)
!g_monitor_enabled && !g_monitor_enabled &&
g_fm_radio_mode) g_fm_radio_mode)
{ // switch back to FM radio mode { // switch back to FM radio mode
FM_Start(); FM_turn_on();
GUI_SelectNextDisplay(DISPLAY_FM); GUI_SelectNextDisplay(DISPLAY_FM);
} }
} }
@ -2361,12 +2361,12 @@ void APP_time_slice_10ms(void)
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (g_schedule_fm && if (g_schedule_fm &&
g_fm_scan_state != FM_SCAN_OFF && g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF &&
!g_monitor_enabled && !g_monitor_enabled &&
g_current_function != FUNCTION_RECEIVE && g_current_function != FUNCTION_RECEIVE &&
g_current_function != FUNCTION_TRANSMIT) g_current_function != FUNCTION_TRANSMIT)
{ // switch to FM radio mode { // switch to FM radio mode
FM_Play(); FM_scan();
g_schedule_fm = false; g_schedule_fm = false;
} }
#endif #endif
@ -2378,7 +2378,7 @@ void APP_time_slice_10ms(void)
{ {
if (--g_fm_restore_tick_10ms == 0) if (--g_fm_restore_tick_10ms == 0)
{ // switch back to FM radio mode { // switch back to FM radio mode
FM_Start(); FM_turn_on();
GUI_SelectNextDisplay(DISPLAY_FM); GUI_SelectNextDisplay(DISPLAY_FM);
} }
} }

View File

@ -251,7 +251,7 @@ void DTMF_HandleRequest(void)
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (g_fm_radio_mode) if (g_fm_radio_mode)
{ {
FM_TurnOff(); FM_turn_off();
GUI_SelectNextDisplay(DISPLAY_MAIN); GUI_SelectNextDisplay(DISPLAY_MAIN);
} }
#endif #endif

263
app/fm.c
View File

@ -42,7 +42,7 @@ uint16_t g_fm_channels[20];
bool g_fm_radio_mode; bool g_fm_radio_mode;
uint8_t g_fm_radio_tick_500ms; uint8_t g_fm_radio_tick_500ms;
volatile uint16_t g_fm_play_tick_10ms; volatile uint16_t g_fm_play_tick_10ms;
volatile int8_t g_fm_scan_state; fm_scan_state_dir_t g_fm_scan_state_dir;
bool g_fm_auto_scan; bool g_fm_auto_scan;
uint8_t g_fm_channel_position; uint8_t g_fm_channel_position;
bool g_fm_found_frequency; bool g_fm_found_frequency;
@ -50,39 +50,39 @@ bool g_fm_auto_scan;
uint8_t g_fm_resume_tick_500ms; uint8_t g_fm_resume_tick_500ms;
uint16_t g_fm_restore_tick_10ms; uint16_t g_fm_restore_tick_10ms;
bool FM_CheckValidChannel(uint8_t Channel) bool FM_check_valid_channel(const unsigned int Channel)
{ {
return (Channel < ARRAY_SIZE(g_fm_channels) && (g_fm_channels[Channel] >= BK1080_freq_lower && g_fm_channels[Channel] < BK1080_freq_upper)) ? true : false; return (Channel < ARRAY_SIZE(g_fm_channels) && (g_fm_channels[Channel] >= BK1080_freq_lower && g_fm_channels[Channel] < BK1080_freq_upper)) ? true : false;
} }
uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction) unsigned int FM_find_next_channel(unsigned int Channel, const fm_scan_state_dir_t scan_state_dir)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < ARRAY_SIZE(g_fm_channels); i++) for (i = 0; i < ARRAY_SIZE(g_fm_channels); i++)
{ {
if (Channel == 0xFF) if (Channel > ARRAY_SIZE(g_fm_channels))
Channel = ARRAY_SIZE(g_fm_channels) - 1; Channel = ARRAY_SIZE(g_fm_channels) - 1;
else else
if (Channel >= ARRAY_SIZE(g_fm_channels)) if (Channel >= ARRAY_SIZE(g_fm_channels))
Channel = 0; Channel = 0;
if (FM_CheckValidChannel(Channel)) if (FM_check_valid_channel(Channel))
return Channel; return Channel;
Channel += Direction; Channel += scan_state_dir;
} }
return 0xFF; return 0xFF;
} }
int FM_ConfigureChannelState(void) int FM_configure_channel_state(void)
{ {
g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency; g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency;
if (g_eeprom.fm_channel_mode) if (g_eeprom.fm_channel_mode)
{ {
const uint8_t Channel = FM_FindNextChannel(g_eeprom.fm_selected_channel, FM_CHANNEL_UP); const uint8_t Channel = FM_find_next_channel(g_eeprom.fm_selected_channel, FM_CHANNEL_UP);
if (Channel == 0xFF) if (Channel == 0xFF)
{ {
g_eeprom.fm_channel_mode = false; g_eeprom.fm_channel_mode = false;
@ -96,25 +96,7 @@ int FM_ConfigureChannelState(void)
return 0; return 0;
} }
void FM_TurnOff(void) void FM_erase_channels(void)
{
g_fm_radio_mode = false;
g_fm_scan_state = FM_SCAN_OFF;
g_fm_restore_tick_10ms = 0;
if (!g_monitor_enabled)
{
g_speaker_enabled = false;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
}
BK1080_Init(0, false);
g_update_display = true;
g_update_status = true;
}
void FM_EraseChannels(void)
{ {
unsigned int i; unsigned int i;
uint8_t Template[8]; uint8_t Template[8];
@ -126,48 +108,52 @@ void FM_EraseChannels(void)
memset(g_fm_channels, 0xFF, sizeof(g_fm_channels)); memset(g_fm_channels, 0xFF, sizeof(g_fm_channels));
} }
void FM_Tune(uint16_t Frequency, int8_t Step, bool flag) void FM_tune(uint16_t frequency, const fm_scan_state_dir_t scan_state_dir, const bool flag)
{ {
g_speaker_enabled = false; g_speaker_enabled = false;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER); GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
g_fm_play_tick_10ms = (g_fm_scan_state == FM_SCAN_OFF) ? fm_play_noscan_10ms : fm_play_scan_10ms; g_fm_play_tick_10ms = (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF) ? fm_play_noscan_10ms : fm_play_scan_10ms;
g_schedule_fm = false; g_schedule_fm = false;
g_fm_found_frequency = false; g_fm_found_frequency = false;
g_ask_to_save = false; g_ask_to_save = false;
g_ask_to_delete = false; g_ask_to_delete = false;
g_eeprom.fm_frequency_playing = Frequency; g_eeprom.fm_frequency_playing = frequency;
if (!flag) if (!flag)
{ { // wrap-a-around
Frequency += Step;
if (Frequency < BK1080_freq_lower)
Frequency = BK1080_freq_upper - 1u;
else
if (Frequency > (BK1080_freq_upper - 1u))
Frequency = BK1080_freq_lower;
g_eeprom.fm_frequency_playing = Frequency; frequency += scan_state_dir;
if (frequency < BK1080_freq_lower)
frequency = BK1080_freq_upper - 1u;
else
if (frequency >= BK1080_freq_upper)
frequency = BK1080_freq_lower;
g_eeprom.fm_frequency_playing = frequency;
} }
g_fm_scan_state = Step; g_fm_scan_state_dir = scan_state_dir;
BK1080_SetFrequency(g_eeprom.fm_frequency_playing); BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
} }
void FM_PlayAndUpdate(void) void FM_stop_scan(void)
{ {
g_fm_scan_state = FM_SCAN_OFF; // stop scanning
g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
if (g_fm_auto_scan) if (g_fm_auto_scan)
{ { // switch to channel mode
g_eeprom.fm_channel_mode = true; g_eeprom.fm_channel_mode = true;
g_eeprom.fm_selected_channel = 0; g_eeprom.fm_selected_channel = 0;
} }
FM_ConfigureChannelState(); FM_configure_channel_state();
BK1080_SetFrequency(g_eeprom.fm_frequency_playing); BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
SETTINGS_save_fm(); SETTINGS_save_fm();
g_fm_play_tick_10ms = 0; g_fm_play_tick_10ms = 0;
@ -180,7 +166,7 @@ void FM_PlayAndUpdate(void)
g_update_display = true; g_update_display = true;
} }
int FM_CheckFrequencyLock(uint16_t Frequency, uint16_t LowerLimit) int FM_check_frequency_lock(uint16_t Frequency, uint16_t LowerLimit)
{ {
int ret = -1; int ret = -1;
@ -230,14 +216,87 @@ Bail:
return ret; return ret;
} }
static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held) void FM_scan(void)
{
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;
if (!g_eeprom.fm_channel_mode)
g_eeprom.fm_selected_frequency = g_eeprom.fm_frequency_playing;
g_speaker_enabled = true;
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)
{
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);
g_speaker_enabled = 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_monitor_enabled)
{
g_speaker_enabled = false;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
}
// disable the FM chip
BK1080_Init(0, false);
g_update_display = true;
g_update_status = true;
}
// ***************************************
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
// beeps cause bad audio clicks anf audio breaks :(
// so don't use them // so don't use them
g_key_input_count_down = key_input_timeout_500ms; g_key_input_count_down = key_input_timeout_500ms;
if (key_held && !key_pressed) if (key_held && !key_pressed)
@ -248,6 +307,8 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
return; return;
} }
// long press key or short key release
if (!g_fkey_pressed && !key_held) if (!g_fkey_pressed && !key_held)
{ // short key release { // short key release
uint8_t State; uint8_t State;
@ -261,7 +322,7 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
} }
else else
{ {
if (g_fm_scan_state != FM_SCAN_OFF) if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
return; return;
State = g_eeprom.fm_channel_mode ? STATE_USER_MODE : STATE_FREQ_MODE; State = g_eeprom.fm_channel_mode ? STATE_USER_MODE : STATE_FREQ_MODE;
@ -296,7 +357,7 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
if (Frequency < BK1080_freq_lower) if (Frequency < BK1080_freq_lower)
Frequency = BK1080_freq_lower; Frequency = BK1080_freq_lower;
if (Frequency > (BK1080_freq_upper - 1u)) if (Frequency >= BK1080_freq_upper)
Frequency = BK1080_freq_upper - 1u; Frequency = BK1080_freq_upper - 1u;
g_eeprom.fm_selected_frequency = (uint16_t)Frequency; g_eeprom.fm_selected_frequency = (uint16_t)Frequency;
@ -307,8 +368,8 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency; g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency;
BK1080_SetFrequency(g_eeprom.fm_frequency_playing); BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true;
g_request_save_fm = true;
return; return;
} }
} }
@ -323,24 +384,28 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
if (State == STATE_USER_MODE) if (State == STATE_USER_MODE)
{ {
if (FM_CheckValidChannel(Channel)) if (FM_check_valid_channel(Channel))
{ {
#ifdef ENABLE_VOICE #ifdef ENABLE_VOICE
g_another_voice_id = (voice_id_t)Key; g_another_voice_id = (voice_id_t)Key;
#endif #endif
g_eeprom.fm_selected_channel = Channel; g_eeprom.fm_selected_channel = Channel;
g_eeprom.fm_frequency_playing = g_fm_channels[Channel]; g_eeprom.fm_frequency_playing = g_fm_channels[Channel];
BK1080_SetFrequency(g_eeprom.fm_frequency_playing); BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true; g_request_save_fm = true;
return; return;
} }
} }
else else
if (Channel < 20) if (Channel < ARRAY_SIZE(g_fm_channels))
{ {
#ifdef ENABLE_VOICE #ifdef ENABLE_VOICE
g_another_voice_id = (voice_id_t)Key; g_another_voice_id = (voice_id_t)Key;
#endif #endif
g_request_display_screen = DISPLAY_FM; g_request_display_screen = DISPLAY_FM;
g_input_box_index = 0; g_input_box_index = 0;
g_fm_channel_position = Channel; g_fm_channel_position = Channel;
@ -372,7 +437,7 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
case KEY_3: case KEY_3:
g_eeprom.fm_channel_mode = !g_eeprom.fm_channel_mode; g_eeprom.fm_channel_mode = !g_eeprom.fm_channel_mode;
if (!FM_ConfigureChannelState()) if (!FM_configure_channel_state())
{ {
BK1080_SetFrequency(g_eeprom.fm_frequency_playing); BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true; g_request_save_fm = true;
@ -384,7 +449,7 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
} }
} }
static void FM_Key_STAR(bool key_pressed, bool key_held) static void FM_Key_STAR(const bool key_pressed, const bool key_held)
{ {
g_key_input_count_down = key_input_timeout_500ms; g_key_input_count_down = key_input_timeout_500ms;
@ -402,7 +467,7 @@ static void FM_Key_STAR(bool key_pressed, bool key_held)
g_update_status = true; g_update_status = true;
} }
static void FM_Key_EXIT(bool key_pressed, bool key_held) static void FM_Key_EXIT(const bool key_pressed, const bool key_held)
{ {
(void)key_held; (void)key_held;
@ -412,7 +477,7 @@ static void FM_Key_EXIT(bool key_pressed, bool key_held)
if (key_pressed) if (key_pressed)
return; return;
if (g_fm_scan_state == FM_SCAN_OFF) if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF)
{ {
if (g_input_box_index == 0) if (g_input_box_index == 0)
{ {
@ -453,7 +518,7 @@ static void FM_Key_EXIT(bool key_pressed, bool key_held)
} }
else else
{ {
FM_PlayAndUpdate(); FM_stop_scan();
#ifdef ENABLE_VOICE #ifdef ENABLE_VOICE
g_another_voice_id = VOICE_ID_SCANNING_STOP; g_another_voice_id = VOICE_ID_SCANNING_STOP;
@ -463,7 +528,7 @@ static void FM_Key_EXIT(bool key_pressed, bool key_held)
g_request_display_screen = DISPLAY_FM; g_request_display_screen = DISPLAY_FM;
} }
static void FM_Key_MENU(bool key_pressed, bool key_held) static void FM_Key_MENU(const bool key_pressed, const bool key_held)
{ {
(void)key_held; (void)key_held;
@ -487,7 +552,7 @@ static void FM_Key_MENU(bool key_pressed, bool key_held)
//UART_SendText("fm menu 1\r\n"); //UART_SendText("fm menu 1\r\n");
#endif #endif
if (g_fm_scan_state == FM_SCAN_OFF) if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF)
{ // not scanning { // not scanning
if (!g_eeprom.fm_channel_mode) if (!g_eeprom.fm_channel_mode)
@ -512,7 +577,7 @@ static void FM_Key_MENU(bool key_pressed, bool key_held)
{ {
g_fm_channels[g_eeprom.fm_selected_channel] = 0xFFFF; g_fm_channels[g_eeprom.fm_selected_channel] = 0xFFFF;
FM_ConfigureChannelState(); FM_configure_channel_state();
BK1080_SetFrequency(g_eeprom.fm_frequency_playing); BK1080_SetFrequency(g_eeprom.fm_frequency_playing);
g_request_save_fm = true; g_request_save_fm = true;
@ -557,7 +622,7 @@ static void FM_Key_MENU(bool key_pressed, bool key_held)
g_ask_to_save = true; g_ask_to_save = true;
} }
static void FM_Key_UP_DOWN(bool key_pressed, bool key_held, int8_t Step) static void FM_Key_UP_DOWN(const bool key_pressed, const bool key_held, const fm_scan_state_dir_t scan_state_dir)
{ {
g_key_input_count_down = key_input_timeout_500ms; g_key_input_count_down = key_input_timeout_500ms;
@ -576,23 +641,25 @@ static void FM_Key_UP_DOWN(bool key_pressed, bool key_held, int8_t Step)
if (g_ask_to_save) if (g_ask_to_save)
{ {
g_request_display_screen = DISPLAY_FM; g_request_display_screen = DISPLAY_FM;
g_fm_channel_position = NUMBER_AddWithWraparound(g_fm_channel_position, Step, 0, 19); g_fm_channel_position = NUMBER_AddWithWraparound(g_fm_channel_position, scan_state_dir, 0, 19);
return; return;
} }
if (g_fm_scan_state != FM_SCAN_OFF) if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
{ { // scanning
if (g_fm_auto_scan) if (g_fm_auto_scan)
return; return;
FM_Tune(g_eeprom.fm_frequency_playing, Step, false); FM_tune(g_eeprom.fm_frequency_playing, scan_state_dir, false);
g_request_display_screen = DISPLAY_FM; g_request_display_screen = DISPLAY_FM;
return; return;
} }
if (g_eeprom.fm_channel_mode) if (g_eeprom.fm_channel_mode)
{ // we're in channel mode { // we're in channel mode
const uint8_t Channel = FM_FindNextChannel(g_eeprom.fm_selected_channel + Step, Step); const uint8_t Channel = FM_find_next_channel(g_eeprom.fm_selected_channel + scan_state_dir, scan_state_dir);
if (Channel == 0xFF || g_eeprom.fm_selected_channel == Channel) if (Channel == 0xFF || g_eeprom.fm_selected_channel == Channel)
goto Bail; goto Bail;
@ -601,11 +668,11 @@ static void FM_Key_UP_DOWN(bool key_pressed, bool key_held, int8_t Step)
} }
else else
{ // no, frequency mode { // no, frequency mode
uint16_t Frequency = g_eeprom.fm_selected_frequency + Step; uint16_t Frequency = g_eeprom.fm_selected_frequency + scan_state_dir;
if (Frequency < BK1080_freq_lower) if (Frequency < BK1080_freq_lower)
Frequency = BK1080_freq_upper - 1u; Frequency = BK1080_freq_upper - 1u;
else else
if (Frequency > (BK1080_freq_upper - 1u)) if (Frequency >= BK1080_freq_upper)
Frequency = BK1080_freq_lower; Frequency = BK1080_freq_lower;
g_eeprom.fm_frequency_playing = Frequency; g_eeprom.fm_frequency_playing = Frequency;
@ -640,10 +707,10 @@ void FM_process_key(key_code_t Key, bool key_pressed, bool key_held)
FM_Key_MENU(key_pressed, key_held); FM_Key_MENU(key_pressed, key_held);
return; return;
case KEY_UP: case KEY_UP:
FM_Key_UP_DOWN(key_pressed, key_held, 1); FM_Key_UP_DOWN(key_pressed, key_held, FM_SCAN_STATE_DIR_UP);
break; break;
case KEY_DOWN: case KEY_DOWN:
FM_Key_UP_DOWN(key_pressed, key_held, -1); FM_Key_UP_DOWN(key_pressed, key_held, FM_SCAN_STATE_DIR_DOWN);
break;; break;;
case KEY_STAR: case KEY_STAR:
FM_Key_STAR(key_pressed, key_held); FM_Key_STAR(key_pressed, key_held);
@ -661,57 +728,3 @@ void FM_process_key(key_code_t Key, bool key_pressed, bool key_held)
break; break;
} }
} }
void FM_Play(void)
{
if (!FM_CheckFrequencyLock(g_eeprom.fm_frequency_playing, BK1080_freq_lower))
{
if (!g_fm_auto_scan)
{
g_fm_play_tick_10ms = 0;
g_fm_found_frequency = true;
if (!g_eeprom.fm_channel_mode)
g_eeprom.fm_selected_frequency = g_eeprom.fm_frequency_playing;
g_speaker_enabled = true;
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_PlayAndUpdate();
GUI_SelectNextDisplay(DISPLAY_FM);
return;
}
}
if (g_fm_auto_scan && g_eeprom.fm_frequency_playing > (BK1080_freq_upper - 1))
FM_PlayAndUpdate();
else
FM_Tune(g_eeprom.fm_frequency_playing, g_fm_scan_state, false);
GUI_SelectNextDisplay(DISPLAY_FM);
}
void FM_Start(void)
{
g_fm_radio_mode = true;
g_fm_scan_state = FM_SCAN_OFF;
g_fm_restore_tick_10ms = 0;
// enable the FM radio chip and audio
BK1080_Init(g_eeprom.fm_frequency_playing, true);
g_speaker_enabled = true;
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
g_update_display = true;
g_update_status = true;
}

View File

@ -22,15 +22,18 @@
#define FM_CHANNEL_UP 0x01 #define FM_CHANNEL_UP 0x01
#define FM_CHANNEL_DOWN 0xFF #define FM_CHANNEL_DOWN 0xFF
enum { enum fm_scan_state_dir_e {
FM_SCAN_OFF = 0, FM_SCAN_STATE_DIR_DOWN = -1,
FM_SCAN_STATE_DIR_OFF = 0,
FM_SCAN_STATE_DIR_UP,
}; };
typedef enum fm_scan_state_dir_e fm_scan_state_dir_t;
extern uint16_t g_fm_channels[20]; extern uint16_t g_fm_channels[20];
extern bool g_fm_radio_mode; extern bool g_fm_radio_mode;
extern uint8_t g_fm_radio_tick_500ms; extern uint8_t g_fm_radio_tick_500ms;
extern volatile uint16_t g_fm_play_tick_10ms; extern volatile uint16_t g_fm_play_tick_10ms;
extern volatile int8_t g_fm_scan_state; extern fm_scan_state_dir_t g_fm_scan_state_dir;
extern bool g_fm_auto_scan; extern bool g_fm_auto_scan;
extern uint8_t g_fm_channel_position; extern uint8_t g_fm_channel_position;
// Doubts about whether this should be signed or not // Doubts about whether this should be signed or not
@ -40,20 +43,17 @@ extern bool g_fm_auto_scan;
extern uint8_t g_fm_resume_tick_500ms; extern uint8_t g_fm_resume_tick_500ms;
extern uint16_t g_fm_restore_tick_10ms; extern uint16_t g_fm_restore_tick_10ms;
bool FM_CheckValidChannel(uint8_t Channel); bool FM_check_valid_channel(const unsigned int Channel);
uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction); unsigned int FM_find_next_channel(unsigned int Channel, const fm_scan_state_dir_t scan_state_dir);
int FM_ConfigureChannelState(void); int FM_configure_channel_state(void);
void FM_TurnOff(void); void FM_erase_channels(void);
void FM_EraseChannels(void); void FM_tune(uint16_t frequency, const fm_scan_state_dir_t scan_state_dir, const bool flag);
void FM_stop_scan(void);
void FM_Tune(uint16_t Frequency, int8_t Step, bool bFlag); int FM_check_frequency_lock(uint16_t Frequency, uint16_t LowerLimit);
void FM_PlayAndUpdate(void); void FM_scan(void);
int FM_CheckFrequencyLock(uint16_t Frequency, uint16_t LowerLimit); void FM_turn_on(void);
void FM_turn_off(void);
void FM_process_key(key_code_t Key, bool bKeyPressed, bool bKeyHeld); void FM_process_key(key_code_t Key, bool bKeyPressed, bool bKeyHeld);
void FM_Play(void);
void FM_Start(void);
#endif #endif

View File

@ -174,9 +174,9 @@ void GENERIC_Key_PTT(bool key_pressed)
} }
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (g_fm_scan_state != FM_SCAN_OFF) if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
{ // FM radio is scanning .. stop { // FM radio is scanning .. stop
FM_PlayAndUpdate(); FM_stop_scan();
#ifdef ENABLE_VOICE #ifdef ENABLE_VOICE
g_another_voice_id = VOICE_ID_SCANNING_STOP; g_another_voice_id = VOICE_ID_SCANNING_STOP;
#endif #endif

View File

@ -584,7 +584,7 @@ void BOARD_eeprom_load(void)
// 0E40..0E67 // 0E40..0E67
EEPROM_ReadBuffer(0x0E40, g_fm_channels, sizeof(g_fm_channels)); EEPROM_ReadBuffer(0x0E40, g_fm_channels, sizeof(g_fm_channels));
FM_ConfigureChannelState(); FM_configure_channel_state();
#endif #endif
// 0E90..0E97 // 0E90..0E97

View File

@ -33,10 +33,10 @@
#if defined(ENABLE_FMRADIO_64_108) #if defined(ENABLE_FMRADIO_64_108)
const freq_band_table_t FM_RADIO_FREQ_BAND_TABLE[] = const freq_band_table_t FM_RADIO_FREQ_BAND_TABLE[] =
{ {
{875, 1080}, {875, 1080}, // 87.5 ~ 108 MHz
{760, 1080}, {760, 1080}, // 76 ~ 108 MHz
{760, 900}, {760, 900}, // 76 ~ 90 MHz
{640, 760} {640, 760} // 64 ~ 76 MHz
}; };
#elif defined(ENABLE_FMRADIO_875_108) #elif defined(ENABLE_FMRADIO_875_108)
const freq_band_table_t FM_RADIO_FREQ_BAND_TABLE[] = const freq_band_table_t FM_RADIO_FREQ_BAND_TABLE[] =
@ -108,26 +108,32 @@ void BK1080_Init(const uint16_t frequency, const bool initialise)
{ {
unsigned int i; unsigned int i;
// #if (ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE) > 1) // compiler doesn't like this :(
#if defined(ENABLE_FMRADIO_64_108) #if defined(ENABLE_FMRADIO_64_108)
// determine the lower and upper frequency limits
// determine the lower and upper frequency limits when multiple bands are used
BK1080_freq_lower = 0xffff; BK1080_freq_lower = 0xffff;
BK1080_freq_upper = 0; BK1080_freq_upper = 0;
for (i = 0; i < ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE); i++) for (i = 0; i < ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE); i++)
{ {
const unsigned int lower = FM_RADIO_FREQ_BAND_TABLE[i].lower; const uint16_t lower = FM_RADIO_FREQ_BAND_TABLE[i].lower;
const unsigned int upper = FM_RADIO_FREQ_BAND_TABLE[i].upper; const uint16_t upper = FM_RADIO_FREQ_BAND_TABLE[i].upper;
if (BK1080_freq_lower > lower) if (BK1080_freq_lower > lower)
BK1080_freq_lower = lower; BK1080_freq_lower = lower;
if (BK1080_freq_lower > upper) // if (BK1080_freq_lower > upper)
BK1080_freq_lower = upper; // BK1080_freq_lower = upper;
if (BK1080_freq_upper < lower) // if (BK1080_freq_upper < lower)
BK1080_freq_upper = lower; // BK1080_freq_upper = lower;
if (BK1080_freq_upper < upper) if (BK1080_freq_upper < upper)
BK1080_freq_upper = upper; BK1080_freq_upper = upper;
} }
#else #else
// only 1 band is used
BK1080_freq_lower = FM_RADIO_FREQ_BAND_TABLE[0].lower; BK1080_freq_lower = FM_RADIO_FREQ_BAND_TABLE[0].lower;
BK1080_freq_upper = FM_RADIO_FREQ_BAND_TABLE[0].upper; BK1080_freq_upper = FM_RADIO_FREQ_BAND_TABLE[0].upper;
#endif #endif
@ -144,8 +150,8 @@ void BK1080_Init(const uint16_t frequency, const bool initialise)
SYSTEM_DelayMs(250); SYSTEM_DelayMs(250);
BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA83C); BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA83C); // 1010 1000 0011 1100
BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA8BC); BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA8BC); // 1010 1000 1011 1100
SYSTEM_DelayMs(60); SYSTEM_DelayMs(60);
@ -199,18 +205,22 @@ void BK1080_SetFrequency(uint16_t Frequency)
// #if (ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE) > 1) // compiler doesn't like this :( // #if (ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE) > 1) // compiler doesn't like this :(
#if defined(ENABLE_FMRADIO_64_108) #if defined(ENABLE_FMRADIO_64_108)
// determine which band to use // determine which band to use
for (band = 0; band < ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE); band++) for (band = 0; band < ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE); band++)
if (Frequency >= FM_RADIO_FREQ_BAND_TABLE[band].lower && Frequency < FM_RADIO_FREQ_BAND_TABLE[band].upper) if (Frequency >= FM_RADIO_FREQ_BAND_TABLE[band].lower && Frequency < FM_RADIO_FREQ_BAND_TABLE[band].upper)
break; break;
if (band >= ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE)) if (band >= ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE))
{ { // out of band
Frequency = BK1080_freq_lower; Frequency = BK1080_freq_lower;
// return; // return;
} }
BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, (SEEK_THRESHOLD << 8) | (band << 6) | (CHAN_SPACING << 4) | (VOLUME << 0)); BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, (SEEK_THRESHOLD << 8) | (band << 6) | (CHAN_SPACING << 4) | (VOLUME << 0));
#endif #endif
channel = Frequency - FM_RADIO_FREQ_BAND_TABLE[band].lower; // 100kHz channel spacing channel = Frequency - FM_RADIO_FREQ_BAND_TABLE[band].lower; // 100kHz channel spacing

Binary file not shown.

Binary file not shown.

View File

@ -110,7 +110,7 @@ void SystickHandler(void)
#endif #endif
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (g_fm_scan_state != FM_SCAN_OFF && if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF &&
!g_monitor_enabled && !g_monitor_enabled &&
g_current_function != FUNCTION_TRANSMIT && g_current_function != FUNCTION_TRANSMIT &&
g_current_function != FUNCTION_RECEIVE) g_current_function != FUNCTION_RECEIVE)

View File

@ -68,7 +68,7 @@ void UI_DisplayFM(void)
{ {
memset(str, 0, sizeof(str)); memset(str, 0, sizeof(str));
if (g_fm_scan_state == FM_SCAN_OFF) if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF)
{ {
if (!g_eeprom.fm_channel_mode) if (!g_eeprom.fm_channel_mode)
{ {
@ -106,7 +106,7 @@ void UI_DisplayFM(void)
const unsigned int chan = g_fm_channel_position; const unsigned int chan = g_fm_channel_position;
const uint32_t freq = g_fm_channels[chan]; const uint32_t freq = g_fm_channels[chan];
UI_GenerateChannelString(str, chan, ' '); UI_GenerateChannelString(str, chan, ' ');
if (FM_CheckValidChannel(chan)) if (FM_check_valid_channel(chan))
sprintf(str + strlen(str), " (%u.%u)", freq / 10, freq % 10); sprintf(str + strlen(str), " (%u.%u)", freq / 10, freq % 10);
} }
else else
@ -144,7 +144,7 @@ void UI_DisplayFM(void)
// ************************************* // *************************************
// can't do this during FM radio - audio clicks else // can't do this during FM radio - audio clicks else
if (g_fm_scan_state != FM_SCAN_OFF) if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
{ {
const uint16_t val_07 = BK1080_ReadRegister(0x07); const uint16_t val_07 = BK1080_ReadRegister(0x07);
const uint16_t val_0A = BK1080_ReadRegister(0x0A); const uint16_t val_0A = BK1080_ReadRegister(0x0A);

View File

@ -90,7 +90,7 @@ void GUI_SelectNextDisplay(gui_display_type_t Display)
g_css_scan_mode = CSS_SCAN_MODE_OFF; g_css_scan_mode = CSS_SCAN_MODE_OFF;
g_scan_state_dir = SCAN_STATE_DIR_OFF; g_scan_state_dir = SCAN_STATE_DIR_OFF;
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
g_fm_scan_state = FM_SCAN_OFF; g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF;
g_update_display = true; g_update_display = true;
#endif #endif
g_ask_for_confirmation = 0; g_ask_for_confirmation = 0;