Skip to content

Commit

Permalink
Merge pull request #825 from UltimateHackingKeyboard/fix_light_all_exit
Browse files Browse the repository at this point in the history
Turn off all led slots again when the light all mode is exited
  • Loading branch information
mondalaci authored Dec 11, 2024
2 parents 0c1afcb + f69ccfa commit 39b5b55
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions doc-dev/reference-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ COMMAND = clearStatus
COMMAND = set setEmergencyKey KEYID
COMMAND = validateUserConfig
COMMAND = resetConfiguration
COMMAND = set leds.alwaysOn BOOL
##############
# DEPRECATED #
##############
Expand Down
1 change: 1 addition & 0 deletions right/src/config_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ const config_t DefaultCfg = (config_t){
.KeyBacklightFadeOutTimeout = 0,
.KeyBacklightFadeOutBatteryTimeout = 60000,
.LedsEnabled = true,
.LedsAlwaysOn = false,
.LedBrightnessMultiplier = 1.0f,
.LayerConfig = {
{ .layerIsDefined = true, .exactModifierMatch = false, .modifierLayerMask = 0},
Expand Down
1 change: 1 addition & 0 deletions right/src/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
uint32_t KeyBacklightFadeOutBatteryTimeout;

bool LedsEnabled;
bool LedsAlwaysOn;

// layers
layer_config_t LayerConfig[LayerId_Count];
Expand Down
1 change: 0 additions & 1 deletion right/src/config_parser/parse_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ parser_error_t parseConfig(config_buffer_t *buffer)
Cfg.KeyBacklightFadeOutTimeout = keyBacklightFadeOutTimeout;
Cfg.KeyBacklightFadeOutBatteryTimeout = keyBacklightFadeOutBatteryTimeout;

AlwaysOnMode = false;
LedManager_RecalculateLedBrightness();
LedManager_UpdateSleepModes();
BtPair_ClearUnknownBonds();
Expand Down
12 changes: 8 additions & 4 deletions right/src/led_manager.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "led_manager.h"
#include "event_scheduler.h"
#include "ledmap.h"
#include "usb_composite_device.h"
#include "config_manager.h"
#include "stubs.h"
Expand All @@ -23,25 +24,28 @@ bool DisplaySleepModeActive = false;
uint8_t DisplayBrightness = 0xff;
uint8_t KeyBacklightBrightness = 0xff;

bool AlwaysOnMode = false;

static void recalculateLedBrightness()
{
bool globalSleepMode = !Cfg.LedsEnabled || CurrentPowerMode > PowerMode_Awake || Cfg.LedBrightnessMultiplier == 0.0f;
bool globalAlwaysOn = Cfg.LedsAlwaysOn || Ledmap_AlwaysOn;

if (!AlwaysOnMode && (globalSleepMode || KeyBacklightSleepModeActive)) {
if (!globalAlwaysOn && (globalSleepMode || KeyBacklightSleepModeActive)) {
KeyBacklightBrightness = 0;
} else {
uint8_t keyBacklightBrightnessBase = RunningOnBattery ? Cfg.KeyBacklightBrightnessBatteryDefault : Cfg.KeyBacklightBrightnessDefault;
KeyBacklightBrightness = MIN(255, keyBacklightBrightnessBase * Cfg.LedBrightnessMultiplier);
}

if (!AlwaysOnMode && (globalSleepMode || DisplaySleepModeActive)) {
if (!globalAlwaysOn && (globalSleepMode || DisplaySleepModeActive)) {
DisplayBrightness = 0;
} else {
uint8_t displayBrightnessBase = RunningOnBattery ? Cfg.DisplayBrightnessBatteryDefault : Cfg.DisplayBrightnessDefault;
DisplayBrightness = MIN(255, displayBrightnessBase * Cfg.LedBrightnessMultiplier);
}

if (Ledmap_AlwaysOn) {
KeyBacklightBrightness = 255;
}
}

void LedManager_FullUpdate()
Expand Down
43 changes: 30 additions & 13 deletions right/src/ledmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "layer.h"
#include "layer_switcher.h"
#include "ledmap.h"
#include "led_manager.h"
#include "slave_drivers/is31fl3xxx_driver.h"
#include "config_parser/config_globals.h"
#include "debug.h"
Expand All @@ -25,6 +26,7 @@
static const rgb_t black = RGB(0x00, 0x00, 0x00);
static const rgb_t white = RGB(0xff, 0xff, 0xff);

bool Ledmap_AlwaysOn = false;
backlighting_mode_t TemporaryBacklightingMode = BacklightingMode_Unspecified;

typedef enum {
Expand Down Expand Up @@ -576,6 +578,22 @@ static void updateLedsByPerKeyKeyStragegy() {
}
}

static void setEntireMatrix(uint8_t v) {
#ifdef __ZEPHYR__
#if DEVICE_IS_UHK80_LEFT || DEVICE_IS_UHK80_RIGHT
for (uint8_t i = 0; i < UHK80_LED_DRIVER_LED_COUNT_MAX; i++) {
Uhk80LedDriverValues[i] = v;
}
#endif
#else
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
for (uint8_t i=0; i<LED_DRIVER_LED_COUNT_MAX; i++) {
LedDriverValues[slotId][i] = v;
}
}
#endif
}

static void setAllTo(const rgb_t* color) {
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
color_mode_t colorMode = determineMode(slotId);
Expand All @@ -591,20 +609,9 @@ static void updateLedsByLedTestStragegy() {
}
}


static void updateLedsByLightAllStragegy() {
#ifdef __ZEPHYR__
#if DEVICE_IS_UHK80_LEFT || DEVICE_IS_UHK80_RIGHT
for (uint8_t i = 0; i < UHK80_LED_DRIVER_LED_COUNT_MAX; i++) {
Uhk80LedDriverValues[i] = 255;
}
#endif
#else
for (uint8_t slotId=0; slotId<SLOT_COUNT; slotId++) {
for (uint8_t i=0; i<LED_DRIVER_LED_COUNT_MAX; i++) {
LedDriverValues[slotId][i] = 255;
}
}
#endif
setEntireMatrix(255);
}

void Ledmap_ActivateTestled(uint8_t slotId, uint8_t keyId) {
Expand All @@ -630,6 +637,9 @@ backlighting_mode_t Ledmap_GetEffectiveBacklightMode() {
}

void handleModeChange(backlighting_mode_t from, backlighting_mode_t to) {
if (from == BacklightingMode_LightAll) {
setEntireMatrix(0);
}
if (to == BacklightingMode_LedTest) {
backlightingLedTestModeState = BacklightingLedTestModeState_All;
backlightingLedTestStart = CurrentTime;
Expand Down Expand Up @@ -735,8 +745,14 @@ void Ledmap_InitLedLayout(void) {
#endif
}

static void updateAlwaysOn() {
backlighting_mode_t mode = Ledmap_GetEffectiveBacklightMode();
Ledmap_AlwaysOn = mode == BacklightingMode_LightAll || mode == BacklightingMode_LedTest;
}

void Ledmap_SetTemporaryLedBacklightingMode(backlighting_mode_t newMode) {
TemporaryBacklightingMode = newMode;
updateAlwaysOn();
#ifdef __ZEPHYR__
StateSync_UpdateProperty(StateSyncPropertyId_Backlight, NULL);
#endif
Expand All @@ -752,6 +768,7 @@ void Ledmap_ResetTemporaryLedBacklightingMode() {
void Ledmap_SetLedBacklightingMode(backlighting_mode_t newMode)
{
Cfg.BacklightingMode = newMode;
updateAlwaysOn();
#ifdef __ZEPHYR__
StateSync_UpdateProperty(StateSyncPropertyId_Backlight, NULL);
#endif
Expand Down
1 change: 1 addition & 0 deletions right/src/ledmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
// Variables:

extern bool Ledmap_LedTestActive;
extern bool Ledmap_AlwaysOn;

// Functions:

Expand Down
5 changes: 4 additions & 1 deletion right/src/macros/set_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ static macro_variable_t leds(parser_context_t* ctx, set_command_action_t action)
ASSIGN_FLOAT(Cfg.LedBrightnessMultiplier);
} else if (ConsumeToken(ctx, "enabled")) {
ASSIGN_BOOL(Cfg.LedsEnabled);
} else {
} else if (ConsumeToken(ctx, "alwaysOn")) {
ASSIGN_BOOL(Cfg.LedsAlwaysOn);
}
else {
Macros_ReportError("Parameter not recognized:", ctx->at, ctx->end);
}

Expand Down
3 changes: 0 additions & 3 deletions right/src/usb_commands/usb_command_apply_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ void UsbCommand_ApplyConfigAsync(void) {
static void setLedsWhite() {
// Set the led test backlight mode, but don't activate the switch test mode.
Ledmap_SetLedBacklightingMode(BacklightingMode_LightAll);
AlwaysOnMode = true;
Cfg.KeyBacklightBrightnessDefault = 255;
Cfg.KeyBacklightBrightnessBatteryDefault = 255;
EventVector_Set(EventVector_LedMapUpdateNeeded);
}

Expand Down

0 comments on commit 39b5b55

Please sign in to comment.