From 4322a7d8a960c84aba310517b77e1d1937dc0c7c Mon Sep 17 00:00:00 2001 From: Juan Antonio Date: Fri, 8 Dec 2023 16:51:50 +0100 Subject: [PATCH] Drop gScreenToDisplay switch and use function table Saves some bytes --- app/app.c | 50 ++++----- functions.c | 297 +++++++++++++++++++++++++++------------------------- ui/ui.c | 49 ++++----- ui/ui.h | 10 +- 4 files changed, 204 insertions(+), 202 deletions(-) diff --git a/app/app.c b/app/app.c index 3e38955..6c66483 100644 --- a/app/app.c +++ b/app/app.c @@ -14,6 +14,8 @@ * limitations under the License. */ +#include +#include #include #include "am_fix.h" @@ -72,6 +74,23 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld); +void (*ProcessKeysFunctions[])(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) = { + [DISPLAY_MAIN] = &MAIN_ProcessKeys, + [DISPLAY_MENU] = &MENU_ProcessKeys, + [DISPLAY_SCANNER] = &SCANNER_ProcessKeys, + +#ifdef ENABLE_FMRADIO + [DISPLAY_FM] = &FM_ProcessKeys, +#endif + +#ifdef ENABLE_AIRCOPY + [DISPLAY_AIRCOPY] = &AIRCOPY_ProcessKeys, +#endif +}; + +static_assert(ARRAY_SIZE(ProcessKeysFunctions) == DISPLAY_N_ELEM); + + static void CheckForIncoming(void) { @@ -1580,8 +1599,6 @@ static void ALARM_Off(void) } #endif - - static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) { if (Key == KEY_EXIT && !BACKLIGHT_IsOn() && gEeprom.BACKLIGHT_TIME > 0) @@ -1849,33 +1866,8 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) } #endif } - else if (Key != KEY_SIDE1 && Key != KEY_SIDE2) { - switch (gScreenToDisplay) { - case DISPLAY_MAIN: - MAIN_ProcessKeys(Key, bKeyPressed, bKeyHeld); - break; -#ifdef ENABLE_FMRADIO - case DISPLAY_FM: - FM_ProcessKeys(Key, bKeyPressed, bKeyHeld); - break; -#endif - case DISPLAY_MENU: - MENU_ProcessKeys(Key, bKeyPressed, bKeyHeld); - break; - - case DISPLAY_SCANNER: - SCANNER_ProcessKeys(Key, bKeyPressed, bKeyHeld); - break; - -#ifdef ENABLE_AIRCOPY - case DISPLAY_AIRCOPY: - AIRCOPY_ProcessKeys(Key, bKeyPressed, bKeyHeld); - break; -#endif - case DISPLAY_INVALID: - default: - break; - } + else if (Key != KEY_SIDE1 && Key != KEY_SIDE2 && gScreenToDisplay != DISPLAY_INVALID) { + ProcessKeysFunctions[gScreenToDisplay](Key, bKeyPressed, bKeyHeld); } else #ifdef ENABLE_AIRCOPY diff --git a/functions.c b/functions.c index 8ad703c..a6ca594 100644 --- a/functions.c +++ b/functions.c @@ -92,10 +92,150 @@ void FUNCTION_Init(void) gUpdateStatus = true; } +void FUNCTION_Foreground(const FUNCTION_Type_t PreviousFunction) +{ +#ifdef ENABLE_DTMF_CALLING + if (gDTMF_ReplyState != DTMF_REPLY_NONE) + RADIO_PrepareCssTX(); +#endif + if (PreviousFunction == FUNCTION_TRANSMIT) { + ST7565_FixInterfGlitch(); + gVFO_RSSI_bar_level[0] = 0; + gVFO_RSSI_bar_level[1] = 0; + } else if (PreviousFunction != FUNCTION_RECEIVE) { + return; + } + +#if defined(ENABLE_FMRADIO) + if (gFmRadioMode) + gFM_RestoreCountdown_10ms = fm_restore_countdown_10ms; +#endif + +#ifdef ENABLE_DTMF_CALLING + if (gDTMF_CallState == DTMF_CALL_STATE_CALL_OUT || + gDTMF_CallState == DTMF_CALL_STATE_RECEIVED || + gDTMF_CallState == DTMF_CALL_STATE_RECEIVED_STAY) + { + gDTMF_auto_reset_time_500ms = gEeprom.DTMF_auto_reset_time * 2; + } +#endif + gUpdateStatus = true; +} + +void FUNCTION_PowerSave() { + gPowerSave_10ms = gEeprom.BATTERY_SAVE * 10; + gPowerSaveCountdownExpired = false; + + gRxIdleMode = true; + + gMonitor = false; + + BK4819_DisableVox(); + BK4819_Sleep(); + + BK4819_ToggleGpioOut(BK4819_GPIO0_PIN28_RX_ENABLE, false); + + gUpdateStatus = true; + + if (gScreenToDisplay != DISPLAY_MENU) // 1of11 .. don't close the menu + GUI_SelectNextDisplay(DISPLAY_MAIN); +} + +void FUNCTION_Transmit() +{ + // if DTMF is enabled when TX'ing, it changes the TX audio filtering !! .. 1of11 + BK4819_DisableDTMF(); + +#ifdef ENABLE_DTMF_CALLING + // clear the DTMF RX buffer + DTMF_clear_RX(); +#endif + + // clear the DTMF RX live decoder buffer + gDTMF_RX_live_timeout = 0; + gDTMF_RX_live_timeout = 0; + memset(gDTMF_RX_live, 0, sizeof(gDTMF_RX_live)); + +#if defined(ENABLE_FMRADIO) + if (gFmRadioMode) + BK1080_Init(0, false); +#endif + +#ifdef ENABLE_ALARM + if (gAlarmState == ALARM_STATE_SITE_ALARM) + { + GUI_DisplayScreen(); + + AUDIO_AudioPathOff(); + + SYSTEM_DelayMs(20); + BK4819_PlayTone(500, 0); + SYSTEM_DelayMs(2); + + AUDIO_AudioPathOn(); + + gEnableSpeaker = true; + + SYSTEM_DelayMs(60); + BK4819_ExitTxMute(); + + gAlarmToneCounter = 0; + return; + } +#endif + + gUpdateStatus = true; + + GUI_DisplayScreen(); + + RADIO_SetTxParameters(); + + // turn the RED LED on + BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, true); + + DTMF_Reply(); + + if (gCurrentVfo->DTMF_PTT_ID_TX_MODE == PTT_ID_APOLLO) + BK4819_PlaySingleTone(2525, 250, 0, gEeprom.DTMF_SIDE_TONE); + +#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) + if (gAlarmState != ALARM_STATE_OFF) { + #ifdef ENABLE_TX1750 + if (gAlarmState == ALARM_STATE_TX1750) + BK4819_TransmitTone(true, 1750); + #endif + + #ifdef ENABLE_ALARM + if (gAlarmState == ALARM_STATE_TXALARM) + BK4819_TransmitTone(true, 500); + + gAlarmToneCounter = 0; + #endif + + SYSTEM_DelayMs(2); + AUDIO_AudioPathOn(); + gEnableSpeaker = true; + + return; + } +#endif + + if (gCurrentVfo->SCRAMBLING_TYPE > 0 && gSetting_ScrambleEnable) + BK4819_EnableScramble(gCurrentVfo->SCRAMBLING_TYPE - 1); + else + BK4819_DisableScramble(); + + if (gSetting_backlight_on_tx_rx & BACKLIGHT_ON_TR_TX) { + BACKLIGHT_TurnOn(); + } +} + + + void FUNCTION_Select(FUNCTION_Type_t Function) { const FUNCTION_Type_t PreviousFunction = gCurrentFunction; - const bool bWasPowerSave = (PreviousFunction == FUNCTION_POWER_SAVE); + const bool bWasPowerSave = PreviousFunction == FUNCTION_POWER_SAVE; gCurrentFunction = Function; @@ -109,154 +249,23 @@ void FUNCTION_Select(FUNCTION_Type_t Function) switch (Function) { case FUNCTION_FOREGROUND: -#ifdef ENABLE_DTMF_CALLING - if (gDTMF_ReplyState != DTMF_REPLY_NONE) - RADIO_PrepareCssTX(); -#endif - if (PreviousFunction == FUNCTION_TRANSMIT) - { - ST7565_FixInterfGlitch(); - gVFO_RSSI_bar_level[0] = 0; - gVFO_RSSI_bar_level[1] = 0; - } - else - if (PreviousFunction != FUNCTION_RECEIVE) - break; - - #if defined(ENABLE_FMRADIO) - if (gFmRadioMode) - gFM_RestoreCountdown_10ms = fm_restore_countdown_10ms; - #endif - -#ifdef ENABLE_DTMF_CALLING - if (gDTMF_CallState == DTMF_CALL_STATE_CALL_OUT || - gDTMF_CallState == DTMF_CALL_STATE_RECEIVED || - gDTMF_CallState == DTMF_CALL_STATE_RECEIVED_STAY) - { - gDTMF_auto_reset_time_500ms = gEeprom.DTMF_auto_reset_time * 2; - } -#endif - gUpdateStatus = true; + FUNCTION_Foreground(PreviousFunction); return; + case FUNCTION_POWER_SAVE: + FUNCTION_PowerSave(); + return; + + case FUNCTION_TRANSMIT: + FUNCTION_Transmit(); + break; + case FUNCTION_MONITOR: gMonitor = true; break; case FUNCTION_INCOMING: case FUNCTION_RECEIVE: - break; - - case FUNCTION_POWER_SAVE: - gPowerSave_10ms = gEeprom.BATTERY_SAVE * 10; - gPowerSaveCountdownExpired = false; - - gRxIdleMode = true; - - gMonitor = false; - - BK4819_DisableVox(); - BK4819_Sleep(); - - BK4819_ToggleGpioOut(BK4819_GPIO0_PIN28_RX_ENABLE, false); - - gUpdateStatus = true; - - if (gScreenToDisplay != DISPLAY_MENU) // 1of11 .. don't close the menu - GUI_SelectNextDisplay(DISPLAY_MAIN); - - return; - - case FUNCTION_TRANSMIT: - - // if DTMF is enabled when TX'ing, it changes the TX audio filtering !! .. 1of11 - BK4819_DisableDTMF(); - -#ifdef ENABLE_DTMF_CALLING - // clear the DTMF RX buffer - DTMF_clear_RX(); -#endif - - // clear the DTMF RX live decoder buffer - gDTMF_RX_live_timeout = 0; - gDTMF_RX_live_timeout = 0; - memset(gDTMF_RX_live, 0, sizeof(gDTMF_RX_live)); - - #if defined(ENABLE_FMRADIO) - if (gFmRadioMode) - BK1080_Init(0, false); - #endif - - #ifdef ENABLE_ALARM - if (gAlarmState == ALARM_STATE_SITE_ALARM) - { - GUI_DisplayScreen(); - - AUDIO_AudioPathOff(); - - SYSTEM_DelayMs(20); - BK4819_PlayTone(500, 0); - SYSTEM_DelayMs(2); - - AUDIO_AudioPathOn(); - - gEnableSpeaker = true; - - SYSTEM_DelayMs(60); - BK4819_ExitTxMute(); - - gAlarmToneCounter = 0; - break; - } - #endif - - gUpdateStatus = true; - - GUI_DisplayScreen(); - - RADIO_SetTxParameters(); - - // turn the RED LED on - BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, true); - - DTMF_Reply(); - - if (gCurrentVfo->DTMF_PTT_ID_TX_MODE == PTT_ID_APOLLO) - BK4819_PlaySingleTone(2525, 250, 0, gEeprom.DTMF_SIDE_TONE); - - #if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) - if (gAlarmState != ALARM_STATE_OFF) { - #ifdef ENABLE_TX1750 - if (gAlarmState == ALARM_STATE_TX1750) - BK4819_TransmitTone(true, 1750); - #endif - - #ifdef ENABLE_ALARM - if (gAlarmState == ALARM_STATE_TXALARM) - BK4819_TransmitTone(true, 500); - - gAlarmToneCounter = 0; - #endif - - SYSTEM_DelayMs(2); - AUDIO_AudioPathOn(); - gEnableSpeaker = true; - - break; - } - #endif - - if (gCurrentVfo->SCRAMBLING_TYPE > 0 && gSetting_ScrambleEnable) - BK4819_EnableScramble(gCurrentVfo->SCRAMBLING_TYPE - 1); - else - BK4819_DisableScramble(); - - if (gSetting_backlight_on_tx_rx & BACKLIGHT_ON_TR_TX) { - BACKLIGHT_TurnOn(); - } - - break; - case FUNCTION_BAND_SCOPE: break; } @@ -264,7 +273,7 @@ void FUNCTION_Select(FUNCTION_Type_t Function) gBatterySaveCountdown_10ms = battery_save_count_10ms; gSchedulePowerSave = false; - #if defined(ENABLE_FMRADIO) - gFM_RestoreCountdown_10ms = 0; - #endif +#if defined(ENABLE_FMRADIO) + gFM_RestoreCountdown_10ms = 0; +#endif } diff --git a/ui/ui.c b/ui/ui.c index 90efcc2..50963aa 100644 --- a/ui/ui.c +++ b/ui/ui.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include "app/chFrScanner.h" @@ -34,6 +35,7 @@ #include "ui/menu.h" #include "ui/scanner.h" #include "ui/ui.h" +#include "../misc.h" GUI_DisplayType_t gScreenToDisplay; GUI_DisplayType_t gRequestDisplayScreen = DISPLAY_INVALID; @@ -42,36 +44,27 @@ uint8_t gAskForConfirmation; bool gAskToSave; bool gAskToDelete; + +void (*UI_DisplayFunctions[])(void) = { + [DISPLAY_MAIN] = &UI_DisplayMain, + [DISPLAY_MENU] = &UI_DisplayMenu, + [DISPLAY_SCANNER] = &UI_DisplayScanner, + +#ifdef ENABLE_FMRADIO + [DISPLAY_FM] = &UI_DisplayFM, +#endif + +#ifdef ENABLE_AIRCOPY + [DISPLAY_AIRCOPY] = &UI_DisplayAircopy, +#endif +}; + +static_assert(ARRAY_SIZE(UI_DisplayFunctions) == DISPLAY_N_ELEM); + void GUI_DisplayScreen(void) { - switch (gScreenToDisplay) - { - case DISPLAY_MAIN: - UI_DisplayMain(); - break; - - #ifdef ENABLE_FMRADIO - case DISPLAY_FM: - UI_DisplayFM(); - break; - #endif - - case DISPLAY_MENU: - UI_DisplayMenu(); - break; - - case DISPLAY_SCANNER: - UI_DisplayScanner(); - break; - - #ifdef ENABLE_AIRCOPY - case DISPLAY_AIRCOPY: - UI_DisplayAircopy(); - break; - #endif - - default: - break; + if (gScreenToDisplay != DISPLAY_INVALID) { + UI_DisplayFunctions[gScreenToDisplay](); } } diff --git a/ui/ui.h b/ui/ui.h index 529e38d..97289ae 100644 --- a/ui/ui.h +++ b/ui/ui.h @@ -23,10 +23,18 @@ enum GUI_DisplayType_t { DISPLAY_MAIN = 0, - DISPLAY_FM, DISPLAY_MENU, DISPLAY_SCANNER, + +#ifdef ENABLE_FMRADIO + DISPLAY_FM, +#endif + +#ifdef ENABLE_AIRCOPY DISPLAY_AIRCOPY, +#endif + + DISPLAY_N_ELEM, DISPLAY_INVALID = 0xFFu };