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

325 lines
8.2 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/dtmf.h"
#ifdef ENABLE_FMRADIO
2023-09-14 09:56:30 +01:00
#include "app/fm.h"
#endif
2023-09-09 08:03:56 +01:00
#include "bsp/dp32g030/gpio.h"
#include "dcs.h"
#include "driver/backlight.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"
#include "driver/system.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 "helper/battery.h"
2023-10-25 19:26:22 +01:00
#ifdef ENABLE_MDC1200
#include "mdc1200.h"
#endif
2023-09-09 08:03:56 +01:00
#include "misc.h"
#include "radio.h"
#include "settings.h"
#include "ui/status.h"
#include "ui/ui.h"
2023-10-08 20:23:37 +01:00
function_type_t g_current_function;
2023-09-09 08:03:56 +01:00
void FUNCTION_Init(void)
{
if (IS_NOT_NOAA_CHANNEL(g_rx_vfo->channel_save))
2023-09-09 08:03:56 +01:00
{
2023-10-08 20:23:37 +01:00
g_current_code_type = g_selected_code_type;
if (g_css_scan_mode == CSS_SCAN_MODE_OFF)
2023-11-02 21:44:53 +00:00
g_current_code_type = (g_rx_vfo->channel.am_mode > 0) ? CODE_TYPE_NONE : g_rx_vfo->p_rx->code_type;
2023-09-09 08:03:56 +01:00
}
else
g_current_code_type = CODE_TYPE_CONTINUOUS_TONE;
2023-09-14 09:56:30 +01:00
2023-09-30 11:22:19 +01:00
DTMF_clear_RX();
g_cxcss_tail_found = false;
g_cdcss_lost = false;
g_ctcss_lost = false;
#ifdef ENABLE_VOX
2023-10-29 22:33:38 +00:00
g_vox_lost = false;
#endif
2023-10-29 22:33:38 +00:00
g_squelch_open = false;
2023-10-29 22:33:38 +00:00
g_flag_tail_tone_elimination_complete = false;
g_tail_tone_elimination_tick_10ms = 0;
g_found_ctcss = false;
g_found_cdcss = false;
g_found_ctcss_tick_10ms = 0;
g_found_cdcss_tick_10ms = 0;
g_end_of_rx_detected_maybe = false;
#ifdef ENABLE_NOAA
g_noaa_tick_10ms = 0;
#endif
2023-10-08 20:23:37 +01:00
g_update_status = true;
2023-09-09 08:03:56 +01:00
}
2023-10-08 20:23:37 +01:00
void FUNCTION_Select(function_type_t Function)
2023-09-09 08:03:56 +01:00
{
const function_type_t prev_func = g_current_function;
const bool was_power_save = (prev_func == FUNCTION_POWER_SAVE);
2023-09-09 08:03:56 +01:00
2023-10-08 20:23:37 +01:00
g_current_function = Function;
2023-09-09 08:03:56 +01:00
if (was_power_save && Function != FUNCTION_POWER_SAVE)
{ // wake up
2023-10-29 22:33:38 +00:00
BK4819_Conditional_RX_TurnOn();
2023-10-08 17:14:13 +01:00
g_rx_idle_mode = false;
UI_DisplayStatus(false);
2023-09-09 08:03:56 +01:00
}
g_update_status = true;
switch (Function)
{
case FUNCTION_FOREGROUND:
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
UART_SendText("func forground\r\n");
#endif
2023-10-08 20:23:37 +01:00
if (g_dtmf_reply_state != DTMF_REPLY_NONE)
RADIO_PrepareCssTX();
2023-09-14 09:56:30 +01:00
if (prev_func == FUNCTION_TRANSMIT)
{
2023-10-08 20:23:37 +01:00
g_vfo_rssi_bar_level[0] = 0;
g_vfo_rssi_bar_level[1] = 0;
2023-09-09 08:03:56 +01:00
}
else
if (prev_func != FUNCTION_RECEIVE)
2023-09-14 09:56:30 +01:00
break;
#ifdef ENABLE_FMRADIO
2023-10-08 20:23:37 +01:00
if (g_fm_radio_mode)
2023-11-02 10:00:51 +00:00
g_fm_restore_tick_10ms = g_eeprom.config.setting.scan_hold_time * 50;
2023-09-14 09:56:30 +01:00
#endif
2023-10-08 20:23:37 +01:00
if (g_dtmf_call_state == DTMF_CALL_STATE_CALL_OUT ||
g_dtmf_call_state == DTMF_CALL_STATE_RECEIVED ||
g_dtmf_call_state == DTMF_CALL_STATE_RECEIVED_STAY)
{
2023-11-02 10:00:51 +00:00
g_dtmf_auto_reset_time_500ms = g_eeprom.config.setting.dtmf.auto_reset_time * 2;
}
2023-09-09 08:03:56 +01:00
return;
2023-10-23 14:02:54 +01:00
case FUNCTION_NEW_RECEIVE:
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
2023-10-23 14:02:54 +01:00
UART_SendText("func new receive\r\n");
#endif
break;
case FUNCTION_RECEIVE:
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
UART_SendText("func receive\r\n");
#endif
break;
case FUNCTION_POWER_SAVE:
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
UART_SendText("func power save\r\n");
#endif
2023-10-29 22:33:38 +00:00
if (g_flash_light_state != FLASHLIGHT_SOS)
{
g_monitor_enabled = false;
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
}
2023-11-02 10:00:51 +00:00
g_power_save_tick_10ms = g_eeprom.config.setting.battery_save_ratio * 10;
2023-10-29 22:33:38 +00:00
g_power_save_expired = false;
2023-10-08 17:14:13 +01:00
g_rx_idle_mode = true;
BK4819_DisableVox();
BK4819_Sleep();
2023-10-17 21:22:40 +01:00
BK4819_set_GPIO_pin(BK4819_GPIO0_PIN28_RX_ENABLE, false);
2023-09-17 09:54:24 +01:00
2023-10-29 22:33:38 +00:00
if (g_current_display_screen != DISPLAY_MENU)
2023-10-03 00:14:36 +01:00
GUI_SelectNextDisplay(DISPLAY_MAIN);
return;
case FUNCTION_TRANSMIT:
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
UART_SendText("func transmit\r\n");
#endif
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.backlight_on_tx_rx == 1 || g_eeprom.config.setting.backlight_on_tx_rx == 3)
2023-10-18 11:31:30 +01:00
backlight_turn_on(backlight_tx_rx_time_500ms);
2023-11-02 10:00:51 +00:00
if (g_eeprom.config.setting.dual_watch != DUAL_WATCH_OFF)
2023-10-18 11:31:30 +01:00
{ // dual-RX is enabled
2023-10-29 22:33:38 +00:00
g_dual_watch_tick_10ms = dual_watch_delay_after_tx_10ms;
2023-11-02 10:00:51 +00:00
if (g_dual_watch_tick_10ms < (g_eeprom.config.setting.scan_hold_time * 50))
g_dual_watch_tick_10ms = g_eeprom.config.setting.scan_hold_time * 50;
2023-10-18 11:31:30 +01:00
}
2023-10-26 23:14:46 +01:00
#ifdef ENABLE_MDC1200
BK4819_enable_mdc1200_rx(false);
#endif
// if DTMF is enabled when TX'ing, it changes the TX audio filtering ! .. 1of11
// so MAKE SURE that DTMF is disabled - until needed
BK4819_DisableDTMF();
2023-09-30 11:22:19 +01:00
// clear the DTMF RX buffer
DTMF_clear_RX();
// clear the DTMF RX live decoder buffer
2023-10-08 20:23:37 +01:00
g_dtmf_rx_live_timeout = 0;
memset(g_dtmf_rx_live, 0, sizeof(g_dtmf_rx_live));
#ifdef ENABLE_FMRADIO
2023-10-18 11:31:30 +01:00
// disable the FM radio
2023-10-08 20:23:37 +01:00
if (g_fm_radio_mode)
2023-09-14 09:56:30 +01:00
BK1080_Init(0, false);
#endif
2023-10-18 11:31:30 +01:00
g_update_status = true;
GUI_DisplayScreen();
2023-09-14 09:56:30 +01:00
#ifdef ENABLE_ALARM
2023-11-02 10:00:51 +00:00
if (g_alarm_state == ALARM_STATE_TXALARM && g_eeprom.config.setting.alarm_mode != ALARM_MODE_TONE)
2023-10-18 11:31:30 +01:00
{ // enable the alarm tone but not the TX
2023-10-08 20:23:37 +01:00
g_alarm_state = ALARM_STATE_ALARM;
GUI_DisplayScreen();
2023-10-18 11:31:30 +01:00
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
2023-10-29 22:33:38 +00:00
SYSTEM_DelayMs(2);
2023-11-03 18:41:54 +00:00
BK4819_StartTone1(500, 28, g_current_function == FUNCTION_TRANSMIT);
SYSTEM_DelayMs(2);
2023-10-29 22:33:38 +00:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
SYSTEM_DelayMs(60);
BK4819_ExitTxMute();
2023-10-18 11:31:30 +01:00
g_alarm_tone_counter_10ms = 0;
break;
}
#endif
2023-11-04 02:33:04 +00:00
BK4819_set_scrambler(0);
RADIO_enableTX(false);
#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
2023-10-08 20:23:37 +01:00
if (g_alarm_state != ALARM_STATE_OFF)
{
#ifdef ENABLE_TX1750
2023-10-08 20:23:37 +01:00
if (g_alarm_state == ALARM_STATE_TX1750)
BK4819_TransmitTone(true, 1750);
#endif
#ifdef ENABLE_ALARM
2023-10-08 20:23:37 +01:00
if (g_alarm_state == ALARM_STATE_TXALARM)
BK4819_TransmitTone(true, 500);
#endif
2023-10-18 11:31:30 +01:00
SYSTEM_DelayMs(2);
2023-10-18 11:31:30 +01:00
2023-10-29 22:33:38 +00:00
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_SPEAKER);
2023-10-18 11:31:30 +01:00
#ifdef ENABLE_ALARM
2023-10-18 11:31:30 +01:00
g_alarm_tone_counter_10ms = 0;
#endif
break;
}
2023-10-18 11:31:30 +01:00
else
#endif
2023-10-18 11:31:30 +01:00
if (!DTMF_Reply())
2023-10-25 19:26:22 +01:00
{
2023-10-25 22:38:28 +01:00
#ifdef ENABLE_MDC1200
2023-11-03 00:53:08 +00:00
if (g_current_vfo->channel.mdc1200_mode == MDC1200_MODE_BOT || g_current_vfo->channel.mdc1200_mode == MDC1200_MODE_BOTH)
2023-10-28 08:46:27 +01:00
{
#ifdef ENABLE_MDC1200_SIDE_BEEP
BK4819_StartTone1(880, 50, false);
SYSTEM_DelayMs(120);
BK4819_StopTones(true);
#endif
2023-11-05 23:16:19 +00:00
2023-11-02 10:00:51 +00:00
BK4819_send_MDC1200(MDC1200_OP_CODE_PTT_ID, 0x80, g_eeprom.config.setting.mdc1200_id);
2023-11-05 23:16:19 +00:00
#ifdef ENABLE_MDC1200_SIDE_BEEP
BK4819_StartTone1(880, 50, false);
SYSTEM_DelayMs(120);
BK4819_StopTones(true);
#endif
2023-10-28 08:46:27 +01:00
}
2023-10-25 19:26:22 +01:00
else
2023-10-25 22:38:28 +01:00
#endif
2023-11-02 21:44:53 +00:00
if (g_current_vfo->channel.dtmf_ptt_id_tx_mode == PTT_ID_APOLLO)
{
2023-10-18 11:31:30 +01:00
BK4819_PlayTone(APOLLO_TONE1_HZ, APOLLO_TONE_MS, 0);
}
2023-10-25 19:26:22 +01:00
}
/*
2023-11-05 23:16:19 +00:00
BK4819_write_reg(0x30,
(1u << 15) | // enable VCO calibration
(1u << 14) | // enable something or other
(0u << 10) | // diable RX link
(1u << 9) | // enable AF DAC
(1u << 8) | // enable DISC mode, what's DISC mode ?
(15u << 4) | // enable PLL/VCO
(1u << 3) | // enable PA gain
(1u << 2) | // enable MIC ADC
(1u << 1) | // enable TX DSP
(0u << 0)); // disable RX DSP
*/
2023-11-04 02:33:04 +00:00
if (g_eeprom.config.setting.enable_scrambler)
BK4819_set_scrambler(g_current_vfo->channel.scrambler);
break;
2023-10-16 21:59:17 +01:00
case FUNCTION_PANADAPTER:
2023-10-23 14:02:54 +01:00
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
UART_SendText("func panadpter\r\n");
#endif
break;
2023-09-09 08:03:56 +01:00
}
2023-09-14 09:56:30 +01:00
2023-10-29 22:33:38 +00:00
g_schedule_power_save_tick_10ms = battery_save_count_10ms;
g_schedule_power_save = false;
#ifdef ENABLE_FMRADIO
g_fm_restore_tick_10ms = 0;
2023-09-14 09:56:30 +01:00
#endif
g_update_status = true;
2023-09-09 08:03:56 +01:00
}