Keyboard and screen dimming

Here is a patch for the Pocket keyboard firmware to dim the LEDs after some period of inactivity. The LEDs return to their previous brightness/state when a key is pressed.

This is based on code that was shared in a previous thread for the Reform (I added a timeout to the (standalone) keyboard backlight)

--- a/pocket-reform-keyboard-fw/pocket-hid/src/main.c
+++ b/pocket-reform-keyboard-fw/pocket-hid/src/main.c
@@ -71,6 +71,15 @@ static uint8_t pressed_scancodes[MAX_SCANCODES] = {0,0,0,0,0,0};
 static int pressed_keys = 0;
 static volatile uint32_t led_value = 0;
 
+// used for keyboard dimming 
+int idle_counter = 0;
+int saved_brightness;
+bool dimmed = false;
+
+int led_brightness = 0;
+int led_saturation = 255;
+int led_hue = 127;
+
 void hid_task(void);
 int process_keyboard(uint8_t* resulting_scancodes);
 
@@ -426,8 +435,25 @@ int process_keyboard(uint8_t* resulting_scancodes) {
   }
 
   // if no more keys are held down, allow a new menu command
-  if (total_pressed<1) last_menu_key = 0;
-
+  if (total_pressed<1) {
+    last_menu_key = 0;
+    if (!dimmed) {
+      if (idle_counter >= 1000000) {
+	dimmed = true;
+	saved_brightness = led_brightness;
+	led_set_brightness(0);
+      } else {
+	idle_counter++;
+      }
+    }
+  } else {
+    if (dimmed) {
+      led_set_brightness(saved_brightness);
+      dimmed = false;
+    }
+    idle_counter = 0;
+  }
+    
   // if device is off and user is pressing random keys,
   // show a hint for turning on the device
   if (!remote_get_power_state()) {
@@ -659,10 +685,6 @@ void hid_task(void)
   }
 }
 
-int led_brightness = 0;
-int led_saturation = 255;
-int led_hue = 127;
-
 typedef struct RgbColor
 {
   unsigned char r;

In addition, to turn off the screen after a period of inactivity you can add the following to .sway/config:

exec swayidle -w \
          timeout 600 'swaymsg "output * dpms off"' \
	       resume 'swaymsg "output * dpms on"' 
2 Likes

Funny, I was experimenting with similar things today, so I totally feel the need as well. Do you have an account on https://source.mnt.re and what’s your username? If you request one I will approve it. Then you could fork & make an MR and we take it from there.

I just made an account (username pff) and I can try to fork and make a MR once the account is approved.

1 Like

Ok, I forked the code and submitted a MR. I’m not 100% sure I followed the right pattern for this, but I think so. (I did not create a new branch before making changes to my fork, let me know if I should have…)

1 Like