From aa69d698a77d78f95a7f382bf1fe8e28076d8540 Mon Sep 17 00:00:00 2001 From: mrkusypl Date: Sun, 22 Mar 2026 20:58:27 +0100 Subject: [PATCH] Code optimization (32 B); Restore full VFO state on long press of EXIT --- App/app/action.c | 27 ++++++++--------- App/app/app.c | 4 +-- App/app/common.c | 11 +++++++ App/app/main.c | 76 ++++++++++++++++++------------------------------ App/misc.c | 1 + App/misc.h | 1 + 6 files changed, 58 insertions(+), 62 deletions(-) diff --git a/App/app/action.c b/App/app/action.c index 753f2e1c..f62d4bb0 100644 --- a/App/app/action.c +++ b/App/app/action.c @@ -319,19 +319,23 @@ void ACTION_Handle(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) return; } - enum ACTION_OPT_t funcShort = ACTION_OPT_NONE; - enum ACTION_OPT_t funcLong = ACTION_OPT_NONE; + enum ACTION_OPT_t func = ACTION_OPT_NONE; switch(Key) { case KEY_SIDE1: - funcShort = gEeprom.KEY_1_SHORT_PRESS_ACTION; - funcLong = gEeprom.KEY_1_LONG_PRESS_ACTION; + if (bKeyHeld) + func = gEeprom.KEY_1_LONG_PRESS_ACTION; + else + func = gEeprom.KEY_1_SHORT_PRESS_ACTION; break; case KEY_SIDE2: - funcShort = gEeprom.KEY_2_SHORT_PRESS_ACTION; - funcLong = gEeprom.KEY_2_LONG_PRESS_ACTION; + if (bKeyHeld) + func = gEeprom.KEY_2_LONG_PRESS_ACTION; + else + func = gEeprom.KEY_2_SHORT_PRESS_ACTION; break; case KEY_MENU: - funcLong = gEeprom.KEY_M_LONG_PRESS_ACTION; + if (bKeyHeld) + func = gEeprom.KEY_M_LONG_PRESS_ACTION; break; default: break; @@ -347,17 +351,14 @@ void ACTION_Handle(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) if(!(bKeyHeld && !bKeyPressed)) // don't beep on released after hold gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; - if (bKeyHeld || bKeyPressed) // held + if (bKeyHeld && !bKeyPressed) // button released after hold { - funcShort = funcLong; - - if (!bKeyPressed) //ignore release if held - return; + return; } // held or released after short press beyond this point - action_opt_table[funcShort](); + action_opt_table[func](); } diff --git a/App/app/app.c b/App/app/app.c index 3b63c23d..d9319ebd 100644 --- a/App/app/app.c +++ b/App/app/app.c @@ -1497,7 +1497,6 @@ void cancelUserInputModes(void) if (gDTMF_InputMode || gDTMF_InputBox_Index > 0) { DTMF_clear_input_box(); - gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; gRequestDisplayScreen = DISPLAY_MAIN; gUpdateDisplay = true; } @@ -1539,6 +1538,7 @@ void APP_TimeSlice500ms(void) } cancelUserInputModes(); + gHasVfoBackup = false; } } @@ -1909,7 +1909,7 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) // cancel user input cancelUserInputModes(); - gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; + gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; if (gMonitor) ACTION_Monitor(); //turn off the monitor diff --git a/App/app/common.c b/App/app/common.c index d647efef..fdeb93c5 100644 --- a/App/app/common.c +++ b/App/app/common.c @@ -3,6 +3,7 @@ #include "functions.h" #include "misc.h" #include "settings.h" +#include "ui/inputbox.h" #include "ui/ui.h" void COMMON_KeypadLockToggle() @@ -29,6 +30,11 @@ void COMMON_SwitchVFOs() #endif gEeprom.TX_VFO ^= 1; + if (gInputBoxIndex > 0) { + gInputBoxIndex = 0; + gHasVfoBackup = false; + } + if (gEeprom.CROSS_BAND_RX_TX != CROSS_BAND_OFF) gEeprom.CROSS_BAND_RX_TX = gEeprom.TX_VFO + 1; if (gEeprom.DUAL_WATCH != DUAL_WATCH_OFF) @@ -49,6 +55,11 @@ void COMMON_SwitchVFOMode() if (gEeprom.VFO_OPEN) #endif { + if (gInputBoxIndex > 0) { + gInputBoxIndex = 0; + gHasVfoBackup = false; + } + if (IS_MR_CHANNEL(gTxVfo->CHANNEL_SAVE)) { // swap to frequency mode gEeprom.ScreenChannel[gEeprom.TX_VFO] = gEeprom.FreqChannel[gEeprom.TX_VFO]; diff --git a/App/app/main.c b/App/app/main.c index 2222668b..df341e2d 100644 --- a/App/app/main.c +++ b/App/app/main.c @@ -51,7 +51,26 @@ static VFO_Info_t gVfoBackup; static uint16_t gScreenChannelBackup = 0; static uint16_t gFreqChannelBackup = 0; -static bool gHasVfoBackup = false; + +static void VFO_RestoreBackup(void) { + if (gHasVfoBackup) { + const uint8_t Vfo = gEeprom.TX_VFO; + + // Restore indices + gEeprom.ScreenChannel[Vfo] = gScreenChannelBackup; + gEeprom.FreqChannel[Vfo] = gFreqChannelBackup; + + // Restore full VFO + memcpy(gTxVfo, &gVfoBackup, sizeof(VFO_Info_t)); + + // Save and apply + SETTINGS_SaveVfoIndices(); + RADIO_ConfigureSquelchAndOutputPower(gTxVfo); + RADIO_SetupRegisters(true); + + gHasVfoBackup = false; + } +} static void toggle_chan_scanlist(void) { // toggle the selected channels scanlist setting @@ -613,6 +632,7 @@ static void MAIN_Key_DIGITS(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) } gInputBoxIndex = 0; + gHasVfoBackup = false; uint8_t Channel = (gInputBox[0] * 10) + gInputBox[1]; if (Channel >= 1 && Channel <= ARRAY_SIZE(NoaaFrequencyTable)) { @@ -673,35 +693,10 @@ static void MAIN_Key_EXIT(bool bKeyPressed, bool bKeyHeld) if (bKeyHeld) { // exit key held down if (bKeyPressed) { - if (gInputBoxIndex > 0 || gDTMF_InputBox_Index > 0 || gDTMF_InputMode) - { // cancel key input mode (channel/frequency entry) + // Restore full VFO state on long press EXIT + VFO_RestoreBackup(); - // Restore full VFO state on long press EXIT - if (gHasVfoBackup) { - const uint8_t Vfo = gEeprom.TX_VFO; - - // Restore indices - gEeprom.ScreenChannel[Vfo] = gScreenChannelBackup; - gEeprom.FreqChannel[Vfo] = gFreqChannelBackup; - - // Restore full VFO - memcpy(gTxVfo, &gVfoBackup, sizeof(VFO_Info_t)); - - // Save and apply - SETTINGS_SaveVfoIndices(); - RADIO_ConfigureSquelchAndOutputPower(gTxVfo); - RADIO_SetupRegisters(true); - - gHasVfoBackup = false; - } - - gDTMF_InputMode = false; - gDTMF_InputBox_Index = 0; - memset(gDTMF_String, 0, sizeof(gDTMF_String)); - gInputBoxIndex = 0; - gRequestDisplayScreen = DISPLAY_MAIN; - gBeepToPlay = BEEP_1KHZ_60MS_OPTIONAL; - } + gRequestDisplayScreen = DISPLAY_MAIN; } return; @@ -726,23 +721,8 @@ static void MAIN_Key_EXIT(bool bKeyPressed, bool bKeyHeld) gInputBox[--gInputBoxIndex] = 10; // Restore full VFO state when back to 0 - if (gInputBoxIndex == 0 && gHasVfoBackup) { - const uint8_t Vfo = gEeprom.TX_VFO; - - // Restore indices - gEeprom.ScreenChannel[Vfo] = gScreenChannelBackup; - gEeprom.FreqChannel[Vfo] = gFreqChannelBackup; - - // Restore full VFO - memcpy(gTxVfo, &gVfoBackup, sizeof(VFO_Info_t)); - - // Save and apply - SETTINGS_SaveVfoIndices(); - RADIO_ConfigureSquelchAndOutputPower(gTxVfo); - RADIO_SetupRegisters(true); - - gHasVfoBackup = false; - } + if (gInputBoxIndex == 0) + VFO_RestoreBackup(); gKeyInputCountdown = key_input_timeout_500ms; channelMoveSwitch(); @@ -967,8 +947,10 @@ static void MAIN_Key_UP_DOWN(bool bKeyPressed, bool bKeyHeld, int8_t Direction) uint16_t Channel = gEeprom.ScreenChannel[gEeprom.TX_VFO]; if (bKeyHeld || !bKeyPressed) { // key held or released - if (gInputBoxIndex > 0) + if (gInputBoxIndex > 0) { gInputBoxIndex = 0; + gHasVfoBackup = false; + } if (!bKeyPressed) { if (!bKeyHeld || IS_FREQ_CHANNEL(Channel)) diff --git a/App/misc.c b/App/misc.c index 372d3708..29a2d376 100644 --- a/App/misc.c +++ b/App/misc.c @@ -242,6 +242,7 @@ volatile uint16_t gScanPauseDelayIn_10ms; uint16_t gMenuCountdown; bool gPttWasReleased; bool gPttWasPressed; +bool gHasVfoBackup; uint8_t gKeypadLocked; bool gFlagReconfigureVfos; uint8_t gVfoConfigureMode; diff --git a/App/misc.h b/App/misc.h index 7b1df678..77a0bbbe 100644 --- a/App/misc.h +++ b/App/misc.h @@ -368,6 +368,7 @@ extern AlarmState_t gAlarmState; extern uint16_t gMenuCountdown; extern bool gPttWasReleased; extern bool gPttWasPressed; +extern bool gHasVfoBackup; extern bool gFlagReconfigureVfos; extern uint8_t gVfoConfigureMode; extern bool gFlagResetVfos;