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
// If a key is held down, and the backlight is dimmed, brighten it and reset the timer
else if (dimmed) {
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


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