Skip to content

Commit

Permalink
Merge pull request #1067 from UltimateHackingKeyboard/fade_rgb
Browse files Browse the repository at this point in the history
Fade uhk80 leds gradually.
  • Loading branch information
mondalaci authored Dec 27, 2024
2 parents ca6d687 + 930c4e5 commit eb12223
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions device/src/keyboard/leds.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ void setLedsCs(bool state)

static volatile bool ledsNeedUpdate = false;

static uint8_t currentScaling = 0;
static uint8_t scalingFadeStep = 5;
static uint8_t scalingFadeDelay = 10;

static void setOperationMode(bool on) {
// Set software shutdown control (SSD) register to normal mode
setLedsCs(true);
Expand All @@ -52,13 +56,36 @@ static void sleepLeds() {
}
}

static void recalculateScaling() {
if (currentScaling == KeyBacklightBrightness) {
return;
}

if (currentScaling < KeyBacklightBrightness) {
if (currentScaling < KeyBacklightBrightness && KeyBacklightBrightness - currentScaling >= scalingFadeStep) {
currentScaling += scalingFadeStep;
} else {
currentScaling = KeyBacklightBrightness;
}
}

if (currentScaling > KeyBacklightBrightness) {
if (currentScaling > KeyBacklightBrightness && currentScaling - KeyBacklightBrightness >= scalingFadeStep) {
currentScaling -= scalingFadeStep;
} else {
currentScaling = KeyBacklightBrightness;
}
}
}

void ledUpdater() {
k_sleep(K_MSEC(100));
while (true) {
k_mutex_lock(&SpiMutex, K_FOREVER);

setOperationMode(1);


// Set 180 degree phase delay to reduce audible noise, although it doesn't seem to make a difference
setLedsCs(true);
writeSpi(LedPagePrefix | 2);
Expand Down Expand Up @@ -91,7 +118,7 @@ void ledUpdater() {
writeSpi(LedPagePrefix | 1);
writeSpi(0x00);
for (int i=0; i<255; i++) {
writeSpi(KeyBacklightBrightness);
writeSpi(currentScaling);
}
setLedsCs(false);

Expand All @@ -101,11 +128,16 @@ void ledUpdater() {
k_sleep(K_FOREVER);
}

if (KeyBacklightBrightness == 0) {
if (currentScaling == 0 && KeyBacklightBrightness == 0) {
sleepLeds();
}

ledsNeedUpdate = false;
if (currentScaling != KeyBacklightBrightness) {
k_sleep(K_MSEC(scalingFadeDelay));
recalculateScaling();
}

ledsNeedUpdate = currentScaling != KeyBacklightBrightness;
}
}

Expand Down

0 comments on commit eb12223

Please sign in to comment.