diff --git a/app/action.c b/app/action.c index 08cca36..4c0397e 100644 --- a/app/action.c +++ b/app/action.c @@ -138,7 +138,7 @@ void ACTION_Monitor(void) #ifdef ENABLE_FMRADIO if (g_fm_radio_mode) { - FM_Start(); + FM_turn_on(); g_request_display_screen = DISPLAY_FM; } else @@ -157,12 +157,10 @@ void ACTION_Scan(bool bRestart) { 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) - { // already scanning - - FM_PlayAndUpdate(); + FM_stop_scan(); #ifdef ENABLE_VOICE g_another_voice_id = VOICE_ID_SCANNING_STOP; @@ -174,21 +172,20 @@ void ACTION_Scan(bool bRestart) if (bRestart) { // scan with auto store - FM_EraseChannels(); - g_fm_auto_scan = true; - g_fm_channel_position = 0; - Frequency = BK1080_freq_lower; + FM_erase_channels(); + g_fm_auto_scan = true; + Frequency = BK1080_freq_lower; } else { // scan without auto store - g_fm_auto_scan = false; - g_fm_channel_position = 0; - Frequency = g_eeprom.fm_frequency_playing; + g_fm_auto_scan = false; + Frequency = g_eeprom.fm_frequency_playing; } + g_fm_channel_position = 0; BK1080_GetFrequencyDeviation(Frequency); - FM_Tune(Frequency, 1, bRestart); + FM_tune(Frequency, FM_SCAN_STATE_DIR_UP, bRestart); #ifdef ENABLE_VOICE g_another_voice_id = VOICE_ID_SCANNING_BEGIN; @@ -348,7 +345,7 @@ void ACTION_Scan(bool bRestart) if (g_fm_radio_mode) { // return normal service - FM_TurnOff(); + FM_turn_off(); g_input_box_index = 0; #ifdef ENABLE_VOX @@ -367,7 +364,7 @@ void ACTION_Scan(bool bRestart) RADIO_select_vfos(); RADIO_setup_registers(true); - FM_Start(); + FM_turn_on(); g_input_box_index = 0; diff --git a/app/app.c b/app/app.c index 59c1f9c..2b0441f 100644 --- a/app/app.c +++ b/app/app.c @@ -1909,7 +1909,7 @@ void APP_time_slice_500ms(void) if (g_fm_radio_tick_500ms > 0) 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 #endif @@ -1962,7 +1962,7 @@ void APP_time_slice_500ms(void) } #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 { #ifdef ENABLE_AIRCOPY @@ -2122,7 +2122,7 @@ void APP_time_slice_500ms(void) !g_monitor_enabled && g_fm_radio_mode) { // switch back to FM radio mode - FM_Start(); + FM_turn_on(); GUI_SelectNextDisplay(DISPLAY_FM); } } @@ -2360,13 +2360,13 @@ void APP_time_slice_10ms(void) APP_process_transmit(); #ifdef ENABLE_FMRADIO - if (g_schedule_fm && - g_fm_scan_state != FM_SCAN_OFF && - !g_monitor_enabled && - g_current_function != FUNCTION_RECEIVE && + if (g_schedule_fm && + g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF && + !g_monitor_enabled && + g_current_function != FUNCTION_RECEIVE && g_current_function != FUNCTION_TRANSMIT) { // switch to FM radio mode - FM_Play(); + FM_scan(); g_schedule_fm = false; } #endif @@ -2378,7 +2378,7 @@ void APP_time_slice_10ms(void) { if (--g_fm_restore_tick_10ms == 0) { // switch back to FM radio mode - FM_Start(); + FM_turn_on(); GUI_SelectNextDisplay(DISPLAY_FM); } } diff --git a/app/dtmf.c b/app/dtmf.c index c3b3a16..8c5858f 100644 --- a/app/dtmf.c +++ b/app/dtmf.c @@ -251,7 +251,7 @@ void DTMF_HandleRequest(void) #ifdef ENABLE_FMRADIO if (g_fm_radio_mode) { - FM_TurnOff(); + FM_turn_off(); GUI_SelectNextDisplay(DISPLAY_MAIN); } #endif diff --git a/app/fm.c b/app/fm.c index d841db1..0fca7c2 100644 --- a/app/fm.c +++ b/app/fm.c @@ -38,51 +38,51 @@ #define STATE_USER_MODE 1 #define STATE_SAVE 2 -uint16_t g_fm_channels[20]; -bool g_fm_radio_mode; -uint8_t g_fm_radio_tick_500ms; -volatile uint16_t g_fm_play_tick_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_tick_500ms; -uint16_t g_fm_restore_tick_10ms; +uint16_t g_fm_channels[20]; +bool g_fm_radio_mode; +uint8_t g_fm_radio_tick_500ms; +volatile uint16_t g_fm_play_tick_10ms; +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; -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; } -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; for (i = 0; i < ARRAY_SIZE(g_fm_channels); i++) { - if (Channel == 0xFF) - Channel = ARRAY_SIZE(g_fm_channels) - 1; + if (Channel > ARRAY_SIZE(g_fm_channels)) + Channel = ARRAY_SIZE(g_fm_channels) - 1; else if (Channel >= ARRAY_SIZE(g_fm_channels)) Channel = 0; - if (FM_CheckValidChannel(Channel)) + if (FM_check_valid_channel(Channel)) return Channel; - Channel += Direction; + Channel += scan_state_dir; } return 0xFF; } -int FM_ConfigureChannelState(void) +int FM_configure_channel_state(void) { g_eeprom.fm_frequency_playing = g_eeprom.fm_selected_frequency; 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) { g_eeprom.fm_channel_mode = false; @@ -96,25 +96,7 @@ int FM_ConfigureChannelState(void) return 0; } -void FM_TurnOff(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) +void FM_erase_channels(void) { unsigned int i; uint8_t Template[8]; @@ -126,48 +108,52 @@ void FM_EraseChannels(void) 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; 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_fm_found_frequency = false; g_ask_to_save = false; g_ask_to_delete = false; - g_eeprom.fm_frequency_playing = Frequency; + g_eeprom.fm_frequency_playing = frequency; if (!flag) - { - Frequency += Step; - if (Frequency < BK1080_freq_lower) - Frequency = BK1080_freq_upper - 1u; - else - if (Frequency > (BK1080_freq_upper - 1u)) - Frequency = BK1080_freq_lower; + { // wrap-a-around - 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); } -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) - { - g_eeprom.fm_channel_mode = true; + { // switch to channel mode + g_eeprom.fm_channel_mode = true; g_eeprom.fm_selected_channel = 0; } - FM_ConfigureChannelState(); + FM_configure_channel_state(); + BK1080_SetFrequency(g_eeprom.fm_frequency_playing); + SETTINGS_save_fm(); g_fm_play_tick_10ms = 0; @@ -180,7 +166,7 @@ void FM_PlayAndUpdate(void) 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; @@ -230,14 +216,87 @@ Bail: 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 - g_key_input_count_down = key_input_timeout_500ms; 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; } + // long press key or short key release + if (!g_fkey_pressed && !key_held) { // short key release uint8_t State; @@ -261,7 +322,7 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held) } else { - if (g_fm_scan_state != FM_SCAN_OFF) + if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF) return; 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) Frequency = BK1080_freq_lower; - if (Frequency > (BK1080_freq_upper - 1u)) + if (Frequency >= BK1080_freq_upper) Frequency = BK1080_freq_upper - 1u; 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; BK1080_SetFrequency(g_eeprom.fm_frequency_playing); - g_request_save_fm = true; + g_request_save_fm = true; return; } } @@ -323,27 +384,31 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held) if (State == STATE_USER_MODE) { - if (FM_CheckValidChannel(Channel)) + if (FM_check_valid_channel(Channel)) { #ifdef ENABLE_VOICE g_another_voice_id = (voice_id_t)Key; #endif - g_eeprom.fm_selected_channel = Channel; + + g_eeprom.fm_selected_channel = Channel; g_eeprom.fm_frequency_playing = g_fm_channels[Channel]; + BK1080_SetFrequency(g_eeprom.fm_frequency_playing); + g_request_save_fm = true; return; } } else - if (Channel < 20) + if (Channel < ARRAY_SIZE(g_fm_channels)) { #ifdef ENABLE_VOICE g_another_voice_id = (voice_id_t)Key; #endif + g_request_display_screen = DISPLAY_FM; - g_input_box_index = 0; - g_fm_channel_position = Channel; + g_input_box_index = 0; + g_fm_channel_position = Channel; return; } @@ -372,7 +437,7 @@ static void FM_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held) case KEY_3: 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); 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; @@ -402,7 +467,7 @@ static void FM_Key_STAR(bool key_pressed, bool key_held) 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; @@ -412,7 +477,7 @@ static void FM_Key_EXIT(bool key_pressed, bool key_held) if (key_pressed) 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) { @@ -453,7 +518,7 @@ static void FM_Key_EXIT(bool key_pressed, bool key_held) } else { - FM_PlayAndUpdate(); + FM_stop_scan(); #ifdef ENABLE_VOICE g_another_voice_id = VOICE_ID_SCANNING_STOP; @@ -463,10 +528,10 @@ static void FM_Key_EXIT(bool key_pressed, bool key_held) 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; - + unsigned int i; int channel = -1; @@ -487,7 +552,7 @@ static void FM_Key_MENU(bool key_pressed, bool key_held) //UART_SendText("fm menu 1\r\n"); #endif - if (g_fm_scan_state == FM_SCAN_OFF) + if (g_fm_scan_state_dir == FM_SCAN_STATE_DIR_OFF) { // not scanning 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; - FM_ConfigureChannelState(); + FM_configure_channel_state(); BK1080_SetFrequency(g_eeprom.fm_frequency_playing); 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; } -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; @@ -576,23 +641,25 @@ static void FM_Key_UP_DOWN(bool key_pressed, bool key_held, int8_t Step) if (g_ask_to_save) { 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; } - 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) 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; return; } if (g_eeprom.fm_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) goto Bail; @@ -601,11 +668,11 @@ static void FM_Key_UP_DOWN(bool key_pressed, bool key_held, int8_t Step) } else { // 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) Frequency = BK1080_freq_upper - 1u; else - if (Frequency > (BK1080_freq_upper - 1u)) + if (Frequency >= BK1080_freq_upper) Frequency = BK1080_freq_lower; 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); return; 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; 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;; case KEY_STAR: 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; } } - -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; -} diff --git a/app/fm.h b/app/fm.h index 6f3416c..7e66d01 100644 --- a/app/fm.h +++ b/app/fm.h @@ -22,38 +22,38 @@ #define FM_CHANNEL_UP 0x01 #define FM_CHANNEL_DOWN 0xFF -enum { - FM_SCAN_OFF = 0, +enum fm_scan_state_dir_e { + 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 bool g_fm_radio_mode; -extern uint8_t g_fm_radio_tick_500ms; -extern volatile uint16_t g_fm_play_tick_10ms; -extern volatile int8_t g_fm_scan_state; -extern bool g_fm_auto_scan; -extern uint8_t g_fm_channel_position; -// Doubts about whether this should be signed or not -extern uint16_t g_fm_frequency_deviation; -extern bool g_fm_found_frequency; -extern bool g_fm_auto_scan; -extern uint8_t g_fm_resume_tick_500ms; -extern uint16_t g_fm_restore_tick_10ms; +extern uint16_t g_fm_channels[20]; +extern bool g_fm_radio_mode; +extern uint8_t g_fm_radio_tick_500ms; +extern volatile uint16_t g_fm_play_tick_10ms; +extern fm_scan_state_dir_t g_fm_scan_state_dir; +extern bool g_fm_auto_scan; +extern uint8_t g_fm_channel_position; +// Doubts about whether this should be signed or not +extern uint16_t g_fm_frequency_deviation; +extern bool g_fm_found_frequency; +extern bool g_fm_auto_scan; +extern uint8_t g_fm_resume_tick_500ms; +extern uint16_t g_fm_restore_tick_10ms; -bool FM_CheckValidChannel(uint8_t Channel); -uint8_t FM_FindNextChannel(uint8_t Channel, uint8_t Direction); -int FM_ConfigureChannelState(void); -void FM_TurnOff(void); -void FM_EraseChannels(void); - -void FM_Tune(uint16_t Frequency, int8_t Step, bool bFlag); -void FM_PlayAndUpdate(void); -int FM_CheckFrequencyLock(uint16_t Frequency, uint16_t LowerLimit); - -void FM_process_key(key_code_t Key, bool bKeyPressed, bool bKeyHeld); - -void FM_Play(void); -void FM_Start(void); +bool FM_check_valid_channel(const unsigned int Channel); +unsigned int FM_find_next_channel(unsigned int Channel, const fm_scan_state_dir_t scan_state_dir); +int FM_configure_channel_state(void); +void FM_erase_channels(void); +void FM_tune(uint16_t frequency, const fm_scan_state_dir_t scan_state_dir, const bool flag); +void FM_stop_scan(void); +int FM_check_frequency_lock(uint16_t Frequency, uint16_t LowerLimit); +void FM_scan(void); +void FM_turn_on(void); +void FM_turn_off(void); +void FM_process_key(key_code_t Key, bool bKeyPressed, bool bKeyHeld); #endif diff --git a/app/generic.c b/app/generic.c index 795ba60..9c4167d 100644 --- a/app/generic.c +++ b/app/generic.c @@ -142,9 +142,9 @@ void GENERIC_Key_PTT(bool key_pressed) // UART_printf("gene key 1 %u\r\n", key_pressed); #endif - if (g_scan_state_dir != SCAN_STATE_DIR_OFF || // freq/chan scanning + if (g_scan_state_dir != SCAN_STATE_DIR_OFF || // freq/chan scanning g_current_display_screen == DISPLAY_SEARCH || // CTCSS/CDCSS scanning - g_css_scan_mode != CSS_SCAN_MODE_OFF) // " " + g_css_scan_mode != CSS_SCAN_MODE_OFF) // " " { // we're scanning .. stop if (g_current_display_screen == DISPLAY_SEARCH) @@ -174,9 +174,9 @@ void GENERIC_Key_PTT(bool key_pressed) } #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_PlayAndUpdate(); + FM_stop_scan(); #ifdef ENABLE_VOICE g_another_voice_id = VOICE_ID_SCANNING_STOP; #endif diff --git a/board.c b/board.c index 979a432..7b6efa3 100644 --- a/board.c +++ b/board.c @@ -584,7 +584,7 @@ void BOARD_eeprom_load(void) // 0E40..0E67 EEPROM_ReadBuffer(0x0E40, g_fm_channels, sizeof(g_fm_channels)); - FM_ConfigureChannelState(); + FM_configure_channel_state(); #endif // 0E90..0E97 diff --git a/driver/bk1080.c b/driver/bk1080.c index c956f32..dcd06bb 100644 --- a/driver/bk1080.c +++ b/driver/bk1080.c @@ -33,10 +33,10 @@ #if defined(ENABLE_FMRADIO_64_108) const freq_band_table_t FM_RADIO_FREQ_BAND_TABLE[] = { - {875, 1080}, - {760, 1080}, - {760, 900}, - {640, 760} + {875, 1080}, // 87.5 ~ 108 MHz + {760, 1080}, // 76 ~ 108 MHz + {760, 900}, // 76 ~ 90 MHz + {640, 760} // 64 ~ 76 MHz }; #elif defined(ENABLE_FMRADIO_875_108) 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; +// #if (ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE) > 1) // compiler doesn't like this :( #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_upper = 0; + for (i = 0; i < ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE); i++) { - const unsigned int lower = FM_RADIO_FREQ_BAND_TABLE[i].lower; - const unsigned int upper = FM_RADIO_FREQ_BAND_TABLE[i].upper; + const uint16_t lower = FM_RADIO_FREQ_BAND_TABLE[i].lower; + const uint16_t upper = FM_RADIO_FREQ_BAND_TABLE[i].upper; if (BK1080_freq_lower > lower) BK1080_freq_lower = lower; - if (BK1080_freq_lower > upper) - BK1080_freq_lower = upper; +// if (BK1080_freq_lower > upper) +// BK1080_freq_lower = upper; - if (BK1080_freq_upper < lower) - BK1080_freq_upper = lower; +// if (BK1080_freq_upper < lower) +// BK1080_freq_upper = lower; if (BK1080_freq_upper < upper) BK1080_freq_upper = upper; } + #else + // only 1 band is used BK1080_freq_lower = FM_RADIO_FREQ_BAND_TABLE[0].lower; BK1080_freq_upper = FM_RADIO_FREQ_BAND_TABLE[0].upper; #endif @@ -144,8 +150,8 @@ void BK1080_Init(const uint16_t frequency, const bool initialise) SYSTEM_DelayMs(250); - BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA83C); - BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA8BC); + BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA83C); // 1010 1000 0011 1100 + BK1080_WriteRegister(BK1080_REG_25_INTERNAL, 0xA8BC); // 1010 1000 1011 1100 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 defined(ENABLE_FMRADIO_64_108) + // determine which band to use + 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) break; if (band >= ARRAY_SIZE(FM_RADIO_FREQ_BAND_TABLE)) - { + { // out of band + Frequency = BK1080_freq_lower; // return; } BK1080_WriteRegister(BK1080_REG_05_SYSTEM_CONFIGURATION2, (SEEK_THRESHOLD << 8) | (band << 6) | (CHAN_SPACING << 4) | (VOLUME << 0)); + #endif channel = Frequency - FM_RADIO_FREQ_BAND_TABLE[band].lower; // 100kHz channel spacing diff --git a/firmware.bin b/firmware.bin index 6f2b9ce..4c3222b 100644 Binary files a/firmware.bin and b/firmware.bin differ diff --git a/firmware.packed.bin b/firmware.packed.bin index cb5539f..a6732c9 100644 Binary files a/firmware.packed.bin and b/firmware.packed.bin differ diff --git a/scheduler.c b/scheduler.c index b30e2bb..ad9c875 100644 --- a/scheduler.c +++ b/scheduler.c @@ -110,7 +110,7 @@ void SystickHandler(void) #endif #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_current_function != FUNCTION_TRANSMIT && g_current_function != FUNCTION_RECEIVE) diff --git a/ui/fmradio.c b/ui/fmradio.c index e574b8c..3158b16 100644 --- a/ui/fmradio.c +++ b/ui/fmradio.c @@ -68,7 +68,7 @@ void UI_DisplayFM(void) { 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) { @@ -106,7 +106,7 @@ void UI_DisplayFM(void) const unsigned int chan = g_fm_channel_position; const uint32_t freq = g_fm_channels[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); } else @@ -144,7 +144,7 @@ void UI_DisplayFM(void) // ************************************* // 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_0A = BK1080_ReadRegister(0x0A); diff --git a/ui/ui.c b/ui/ui.c index e9b3fc0..01bfa35 100644 --- a/ui/ui.c +++ b/ui/ui.c @@ -90,8 +90,8 @@ void GUI_SelectNextDisplay(gui_display_type_t Display) g_css_scan_mode = CSS_SCAN_MODE_OFF; g_scan_state_dir = SCAN_STATE_DIR_OFF; #ifdef ENABLE_FMRADIO - g_fm_scan_state = FM_SCAN_OFF; - g_update_display = true; + g_fm_scan_state_dir = FM_SCAN_STATE_DIR_OFF; + g_update_display = true; #endif g_ask_for_confirmation = 0; g_ask_to_save = false;