I added a timeout to the (standalone) keyboard backlight

It was really simple! I recommend trying it.

To add a timeout to the keyboard backlight it was only necessary to modify keyboard.c I added two global variables with the following declarations:

  • bool dimmed = false tracks whether the backlight has timed out already
  • uint32_t idle_counter = 0 tracks how many process_keyboard() calls have occured since the last time total_pressed was greater than 0 (the last time a key was pressed). it’s probably bigger than it needs to be.

I then modified the last lines of process_keyboard(), where it checks whether (total_pressed<1) at about line 270.

here’s the modified section:

// if no more keys are held down, allow a new meta command
if (total_pressed<1) {
last_meta_key = 0;
if (!dimmed) {
// check whether backlight should be dimmed
if (idle_counter >= 600000){ // start dimming the backlight after about 5 minutes
if (idle_counter % 300 == 0) kbd_brightness_dec(); // fadeout
if (idle_counter >= 603000) dimmed = true; //after decremented to 0, stop decrementing, board is dimmed
}
idle_counter++;
}
}
// If a key is held down, and the backlight is dimmed, brighten it and reset the timer
else if (dimmed) {
kbd_brightness_set(10);
idle_counter = 0;
dimmed = false;
}

The nested if statements aren’t particularly elegant but the goal was to prevent unnecessary added code execution once the board is in an idle, dimmed state. I also modified backlight.c just so the startup brightness was 10 to match the other new code.

If requested I can just post the entirety of my keyboard.c

4 Likes

I’ll definitely try this with the Reform keyboard and report back.

Thanks!

Thank you for sharing this!
I implemented this, with an additional variable to save the current brightness value before dimming, I think that way its a pretty universally useful feature.

If anybody is interested, here is a patch to keyboard.c in the latest firmware to dim the keyboard backlight after some time without key presses. The brightness is restored to the previous setting once a key is pressed.

91a92,95
> uint32_t idle_counter = 0;
> int16_t brightness;
> bool dimmed = false;
> 
264,265c268,286
<   if (total_pressed<1) last_meta_key = 0;
< 
---
>   if (total_pressed<1) {
>     last_meta_key = 0;
>     if (!dimmed) {
>       if (idle_counter >= 1000000) {
>         dimmed = true;
>         brightness = kbd_brightness_get();
>         kbd_brightness_set(0);
>       } else {
>         idle_counter++;
>       }
>     }
>   } else {
>     if (dimmed) {
>       kbd_brightness_set(brightness);
>       dimmed = false;
>     }
>     idle_counter = 0;
>   }
>     
2 Likes

Hi,

i’ve published my small additions to the firmware including backlight fade out here: ~rwa/reform: master - sourcehut git

2 Likes

To make contributions, it might be best to create an account and then file a merge request with your changes over at Reform / reform · GitLab

1 Like

Yeah, i’m aware of that - at the moment i consider my changes not “generic” enough to add it to the upstream firmware. It’s just stuff that i thought might be “funny” but not actually usefull for a broader user base.

1 Like