Skip to content

Commit

Permalink
Simplify backlight logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JuantAldea authored and egzumer committed Dec 6, 2023
1 parent 88f7b90 commit bafe372
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 41 deletions.
24 changes: 13 additions & 11 deletions app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "am_fix.h"
#include "app/action.h"

#ifdef ENABLE_AIRCOPY
#include "app/aircopy.h"
#endif
Expand Down Expand Up @@ -436,8 +437,9 @@ void APP_StartListening(FUNCTION_Type_t function)
AUDIO_AudioPathOn();
gEnableSpeaker = true;

if (gSetting_backlight_on_tx_rx >= BACKLIGHT_ON_TR_RX)
if (gSetting_backlight_on_tx_rx != BACKLIGHT_ON_TR_OFF) {
BACKLIGHT_TurnOn();
}

if (gScanStateDir != SCAN_OFF)
CHFRSCANNER_Found();
Expand Down Expand Up @@ -1360,15 +1362,16 @@ void APP_TimeSlice500ms(void)
}
#endif

if (gBacklightCountdown > 0 &&
!gAskToSave &&
!gCssBackgroundScan &&
if (gBacklightCountdown_500ms > 0 && !gAskToSave && !gCssBackgroundScan &&
// don't turn off backlight if user is in backlight menu option
!(gScreenToDisplay == DISPLAY_MENU && (UI_MENU_GetCurrentMenuId() == MENU_ABR || UI_MENU_GetCurrentMenuId() == MENU_ABR_MAX))
)
{ if (--gBacklightCountdown == 0)
if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) // backlight is not set to be always on
BACKLIGHT_TurnOff(); // turn backlight off
!(gScreenToDisplay == DISPLAY_MENU && (UI_MENU_GetCurrentMenuId() == MENU_ABR || UI_MENU_GetCurrentMenuId() == MENU_ABR_MAX)))
{
if (--gBacklightCountdown_500ms == 0) {
if (gEeprom.BACKLIGHT_TIME < (ARRAY_SIZE(gSubMenu_BACKLIGHT) - 1)) {
// backlight is not set to be always on
BACKLIGHT_TurnOff();
}
}
}

if (gSerialConfigCountDown_500ms > 0)
Expand Down Expand Up @@ -1641,10 +1644,9 @@ static void ProcessKey(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
}
else // key pressed or held
{
const uint8_t s = gSetting_backlight_on_tx_rx;
const int m = UI_MENU_GetCurrentMenuId();
if ( //not when PTT and the backlight shouldn't turn on on TX
!(Key == KEY_PTT && s != BACKLIGHT_ON_TR_TX && s != BACKLIGHT_ON_TR_TXRX)
!(Key == KEY_PTT && !(gSetting_backlight_on_tx_rx & BACKLIGHT_ON_TR_TX))
// not in the backlight menu
&& !(gScreenToDisplay == DISPLAY_MENU && ( m == MENU_ABR || m == MENU_ABR_MAX || m == MENU_ABR_MIN))
)
Expand Down
34 changes: 11 additions & 23 deletions driver/backlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "settings.h"

// this is decremented once every 500ms
uint16_t gBacklightCountdown = 0;
uint16_t gBacklightCountdown_500ms = 0;
bool backlightOn;

void BACKLIGHT_InitHardware()
Expand Down Expand Up @@ -54,42 +54,30 @@ void BACKLIGHT_InitHardware()

void BACKLIGHT_TurnOn(void)
{
if (gEeprom.BACKLIGHT_TIME != 0) {
backlightOn = true;
BACKLIGHT_SetBrightness(gEeprom.BACKLIGHT_MAX);
}
else {
if (gEeprom.BACKLIGHT_TIME == 0) {
BACKLIGHT_TurnOff();
return;
}

switch (gEeprom.BACKLIGHT_TIME)
{

backlightOn = true;
BACKLIGHT_SetBrightness(gEeprom.BACKLIGHT_MAX);

switch (gEeprom.BACKLIGHT_TIME) {
default:
case 1: // 5 sec
gBacklightCountdown = 5;
break;
case 2: // 10 sec
gBacklightCountdown = 10;
break;
case 3: // 20 sec
gBacklightCountdown = 20;
gBacklightCountdown_500ms = 1 + (2 << (gEeprom.BACKLIGHT_TIME - 1)) * 10;
break;
case 4: // 1 min
gBacklightCountdown = 60;
break;
case 5: // 2 min
gBacklightCountdown = 60 * 2;
break;
case 6: // 4 min
gBacklightCountdown = 60 * 4;
gBacklightCountdown_500ms = 1 + (2 << (gEeprom.BACKLIGHT_TIME - 4)) * 120;
break;
case 7: // always on
gBacklightCountdown = 0;
gBacklightCountdown_500ms = 0;
break;
}

gBacklightCountdown *= 2;
}

void BACKLIGHT_TurnOff()
Expand All @@ -106,7 +94,7 @@ void BACKLIGHT_TurnOff()
#else
BACKLIGHT_SetBrightness(gEeprom.BACKLIGHT_MIN);
#endif
gBacklightCountdown = 0;
gBacklightCountdown_500ms = 0;
backlightOn = false;
}

Expand Down
3 changes: 1 addition & 2 deletions driver/backlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <stdint.h>
#include <stdbool.h>

extern uint16_t gBacklightCountdown;
extern uint16_t gBacklightCountdown_500ms;
extern uint8_t gBacklightBrightness;

#ifdef ENABLE_BLMIN_TMP_OFF
Expand All @@ -38,4 +38,3 @@ bool BACKLIGHT_IsOn();
void BACKLIGHT_SetBrightness(uint8_t brigtness);

#endif

4 changes: 2 additions & 2 deletions functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ void FUNCTION_Select(FUNCTION_Type_t Function)
else
BK4819_DisableScramble();

if (gSetting_backlight_on_tx_rx == BACKLIGHT_ON_TR_TX ||
gSetting_backlight_on_tx_rx == BACKLIGHT_ON_TR_TXRX)
if (gSetting_backlight_on_tx_rx & BACKLIGHT_ON_TR_TX) {
BACKLIGHT_TurnOn();
}

break;

Expand Down
2 changes: 1 addition & 1 deletion misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ bool gSetting_350EN;
uint8_t gSetting_F_LOCK;
bool gSetting_ScrambleEnable;

uint8_t gSetting_backlight_on_tx_rx;
enum BacklightOnRxTx_t gSetting_backlight_on_tx_rx;

#ifdef ENABLE_AM_FIX
bool gSetting_AM_fix;
Expand Down
3 changes: 1 addition & 2 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ extern bool gSetting_350EN;
extern uint8_t gSetting_F_LOCK;
extern bool gSetting_ScrambleEnable;

extern uint8_t gSetting_backlight_on_tx_rx;
extern enum BacklightOnRxTx_t gSetting_backlight_on_tx_rx;

#ifdef ENABLE_AM_FIX
extern bool gSetting_AM_fix;
Expand Down Expand Up @@ -338,4 +338,3 @@ int32_t NUMBER_AddWithWraparound(int32_t Base, int32_t Add, int32_t LowerLimit,
unsigned long StrToUL(const char * str);

#endif

0 comments on commit bafe372

Please sign in to comment.