Refactoring #285

Signed-off-by: Krzysiek Egzmont <egzumer@gmail.com>
This commit is contained in:
Juan Antonio
2023-12-16 18:34:05 +01:00
committed by Krzysiek Egzmont
parent e7a21c470f
commit 2a75f8a9be
6 changed files with 213 additions and 215 deletions

View File

@@ -1135,6 +1135,7 @@ static void CheckKeys(void)
void APP_TimeSlice10ms(void) void APP_TimeSlice10ms(void)
{ {
gNextTimeslice = false;
gFlashLightBlinkCounter++; gFlashLightBlinkCounter++;
#ifdef ENABLE_BOOT_BEEPS #ifdef ENABLE_BOOT_BEEPS
@@ -1317,6 +1318,7 @@ void cancelUserInputModes(void)
// this is called once every 500ms // this is called once every 500ms
void APP_TimeSlice500ms(void) void APP_TimeSlice500ms(void)
{ {
gNextTimeslice_500ms = false;
bool exit_menu = false; bool exit_menu = false;
// Skipped authentic device check // Skipped authentic device check
@@ -1433,25 +1435,24 @@ void APP_TimeSlice500ms(void)
#endif #endif
} }
#ifdef ENABLE_FMRADIO if (!gCssBackgroundScan
if ((gFM_ScanState == FM_SCAN_OFF || gAskToSave) && !gCssBackgroundScan) #ifdef ENABLE_FMRADIO
#else && (gFM_ScanState == FM_SCAN_OFF || gAskToSave)
if (!gCssBackgroundScan) #endif
#endif ) {
{ if (gScanStateDir == SCAN_OFF && !SCANNER_IsScanning()
#ifdef ENABLE_AIRCOPY #ifdef ENABLE_AIRCOPY
if (gScanStateDir == SCAN_OFF && gScreenToDisplay != DISPLAY_AIRCOPY && !SCANNER_IsScanning()) && gScreenToDisplay != DISPLAY_AIRCOPY
#else #endif
if (gScanStateDir == SCAN_OFF && !SCANNER_IsScanning()) ) {
#endif if (gEeprom.AUTO_KEYPAD_LOCK && gKeyLockCountdown > 0 && !gDTMF_InputMode && gScreenToDisplay != DISPLAY_MENU) {
{ if (--gKeyLockCountdown == 0) {
if (gEeprom.AUTO_KEYPAD_LOCK && gKeyLockCountdown > 0 && !gDTMF_InputMode && gScreenToDisplay != DISPLAY_MENU)
{
if (--gKeyLockCountdown == 0)
gEeprom.KEY_LOCK = true; // lock the keyboard gEeprom.KEY_LOCK = true; // lock the keyboard
gUpdateStatus = true; // lock symbol needs showing gUpdateStatus = true; // lock symbol needs showing
} }
}
if (exit_menu) if (exit_menu)
{ {
gMenuCountdown = 0; gMenuCountdown = 0;
@@ -1485,24 +1486,24 @@ void APP_TimeSlice500ms(void)
gUpdateStatus = true; gUpdateStatus = true;
gUpdateDisplay = true; gUpdateDisplay = true;
{
GUI_DisplayType_t disp = DISPLAY_INVALID; GUI_DisplayType_t disp = DISPLAY_INVALID;
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (gFmRadioMode && ! FUNCTION_IsRx()) { if (gFmRadioMode && ! FUNCTION_IsRx()) {
disp = DISPLAY_FM; disp = DISPLAY_FM;
} }
#endif #endif
if (disp == DISPLAY_INVALID) if (disp == DISPLAY_INVALID) {
{
#ifndef ENABLE_NO_CODE_SCAN_TIMEOUT #ifndef ENABLE_NO_CODE_SCAN_TIMEOUT
if (!SCANNER_IsScanning()) if (!SCANNER_IsScanning())
#endif #endif
{
disp = DISPLAY_MAIN; disp = DISPLAY_MAIN;
} }
}
if (disp != DISPLAY_INVALID) if (disp != DISPLAY_INVALID) {
GUI_SelectNextDisplay(disp); GUI_SelectNextDisplay(disp);
} }
} }
@@ -1781,11 +1782,11 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
gUpdateStatus = true; gUpdateStatus = true;
} }
if (!bFlag) if (bFlag) {
{ goto Skip;
if (gCurrentFunction == FUNCTION_TRANSMIT) }
{ // transmitting
if (gCurrentFunction == FUNCTION_TRANSMIT) {
#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) #if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
if (gAlarmState == ALARM_STATE_OFF) if (gAlarmState == ALARM_STATE_OFF)
#endif #endif
@@ -1804,6 +1805,7 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
} }
else else
{ {
Code = DTMF_GetCharacter(Key - KEY_0); Code = DTMF_GetCharacter(Key - KEY_0);
if (Code == 0xFF) if (Code == 0xFF)
goto Skip; goto Skip;
@@ -1844,8 +1846,7 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
} }
} }
#if defined(ENABLE_ALARM) || defined(ENABLE_TX1750) #if defined(ENABLE_ALARM) || defined(ENABLE_TX1750)
else if ((!bKeyHeld && bKeyPressed) || (gAlarmState == ALARM_STATE_TX1750 && bKeyHeld && !bKeyPressed)) else if ((!bKeyHeld && bKeyPressed) || (gAlarmState == ALARM_STATE_TX1750 && bKeyHeld && !bKeyPressed)) {
{
ALARM_Off(); ALARM_Off();
if (gEeprom.REPEATER_TAIL_TONE_ELIMINATION == 0) if (gEeprom.REPEATER_TAIL_TONE_ELIMINATION == 0)
@@ -1864,16 +1865,14 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
else if (Key != KEY_SIDE1 && Key != KEY_SIDE2 && gScreenToDisplay != DISPLAY_INVALID) { else if (Key != KEY_SIDE1 && Key != KEY_SIDE2 && gScreenToDisplay != DISPLAY_INVALID) {
ProcessKeysFunctions[gScreenToDisplay](Key, bKeyPressed, bKeyHeld); ProcessKeysFunctions[gScreenToDisplay](Key, bKeyPressed, bKeyHeld);
} }
else else if (!SCANNER_IsScanning()
#ifdef ENABLE_AIRCOPY #ifdef ENABLE_AIRCOPY
if (!SCANNER_IsScanning() && gScreenToDisplay != DISPLAY_AIRCOPY) && gScreenToDisplay != DISPLAY_AIRCOPY
#else
if (!SCANNER_IsScanning())
#endif #endif
{ ) {
ACTION_Handle(Key, bKeyPressed, bKeyHeld); ACTION_Handle(Key, bKeyPressed, bKeyHeld);
} }
else if (!bKeyHeld && bKeyPressed) else if (!bKeyHeld && bKeyPressed) {
gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL; gBeepToPlay = BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL;
} }
@@ -1904,24 +1903,24 @@ Skip:
gUpdateStatus = true; gUpdateStatus = true;
} }
#ifdef ENABLE_FMRADIO #ifdef ENABLE_FMRADIO
if (gRequestSaveFM) if (gRequestSaveFM)
{ {
gRequestSaveFM = false;
if (!bKeyHeld) if (!bKeyHeld)
SETTINGS_SaveFM(); SETTINGS_SaveFM();
else else
gFlagSaveFM = true; gFlagSaveFM = true;
gRequestSaveFM = false;
} }
#endif #endif
if (gRequestSaveVFO) if (gRequestSaveVFO)
{ {
gRequestSaveVFO = false;
if (!bKeyHeld) if (!bKeyHeld)
SETTINGS_SaveVfoIndices(); SETTINGS_SaveVfoIndices();
else else
gFlagSaveVfo = true; gFlagSaveVfo = true;
gRequestSaveVFO = false;
} }
if (gRequestSaveChannel > 0) // TODO: remove the gRequestSaveChannel, why use global variable for that?? if (gRequestSaveChannel > 0) // TODO: remove the gRequestSaveChannel, why use global variable for that??

