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

470 lines
10 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 "app/action.h"
#include "app/app.h"
#include "app/dtmf.h"
#ifdef ENABLE_FMRADIO
2023-09-14 09:56:30 +01:00
#include "app/fm.h"
#endif
#include "app/search.h"
2023-09-09 08:03:56 +01:00
#include "audio.h"
#include "bsp/dp32g030/gpio.h"
#ifdef ENABLE_FMRADIO
2023-09-14 09:56:30 +01:00
#include "driver/bk1080.h"
#endif
2023-09-09 08:03:56 +01:00
#include "driver/bk4819.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
#ifdef ENABLE_SCAN_IGNORE_LIST
#include "freq_ignore.h"
#endif
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"
static void ACTION_FlashLight(void)
{
2023-10-08 20:23:37 +01:00
switch (g_flash_light_state)
2023-09-09 08:03:56 +01:00
{
2023-10-28 22:02:07 +01:00
case FLASHLIGHT_OFF:
g_flash_light_state = FLASHLIGHT_ON;
2023-09-09 08:03:56 +01:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_FLASHLIGHT);
break;
2023-10-28 22:02:07 +01:00
case FLASHLIGHT_ON:
g_flash_light_state = FLASHLIGHT_BLINK;
break;
case FLASHLIGHT_BLINK:
g_flash_light_blink_tick_10ms = 0;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_FLASHLIGHT);
2023-10-29 22:33:38 +00:00
2023-10-28 22:02:07 +01:00
g_flash_light_state = FLASHLIGHT_SOS;
2023-10-29 22:33:38 +00:00
if (g_current_function == FUNCTION_POWER_SAVE)
FUNCTION_Select(FUNCTION_RECEIVE);
2023-09-09 08:03:56 +01:00
break;
2023-10-28 22:02:07 +01:00
case FLASHLIGHT_SOS:
2023-10-29 22:33:38 +00:00
#ifdef ENABLE_FLASH_LIGHT_SOS_TONE
2023-11-06 09:08:09 +00:00
BK4819_stop_tones(g_current_function == FUNCTION_TRANSMIT);
2023-10-29 22:33:38 +00:00
#endif
2023-10-28 22:02:07 +01:00
2023-10-28 23:11:57 +01:00
// Fallthrough
2023-09-09 08:03:56 +01:00
default:
2023-10-28 22:02:07 +01:00
g_flash_light_state = FLASHLIGHT_OFF;
2023-09-09 08:03:56 +01:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_FLASHLIGHT);
2023-10-28 22:02:07 +01:00
break;
2023-09-09 08:03:56 +01:00
}
}
void ACTION_Power(void)
{
2023-11-24 11:52:11 +00:00
if (++g_tx_vfo->channel.tx_power > OUTPUT_POWER_USER)
2023-11-02 21:44:53 +00:00
g_tx_vfo->channel.tx_power = OUTPUT_POWER_LOW;
2023-09-09 08:03:56 +01:00
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
2023-11-02 21:44:53 +00:00
// UART_printf("act_pwr %u\r\n", g_tx_vfo->channel.tx_power);
#endif
2023-10-08 20:23:37 +01:00
g_request_save_channel = 1;
2023-09-09 08:03:56 +01:00
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_VOICE
g_another_voice_id = VOICE_ID_POWER;
2023-09-09 08:03:56 +01:00
#endif
g_request_display_screen = g_current_display_screen;
2023-09-09 08:03:56 +01:00
}
void ACTION_Monitor(void)
2023-09-09 08:03:56 +01:00
{
2023-10-29 22:33:38 +00:00
if (!g_monitor_enabled) // (g_current_function != FUNCTION_MONITOR)
{ // enable monitor mode
g_monitor_enabled = true;
2023-10-30 00:14:38 +00:00
g_beep_to_play = BEEP_NONE;
if (!g_squelch_open && GPIO_CheckBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER))
2023-11-06 09:08:09 +00:00
BK4819_stop_tones(g_current_function == FUNCTION_TRANSMIT);
2023-10-29 22:33:38 +00:00
#ifdef ENABLE_NOAA
2023-11-02 10:00:51 +00:00
// if (g_rx_vfo->channel_save >= NOAA_CHANNEL_FIRST && g_noaa_mode)
if (IS_NOAA_CHANNEL(g_rx_vfo->channel_save) && g_noaa_mode)
2023-10-08 20:23:37 +01:00
g_noaa_channel = g_rx_vfo->channel_save - NOAA_CHANNEL_FIRST;
2023-09-09 08:03:56 +01:00
#endif
2023-10-29 22:33:38 +00:00
APP_start_listening();
2023-09-09 08:03:56 +01:00
return;
}
2023-10-29 22:33:38 +00:00
// disable monitor
2023-10-08 20:23:37 +01:00
g_monitor_enabled = false;
2023-10-29 22:33:38 +00:00
if (!g_squelch_open)
2023-10-29 22:33:38 +00:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
if (g_scan_state_dir != SCAN_STATE_DIR_OFF)
2023-11-07 08:48:32 +00:00
g_scan_tick_10ms = g_eeprom.config.setting.scan_hold_time * 50;
2023-09-09 08:03:56 +01:00
#ifdef g_power_save_expired
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.dual_watch == DUAL_WATCH_OFF && g_noaa_mode)
2023-09-09 08:03:56 +01:00
{
g_noaa_tick_10ms = noaa_tick_10ms;
2023-10-29 22:33:38 +00:00
g_schedule_noaa = false;
2023-09-09 08:03:56 +01:00
}
#endif
RADIO_setup_registers(true);
2023-09-09 08:03:56 +01:00
#ifdef ENABLE_FMRADIO
2023-10-08 20:23:37 +01:00
if (g_fm_radio_mode)
2023-09-14 09:56:30 +01:00
{
2023-10-30 14:12:27 +00:00
FM_turn_on();
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_FM;
2023-09-14 09:56:30 +01:00
}
else
#endif
g_request_display_screen = g_current_display_screen;
2023-09-09 08:03:56 +01:00
}
void ACTION_Scan(bool bRestart)
{
#ifdef ENABLE_FMRADIO
2023-10-08 20:23:37 +01:00
if (g_fm_radio_mode)
2023-09-09 08:03:56 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_current_function != FUNCTION_RECEIVE &&
2023-10-29 22:33:38 +00:00
g_current_function != FUNCTION_TRANSMIT &&
!g_monitor_enabled)
2023-09-09 08:03:56 +01:00
{
2023-09-14 09:56:30 +01:00
GUI_SelectNextDisplay(DISPLAY_FM);
2023-10-30 14:12:27 +00:00
if (g_fm_scan_state_dir != FM_SCAN_STATE_DIR_OFF)
{ // already scanning - stop
2023-10-30 14:12:27 +00:00
FM_stop_scan();
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-14 09:56:30 +01:00
#endif
2023-09-09 08:03:56 +01:00
}
else
{ // start scanning
2023-09-14 09:56:30 +01:00
uint16_t Frequency;
2023-09-14 09:56:30 +01:00
if (bRestart)
{ // scan with auto store
2023-10-30 14:12:27 +00:00
FM_erase_channels();
g_fm_auto_scan = true;
Frequency = BK1080_freq_lower;
2023-09-14 09:56:30 +01:00
}
else
{ // scan without auto store
2023-10-30 14:12:27 +00:00
g_fm_auto_scan = false;
2023-11-02 10:00:51 +00:00
Frequency = g_eeprom.config.setting.fm_radio.selected_frequency;
2023-09-14 09:56:30 +01:00
}
2023-10-30 14:12:27 +00:00
g_fm_channel_position = 0;
2023-10-31 06:56:21 +00:00
BK1080_get_freq_offset(Frequency);
2023-10-11 17:55:34 +01:00
2023-10-30 14:12:27 +00:00
FM_tune(Frequency, FM_SCAN_STATE_DIR_UP, bRestart);
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_BEGIN;
2023-09-14 09:56:30 +01:00
#endif
2023-09-09 08:03:56 +01:00
}
}
2023-09-14 09:56:30 +01:00
return;
2023-09-09 08:03:56 +01:00
}
2023-09-14 09:56:30 +01:00
#endif
if (g_current_display_screen != DISPLAY_SEARCH)
{ // not in freq/ctcss/cdcss search mode
2023-09-20 11:23:45 +01:00
2023-11-08 11:27:26 +00:00
if (IS_NOAA_CHANNEL(g_tx_vfo->channel_save))
return;
2023-10-08 20:23:37 +01:00
g_monitor_enabled = false;
2023-10-29 22:33:38 +00:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
2023-09-30 11:22:19 +01:00
DTMF_clear_RX();
#ifdef ENABLE_DTMF_LIVE_DECODER
g_dtmf_rx_live_timeout = 0;
memset(g_dtmf_rx_live, 0, sizeof(g_dtmf_rx_live));
#endif
2023-09-30 11:22:19 +01:00
RADIO_select_vfos();
2023-09-09 08:03:56 +01:00
2023-11-08 11:27:26 +00:00
GUI_SelectNextDisplay(DISPLAY_MAIN);
2023-10-05 23:58:55 +01:00
2023-11-08 11:27:26 +00:00
if (g_scan_state_dir != SCAN_STATE_DIR_OFF)
{ // currently scanning
if (g_scan_next_channel <= USER_CHANNEL_LAST)
{ // channel mode
if (g_eeprom.config.setting.scan_list_default < 2)
{ // keep scanning but toggle between scan lists
//g_eeprom.config.setting.scan_list_default = (g_eeprom.config.setting.scan_list_default + 1) % 3;
g_eeprom.config.setting.scan_list_default++;
// jump to the next channel
APP_channel_next(true, g_scan_state_dir);
g_scan_tick_10ms = 0;
g_scan_pause_time_mode = false;
g_update_status = true;
return;
2023-10-05 23:58:55 +01:00
}
2023-11-08 11:27:26 +00:00
g_eeprom.config.setting.scan_list_default = 0; // back to scan list 1 - the next time we start scanning
2023-09-09 08:03:56 +01:00
}
2023-11-08 11:27:26 +00:00
// *****************
// stop scanning
APP_stop_scan();
g_request_display_screen = DISPLAY_MAIN;
2023-11-08 11:27:26 +00:00
return;
}
2023-11-08 11:27:26 +00:00
// **********************
// start scanning
{
const uint32_t freq = g_tx_vfo->freq_config_rx.frequency;
const frequency_band_t band = FREQUENCY_GetBand(freq);
g_scan_initial_upper = FREQ_BAND_TABLE[band].upper;
g_scan_initial_lower = FREQ_BAND_TABLE[band].lower;
g_scan_initial_step_size = g_tx_vfo->step_freq;
2023-09-09 08:03:56 +01:00
}
2023-11-08 11:27:26 +00:00
#ifdef ENABLE_SCAN_RANGES
2023-11-09 12:22:54 +00:00
if (IS_FREQ_CHANNEL(g_tx_vfo->channel_save) && g_eeprom.config.setting.scan_ranges_enable)
2023-11-08 11:27:26 +00:00
{
2023-11-11 13:09:24 +00:00
uint32_t freq = g_tx_vfo->freq_config_rx.frequency;
2023-11-08 11:27:26 +00:00
FREQUENCY_scan_range(freq, &g_scan_initial_lower, &g_scan_initial_upper, &g_scan_initial_step_size);
2023-11-11 13:09:24 +00:00
// freq = FREQUENCY_floor_to_step(freq, g_scan_initial_step_size, g_scan_initial_lower, g_scan_initial_upper); }
2023-11-08 11:27:26 +00:00
}
#endif
g_monitor_enabled = false;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
RADIO_setup_registers(true);
APP_channel_next(true, SCAN_STATE_DIR_FORWARD);
2023-11-08 19:07:52 +00:00
g_scan_tick_10ms = 0; // go NOW
2023-11-08 11:27:26 +00:00
g_scan_pause_time_mode = false;
#ifdef ENABLE_VOICE
AUDIO_SetVoiceID(0, VOICE_ID_SCANNING_BEGIN);
AUDIO_PlaySingleVoice(true);
#endif
// clear the other vfo's rssi level (to hide the antenna symbol)
g_vfo_rssi_bar_level[(g_rx_vfo_num + 1) & 1u] = 0;
g_update_status = true;
return;
2023-09-09 08:03:56 +01:00
}
// freq/ctcss/cdcss/search mode
// TODO: fixme
2023-10-05 23:58:55 +01:00
// if (!bRestart)
if (!bRestart && g_scan_next_channel <= USER_CHANNEL_LAST)
2023-10-05 23:58:55 +01:00
{ // channel mode, keep scanning but toggle between scan lists
2023-11-02 10:00:51 +00:00
g_eeprom.config.setting.scan_list_default = (g_eeprom.config.setting.scan_list_default + 1) % 3;
2023-10-05 23:58:55 +01:00
// jump to the next channel
APP_channel_next(true, g_scan_state_dir);
2023-11-07 08:48:32 +00:00
g_scan_tick_10ms = 0;
g_scan_pause_time_mode = false;
2023-10-05 23:58:55 +01:00
2023-10-08 20:23:37 +01:00
g_update_status = true;
2023-10-04 18:54:26 +01:00
}
else
{ // stop scanning
APP_stop_scan();
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_MAIN;
2023-09-20 11:23:45 +01:00
}
2023-09-09 08:03:56 +01:00
}
#ifdef ENABLE_VOX
void ACTION_Vox(void)
{
// toggle VOX on/off
g_eeprom.config.setting.vox_enabled = (g_eeprom.config.setting.vox_enabled + 1) & 1u;
g_request_save_settings = true;
g_flag_reconfigure_vfos = true;
#ifdef ENABLE_VOICE
2023-11-02 10:00:51 +00:00
g_another_voice_id = VOICE_ID_VOX;
#endif
2023-11-02 10:00:51 +00:00
g_update_status = true;
}
#endif
2023-09-09 08:03:56 +01:00
#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
static void ACTION_AlarmOr1750(const bool b1750)
2023-09-09 09:01:52 +01:00
{
2023-10-08 20:23:37 +01:00
g_input_box_index = 0;
2023-11-06 12:21:52 +00:00
(void)b1750; // stop compile warning
2023-10-10 10:42:22 +01:00
#if defined(ENABLE_ALARM) && defined(ENABLE_TX1750)
2023-10-08 20:23:37 +01:00
g_alarm_state = b1750 ? ALARM_STATE_TX1750 : ALARM_STATE_TXALARM;
2023-10-18 11:31:30 +01:00
g_alarm_running_counter_10ms = 0;
#elif defined(ENABLE_ALARM)
2023-10-08 20:23:37 +01:00
g_alarm_state = ALARM_STATE_TXALARM;
2023-10-18 11:31:30 +01:00
g_alarm_running_counter_10ms = 0;
#else
2023-10-08 20:23:37 +01:00
g_alarm_state = ALARM_STATE_TX1750;
#endif
2023-10-08 20:23:37 +01:00
g_flag_prepare_tx = true;
2023-10-03 00:14:36 +01:00
2023-11-06 12:21:52 +00:00
if (g_current_display_screen != DISPLAY_MENU)
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_MAIN;
2023-09-09 09:01:52 +01:00
}
#endif
2023-09-09 08:03:56 +01:00
#ifdef ENABLE_FMRADIO
2023-09-14 09:56:30 +01:00
void ACTION_FM(void)
2023-09-09 08:03:56 +01:00
{
2023-10-30 01:57:11 +00:00
if (g_current_function != FUNCTION_TRANSMIT)
2023-09-09 08:03:56 +01:00
{
2023-10-08 20:23:37 +01:00
if (g_fm_radio_mode)
{ // return normal service
2023-10-30 14:12:27 +00:00
FM_turn_off();
2023-10-11 17:55:34 +01:00
g_input_box_index = 0;
#ifdef ENABLE_VOX
2023-10-29 22:33:38 +00:00
g_vox_resume_tick_10ms = 80;
#endif
2023-10-08 20:23:37 +01:00
g_flag_reconfigure_vfos = true;
2023-10-03 00:14:36 +01:00
2023-10-08 20:23:37 +01:00
g_request_display_screen = DISPLAY_MAIN;
2023-09-14 09:56:30 +01:00
return;
}
// switch to FM radio mode
2023-10-30 01:57:11 +00:00
g_monitor_enabled = false;
RADIO_select_vfos();
RADIO_setup_registers(true);
2023-10-30 14:12:27 +00:00
FM_turn_on();
2023-10-08 20:23:37 +01:00
g_input_box_index = 0;
2023-10-03 00:14:36 +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-09-14 09:56:30 +01:00
#endif
2023-09-09 08:03:56 +01:00
2023-10-16 06:54:27 +01:00
void ACTION_process(const key_code_t Key, const bool key_pressed, const bool key_held)
2023-09-09 08:03:56 +01:00
{
2023-09-12 11:01:34 +01:00
uint8_t Short = ACTION_OPT_NONE;
uint8_t Long = ACTION_OPT_NONE;
2023-09-09 08:03:56 +01:00
if (Key == KEY_SIDE1)
{
2023-11-02 10:00:51 +00:00
Short = g_eeprom.config.setting.key1_short;
Long = g_eeprom.config.setting.key1_long;
2023-09-09 08:03:56 +01:00
}
else
2023-09-12 11:01:34 +01:00
if (Key == KEY_SIDE2)
2023-09-09 08:03:56 +01:00
{
2023-11-02 10:00:51 +00:00
Short = g_eeprom.config.setting.key2_short;
Long = g_eeprom.config.setting.key2_long;
2023-09-09 08:03:56 +01:00
}
2023-10-08 20:23:37 +01:00
if (!key_held && key_pressed)
2023-09-09 08:03:56 +01:00
return;
2023-10-08 20:23:37 +01:00
if (key_held || key_pressed)
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;
Short = Long;
2023-09-12 11:01:34 +01:00
2023-10-08 20:23:37 +01:00
if (!key_pressed)
2023-09-09 08:03:56 +01:00
return;
}
switch (Short)
{
2023-09-12 11:01:34 +01:00
default:
case ACTION_OPT_NONE:
2023-09-12 11:01:34 +01:00
break;
case ACTION_OPT_FLASHLIGHT:
2023-09-09 08:03:56 +01:00
ACTION_FlashLight();
break;
2023-09-12 11:01:34 +01:00
case ACTION_OPT_POWER:
2023-09-09 08:03:56 +01:00
ACTION_Power();
break;
2023-09-12 11:01:34 +01:00
case ACTION_OPT_MONITOR:
2023-09-09 08:03:56 +01:00
ACTION_Monitor();
break;
2023-09-12 11:01:34 +01:00
case ACTION_OPT_SCAN:
2023-09-09 08:03:56 +01:00
ACTION_Scan(true);
break;
2023-09-12 11:01:34 +01:00
case ACTION_OPT_VOX:
#ifdef ENABLE_VOX
ACTION_Vox();
#endif
2023-09-09 08:03:56 +01:00
break;
2023-09-12 11:01:34 +01:00
case ACTION_OPT_ALARM:
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_ALARM
2023-09-09 09:01:52 +01:00
ACTION_AlarmOr1750(false);
#endif
2023-09-09 08:03:56 +01:00
break;
#ifdef ENABLE_FMRADIO
2023-09-14 09:56:30 +01:00
case ACTION_OPT_FM:
ACTION_FM();
break;
#endif
2023-09-12 11:01:34 +01:00
case ACTION_OPT_1750:
#ifdef ENABLE_TX1750
2023-09-09 09:01:52 +01:00
ACTION_AlarmOr1750(true);
#endif
2023-09-09 08:03:56 +01:00
break;
}
}