Skip to content

Commit

Permalink
Improve key responsiveness in long loops
Browse files Browse the repository at this point in the history
  • Loading branch information
xawen committed Nov 20, 2023
1 parent 34f2e29 commit 13268fb
Showing 1 changed file with 101 additions and 80 deletions.
181 changes: 101 additions & 80 deletions app/spectrum.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,42 @@ uint8_t CurrentStepCountIndex;
uint8_t CurrentStepCount;
uint16_t CurrentScanDelay;
uint16_t RssiValue[128] = {0};
bool bExit = FALSE;
KEY_t Key;
KEY_t LastKey = KEY_NONE;

uint8_t bDebug = 0;

void DrawLabels(void){
uint32_t Divider = 10000000U;
uint8_t X = 30;
uint8_t Y = 65;

for (uint8_t i = 0; i < 8; i++) {
UI_DrawBigDigit(X, Y, (CurrentFreq / Divider) % 10U);
Divider /= 10;
if (i == 2) {
X += 16;
} else {
X += 12;
}
}

Int2Ascii(FreqMin, 8);
UI_DrawSmallString(2, 2, gShortString, 8);
Int2Ascii(FreqMax, 8);
UI_DrawSmallString(110, 2, gShortString, 8);

Int2Ascii(CurrentStepCount, 3);
UI_DrawSmallString(2, 74, gShortString, 3);

Int2Ascii(CurrentScanDelay, 2);
UI_DrawSmallString(142, 74, gShortString, 2);

//Int2Ascii(CurrentScanDelay, 2); //Use this posistion for scan step. Todo: convert scanstep from index to printable string.
//UI_DrawSmallString(2, 64, gShortString, 2);
}

void SetFreqMinMax(void){
uint32_t FreqDelta;

Expand Down Expand Up @@ -79,6 +112,12 @@ void IncrementStepIndex(void){
CurrentStepCountIndex = (CurrentStepCountIndex + 1) % STEPS_COUNT;
SetStepCount();
SetFreqMinMax();
DrawLabels();
}

void IncrementScanDelay(void){
CurrentScanDelay = (CurrentScanDelay + 10) % 100;
DrawLabels();
}

void DrawBars(){
Expand All @@ -99,30 +138,6 @@ void DrawBars(){
}
}

void DrawLabels(void){
uint32_t Divider = 10000000U;
uint8_t X = 30;
uint8_t Y = 65;

for (uint8_t i = 0; i < 8; i++) {
UI_DrawBigDigit(X, Y, (CurrentFreq / Divider) % 10U);
Divider /= 10;
if (i == 2) {
X += 16;
} else {
X += 12;
}
}

Int2Ascii(FreqMin, 8);
UI_DrawSmallString(2, 2, gShortString, 8);
Int2Ascii(FreqMax, 8);
UI_DrawSmallString(110, 2, gShortString, 8);

Int2Ascii(CurrentStepCount, 3);
UI_DrawSmallString(2, 74, gShortString, 3);
}

void StopSpectrum(void)
{
SCREEN_TurnOn();
Expand All @@ -139,14 +154,66 @@ void StopSpectrum(void)
UI_DrawMain(true);
}

void Spectrum_Loop(void){
void CheckKeys(void){
Key = KEY_GetButton();
if (Key != LastKey) {
switch (Key) {
case KEY_NONE:
break;
case KEY_EXIT:
StopSpectrum();
bExit = TRUE;
return;
break;
case KEY_MENU:
break;
case KEY_UP:
break;
case KEY_DOWN:
break;
case KEY_1:
IncrementStepIndex();
break;
case KEY_2:
break;
case KEY_3:
IncrementScanDelay();
break;
case KEY_4:
BK4819_ToggleAGCMode(0);
break;
case KEY_5:
BK4819_ToggleAGCMode(1);
break;
case KEY_6:
break;
case KEY_7:
break;
case KEY_8:
break;
case KEY_9:
break;
case KEY_0:
bDebug ^= 1;
break;
case KEY_HASH:
break;
case KEY_STAR:
break;
default:
break;
}

KEY_t Key;
KEY_t LastKey = KEY_NONE;
LastKey = Key;
}
}

void Spectrum_Loop(void){
uint32_t FreqToCheck;

DrawLabels();

while (1) {
//Do non-interactive stuff here
FreqToCheck = FreqMin;

// These are used in radio.c when the frequency is set. Some combination is needed in the loop.
Expand All @@ -170,7 +237,7 @@ void Spectrum_Loop(void){
BK4819_WriteRegister(0x30, 0xBFF1);
//BK4819_SetFilterBandwidth(false);
//BK4819_EnableFilter(true);
DELAY_WaitMS(50);
DELAY_WaitMS(CurrentScanDelay);

RssiValue[i] = BK4819_GetRSSI();

Expand All @@ -184,59 +251,13 @@ void Spectrum_Loop(void){
//------------------------end test

FreqToCheck += CurrentFreqStep;
}

DrawLabels();
DrawBars();
//----------------

Key = KEY_GetButton();
if (Key != LastKey) {
switch (Key) {
case KEY_NONE:
break;
case KEY_EXIT:
StopSpectrum();
return;
break;
case KEY_MENU:
break;
case KEY_UP:
break;
case KEY_DOWN:
break;
case KEY_1:
IncrementStepIndex();
break;
case KEY_2:
break;
case KEY_3:
break;
case KEY_4:
break;
case KEY_5:
break;
case KEY_6:
break;
case KEY_7:
break;
case KEY_8:
break;
case KEY_9:
break;
case KEY_0:
bDebug ^= 1;
break;
case KEY_HASH:
break;
case KEY_STAR:
break;
default:
break;
CheckKeys();
if (bExit){
return;
}

LastKey = Key;
}
DrawBars();
}
}

Expand All @@ -247,7 +268,7 @@ void APP_Spectrum(void){
CurrentFreqStepIndex = gSettings.FrequencyStep;
CurrentFreqStep = FREQUENCY_GetStep(CurrentFreqStepIndex);
CurrentStepCountIndex = STEPS_16;
CurrentScanDelay = 100;
CurrentScanDelay = 40;

SetStepCount();
SetFreqMinMax();
Expand Down

0 comments on commit 13268fb

Please sign in to comment.