View File

@@ -16,13 +16,15 @@
#include <stdio.h> // NULL #include <stdio.h> // NULL
#include "audio.h" #include "../audio.h"
#include "../bsp/dp32g030/gpio.h"
#include "../bsp/dp32g030/portcon.h"
#include "bk4819.h" #include "bk4819.h"
#include "bsp/dp32g030/gpio.h" #include "gpio.h"
#include "bsp/dp32g030/portcon.h" #include "system.h"
#include "driver/gpio.h" #include "systick.h"
#include "driver/system.h"
#include "driver/systick.h"
#ifndef ARRAY_SIZE #ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
@@ -127,7 +129,6 @@ static uint16_t BK4819_ReadU16(void)
PORTCON_PORTC_IE = (PORTCON_PORTC_IE & ~PORTCON_PORTC_IE_C2_MASK) | PORTCON_PORTC_IE_C2_BITS_ENABLE; PORTCON_PORTC_IE = (PORTCON_PORTC_IE & ~PORTCON_PORTC_IE_C2_MASK) | PORTCON_PORTC_IE_C2_BITS_ENABLE;
GPIOC->DIR = (GPIOC->DIR & ~GPIO_DIR_2_MASK) | GPIO_DIR_2_BITS_INPUT; GPIOC->DIR = (GPIOC->DIR & ~GPIO_DIR_2_MASK) | GPIO_DIR_2_BITS_INPUT;
SYSTICK_DelayUs(1); SYSTICK_DelayUs(1);
Value = 0; Value = 0;
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
{ {

View File

@@ -117,19 +117,13 @@ KEY_Code_t KEYBOARD_Poll(void)
GPIOA->DATA &= keyboard[j].set_to_zero_mask; GPIOA->DATA &= keyboard[j].set_to_zero_mask;
// Read all 4 GPIO pins at once .. with de-noise, max of 8 sample loops // Read all 4 GPIO pins at once .. with de-noise, max of 8 sample loops
for (i = 0, k = 0, reg = 0; i < 3 && k < 8; i++, k++) for (i = 0, k = 0, reg = 0; i < 3 && k < 8; i++, k++) {
{
uint16_t reg2;
SYSTICK_DelayUs(1); SYSTICK_DelayUs(1);
uint16_t reg2 = GPIOA->DATA;
reg2 = GPIOA->DATA; i *= reg == reg2;
if (reg != reg2)
{ // noise
reg = reg2; reg = reg2;
i = 0;
}
} }
if (i < 3) if (i < 3)
break; // noise is too bad break; // noise is too bad

View File

@@ -14,10 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
#include "bsp/dp32g030/pmu.h" #include "../bsp/dp32g030/pmu.h"
#include "bsp/dp32g030/syscon.h" #include "../bsp/dp32g030/syscon.h"
#include "driver/system.h" #include "system.h"
#include "driver/systick.h" #include "systick.h"
void SYSTEM_DelayMs(uint32_t Delay) void SYSTEM_DelayMs(uint32_t Delay)
{ {

View File

@@ -15,8 +15,8 @@
*/ */
#include "ARMCM0.h" #include "ARMCM0.h"
#include "driver/systick.h" #include "systick.h"
#include "misc.h" #include "../misc.h"
// 0x20000324 // 0x20000324
static uint32_t gTickMultiplier; static uint32_t gTickMultiplier;
@@ -30,16 +30,20 @@ void SYSTICK_Init(void)
void SYSTICK_DelayUs(uint32_t Delay) void SYSTICK_DelayUs(uint32_t Delay)
{ {
const uint32_t ticks = Delay * gTickMultiplier; const uint32_t ticks = Delay * gTickMultiplier;
uint32_t i = 0; uint32_t elapsed_ticks = 0;
uint32_t Start = SysTick->LOAD; uint32_t Start = SysTick->LOAD;
uint32_t Previous = SysTick->VAL; uint32_t Previous = SysTick->VAL;
do { do {
uint32_t Current; uint32_t Current;
uint32_t Delta;
while ((Current = SysTick->VAL) == Previous) {}
Delta = (Current < Previous) ? -Current : Start - Current;
i += Delta + Previous;
Previous = Current;
} while (i < ticks);
}
do {
Current = SysTick->VAL;
} while (Current == Previous);
uint32_t Delta = ((Current < Previous) ? - Current : Start - Current);
elapsed_ticks += Delta + Previous;
Previous = Current;
} while (elapsed_ticks < ticks);
}

42
main.c
View File

@@ -14,18 +14,26 @@
* limitations under the License. * limitations under the License.
*/ */
#include <stdint.h>
#include <string.h> #include <string.h>
#include <stdio.h> // NULL #include <stdio.h> // NULL
#ifdef ENABLE_AM_FIX #ifdef ENABLE_AM_FIX
#include "am_fix.h" #include "am_fix.h"
#endif #endif
#include "audio.h"
#include "board.h"
#include "misc.h"
#include "radio.h"
#include "settings.h"
#include "version.h"
#include "app/app.h" #include "app/app.h"
#include "app/dtmf.h" #include "app/dtmf.h"
#include "audio.h"
#include "bsp/dp32g030/gpio.h" #include "bsp/dp32g030/gpio.h"
#include "bsp/dp32g030/syscon.h" #include "bsp/dp32g030/syscon.h"
#include "board.h"
#include "driver/backlight.h" #include "driver/backlight.h"
#include "driver/bk4819.h" #include "driver/bk4819.h"
#include "driver/gpio.h" #include "driver/gpio.h"
@@ -34,16 +42,13 @@
#ifdef ENABLE_UART #ifdef ENABLE_UART
#include "driver/uart.h" #include "driver/uart.h"
#endif #endif
#include "helper/battery.h" #include "helper/battery.h"
#include "helper/boot.h" #include "helper/boot.h"
#include "misc.h"
#include "radio.h"
#include "settings.h"
#include "ui/lock.h" #include "ui/lock.h"
#include "ui/welcome.h" #include "ui/welcome.h"
#include "ui/menu.h" #include "ui/menu.h"
#include "version.h"
void _putchar(__attribute__((unused)) char c) void _putchar(__attribute__((unused)) char c)
{ {
@@ -67,6 +72,7 @@ void Main(void)
| SYSCON_DEV_CLK_GATE_AES_BITS_ENABLE | SYSCON_DEV_CLK_GATE_AES_BITS_ENABLE
| SYSCON_DEV_CLK_GATE_PWM_PLUS0_BITS_ENABLE; | SYSCON_DEV_CLK_GATE_PWM_PLUS0_BITS_ENABLE;
SYSTICK_Init(); SYSTICK_Init();
BOARD_Init(); BOARD_Init();
@@ -102,9 +108,9 @@ void Main(void)
BATTERY_GetReadings(false); BATTERY_GetReadings(false);
#ifdef ENABLE_AM_FIX #ifdef ENABLE_AM_FIX
AM_fix_init(); AM_fix_init();
#endif #endif
const BOOT_Mode_t BootMode = BOOT_GetMode(); const BOOT_Mode_t BootMode = BOOT_GetMode();
@@ -211,24 +217,18 @@ void Main(void)
#ifdef ENABLE_NOAA #ifdef ENABLE_NOAA
RADIO_ConfigureNOAA(); RADIO_ConfigureNOAA();
#endif #endif
// ******************
} }
while (1) while (true) {
{
APP_Update(); APP_Update();
if (gNextTimeslice) if (gNextTimeslice) {
{
APP_TimeSlice10ms();
gNextTimeslice = false;
}
if (gNextTimeslice_500ms) APP_TimeSlice10ms();
{
if (gNextTimeslice_500ms) {
APP_TimeSlice500ms(); APP_TimeSlice500ms();
gNextTimeslice_500ms = false; }
} }
} }
} }