custom-uv-k5-firmware/functions.c

191 lines
4.7 KiB
C
Raw Normal View History

/* 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>
2023-08-31 21:22:08 +08:00
#include "app/dtmf.h"
2023-09-14 05:55:14 +08:00
#if defined(ENABLE_FMRADIO)
2023-08-29 07:15:44 +08:00
#include "app/fm.h"
2023-09-14 05:55:14 +08:00
#endif
2023-08-15 00:30:30 +08:00
#include "bsp/dp32g030/gpio.h"
#include "dcs.h"
2023-09-14 05:55:14 +08:00
#if defined(ENABLE_FMRADIO)
2023-08-15 00:30:30 +08:00
#include "driver/bk1080.h"
2023-09-14 05:55:14 +08:00
#endif
2023-08-15 00:30:30 +08:00
#include "driver/bk4819.h"
#include "driver/gpio.h"
#include "driver/system.h"
#include "functions.h"
#include "helper/battery.h"
#include "misc.h"
#include "radio.h"
2023-08-14 19:28:09 +08:00
#include "settings.h"
#include "ui/status.h"
#include "ui/ui.h"
2023-08-15 21:24:40 +08:00
FUNCTION_Type_t gCurrentFunction;
void FUNCTION_Init(void)
{
2023-09-03 22:56:49 +08:00
if (IS_NOT_NOAA_CHANNEL(gRxVfo->CHANNEL_SAVE)) {
2023-09-08 02:57:40 +08:00
gCurrentCodeType = gSelectedCodeType;
2023-09-03 22:56:49 +08:00
if (gCssScanMode == CSS_SCAN_MODE_OFF) {
if (gRxVfo->IsAM) {
2023-09-08 02:57:40 +08:00
gCurrentCodeType = CODE_TYPE_OFF;
} else {
2023-09-08 02:57:40 +08:00
gCurrentCodeType = gRxVfo->pRX->CodeType;
}
}
} else {
2023-09-08 02:57:40 +08:00
gCurrentCodeType = CODE_TYPE_CONTINUOUS_TONE;
}
2023-08-31 21:22:08 +08:00
gDTMF_RequestPending = false;
2023-08-16 06:54:04 +08:00
gDTMF_WriteIndex = 0;
memset(gDTMF_Received, 0, sizeof(gDTMF_Received));
2023-08-16 06:46:09 +08:00
g_CxCSS_TAIL_Found = false;
g_CDCSS_Lost = false;
g_CTCSS_Lost = false;
g_VOX_Lost = false;
2023-08-26 19:41:11 +08:00
g_SquelchLost = false;
2023-09-06 06:47:26 +08:00
gTailNoteEliminationCountdown = 0;
2023-09-07 20:30:52 +08:00
gFlagTteComplete = false;
2023-08-30 20:21:07 +08:00
gFoundCTCSS = false;
gFoundCDCSS = false;
gFoundCTCSSCountdown = 0;
gFoundCDCSSCountdown = 0;
2023-09-03 22:56:49 +08:00
gEndOfRxDetectedMaybe = false;
gSystickCountdown2 = 0;
}
2023-08-15 21:24:40 +08:00
void FUNCTION_Select(FUNCTION_Type_t Function)
{
2023-08-15 21:24:40 +08:00
FUNCTION_Type_t PreviousFunction;
2023-08-15 00:30:30 +08:00
bool bWasPowerSave;
2023-08-14 19:28:09 +08:00
PreviousFunction = gCurrentFunction;
2023-08-15 00:30:30 +08:00
bWasPowerSave = (PreviousFunction == FUNCTION_POWER_SAVE);
2023-08-14 19:28:09 +08:00
gCurrentFunction = Function;
2023-08-15 00:30:30 +08:00
if (bWasPowerSave) {
if (Function != FUNCTION_POWER_SAVE) {
BK4819_Conditional_RX_TurnOn_and_GPIO6_Enable();
2023-09-03 22:56:49 +08:00
gRxIdleMode = false;
UI_DisplayStatus();
2023-08-14 19:28:09 +08:00
}
}
switch (Function) {
2023-09-05 05:26:57 +08:00
case FUNCTION_FOREGROUND:
if (gDTMF_ReplyState != DTMF_REPLY_NONE) {
2023-09-03 22:56:49 +08:00
RADIO_PrepareCssTX();
2023-08-14 19:28:09 +08:00
}
2023-08-15 00:30:30 +08:00
if (PreviousFunction == FUNCTION_TRANSMIT) {
2023-08-15 21:24:40 +08:00
gVFO_RSSI_Level[0] = 0;
gVFO_RSSI_Level[1] = 0;
2023-09-14 07:29:36 +08:00
} else if (PreviousFunction != FUNCTION_RECEIVE) {
break;
}
2023-09-14 05:55:14 +08:00
#if defined(ENABLE_FMRADIO)
2023-09-14 07:29:36 +08:00
if (gFmRadioMode) {
gFM_RestoreCountdown = 500;
}
2023-09-14 05:55:14 +08:00
#endif
2023-09-14 07:29:36 +08:00
if (gDTMF_CallState == DTMF_CALL_STATE_CALL_OUT || gDTMF_CallState == DTMF_CALL_STATE_RECEIVED) {
gDTMF_AUTO_RESET_TIME = 1 + (gEeprom.DTMF_AUTO_RESET_TIME * 2);
2023-08-14 19:28:09 +08:00
}
return;
2023-08-14 19:28:09 +08:00
case FUNCTION_MONITOR:
2023-09-05 05:26:57 +08:00
case FUNCTION_INCOMING:
case FUNCTION_RECEIVE:
break;
2023-08-14 19:28:09 +08:00
case FUNCTION_POWER_SAVE:
2023-08-14 19:28:09 +08:00
gBatterySave = gEeprom.BATTERY_SAVE * 10;
2023-09-03 22:56:49 +08:00
gRxIdleMode = true;
2023-08-14 19:28:09 +08:00
BK4819_DisableVox();
BK4819_Sleep();
BK4819_ToggleGpioOut(BK4819_GPIO6_PIN2, false);
2023-08-15 18:11:40 +08:00
gBatterySaveCountdownExpired = false;
2023-08-30 07:19:12 +08:00
gUpdateStatus = true;
2023-08-14 19:28:09 +08:00
GUI_SelectNextDisplay(DISPLAY_MAIN);
return;
2023-08-14 19:28:09 +08:00
case FUNCTION_TRANSMIT:
2023-09-14 05:55:14 +08:00
#if defined(ENABLE_FMRADIO)
if (gFmRadioMode) {
BK1080_Init(0, false);
}
2023-09-14 05:55:14 +08:00
#endif
2023-08-15 00:30:30 +08:00
2023-09-14 23:32:21 +08:00
#if defined(ENABLE_ALARM)
if (gAlarmState == ALARM_STATE_TXALARM && gEeprom.ALARM_MODE != ALARM_MODE_TONE) {
gAlarmState = ALARM_STATE_ALARM;
GUI_DisplayScreen();
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
SYSTEM_DelayMs(20);
BK4819_PlayTone(500, 0);
SYSTEM_DelayMs(2);
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
gEnableSpeaker = true;
SYSTEM_DelayMs(60);
BK4819_ExitTxMute();
gAlarmToneCounter = 0;
break;
}
2023-09-14 23:32:21 +08:00
#endif
2023-08-15 00:30:30 +08:00
GUI_DisplayScreen();
RADIO_SetTxParameters();
BK4819_ToggleGpioOut(BK4819_GPIO1_PIN29_RED, true);
2023-08-15 00:30:30 +08:00
DTMF_Reply();
2023-09-14 23:32:21 +08:00
#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
if (gAlarmState != ALARM_STATE_OFF) {
2023-09-14 23:32:21 +08:00
#if defined(ENABLE_TX1750)
if (gAlarmState == ALARM_STATE_TX1750) {
BK4819_TransmitTone(true, 1750);
2023-09-14 23:32:21 +08:00
}
#endif
#if defined(ENABLE_ALARM)
if (gAlarmState == ALARM_STATE_TXALARM) {
BK4819_TransmitTone(true, 500);
}
2023-09-14 23:32:21 +08:00
#endif
SYSTEM_DelayMs(2);
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_AUDIO_PATH);
2023-09-14 23:32:21 +08:00
#if defined(ENABLE_ALARM)
gAlarmToneCounter = 0;
2023-09-14 23:32:21 +08:00
#endif
gEnableSpeaker = true;
break;
2023-08-15 00:30:30 +08:00
}
2023-09-14 23:32:21 +08:00
#endif
if (gCurrentVfo->SCRAMBLING_TYPE && gSetting_ScrambleEnable) {
BK4819_EnableScramble(gCurrentVfo->SCRAMBLING_TYPE - 1U);
} else {
BK4819_DisableScramble();
}
break;
2023-08-15 00:30:30 +08:00
}
gBatterySaveCountdown = 1000;
gSchedulePowerSave = false;
2023-09-14 05:55:14 +08:00
#if defined(ENABLE_FMRADIO)
2023-09-14 07:29:36 +08:00
gFM_RestoreCountdown = 0;
2023-09-14 05:55:14 +08:00
#endif
2023-08-15 00:30:30 +08:00
}