Skip to content

Commit

Permalink
Add basic keyboard idling feature (resolves qmk#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
msvisser authored and TinfoilSubmarine committed Feb 24, 2025
1 parent 0a60f53 commit 1f21a15
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions keyboards/anne_pro/anne_pro.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
/* Update the key status for the reactive effects */
anne_pro_lighting_update_dynamic(record);
anne_pro_lighting_update_timeout(record);

switch (keycode) {
case APL_RGB:
Expand Down
46 changes: 46 additions & 0 deletions keyboards/anne_pro/anne_pro_lighting.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ static UARTConfig led_uart_cfg = {
.cr3 = 0
};

/* Timer to keep track of seconds, this allows the backlight timeout */
static virtual_timer_t seconds_timer;
static volatile uint32_t seconds_timer_counter = 0;

/* Timer callback to update the seconds timer */
static void update_seconds_timer(void *p) {
chSysLockFromISR();
chVTSetI(&seconds_timer, MS2ST(1000), update_seconds_timer, p);
chSysUnlockFromISR();

seconds_timer_counter++;
}

/* State of the leds on the keyboard */
static volatile bool leds_enabled = false;

Expand All @@ -62,6 +75,9 @@ void anne_pro_lighting_init(void) {
uartStart(LED_UART, &led_uart_cfg);
palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(7));

/* Enable the seconds timer for backlight timeout */
chVTSet(&seconds_timer, MS2ST(1000), update_seconds_timer, NULL);
}

/* Buffer for the keystate packet */
Expand Down Expand Up @@ -93,8 +109,38 @@ void anne_pro_lighting_update_dynamic(keyrecord_t *record) {
}
}


/* Timer keeping track of the last keypress */
static uint32_t last_keypress_timer = 0;
/* Was the lighting turned off by the timeout */
static bool turned_off_by_timeout = false;

/* Update the last keypress timer when a key is pressed */
void anne_pro_lighting_update_timeout(keyrecord_t *record) {
/* Make sure this is actually a keypress event */
if (IS_NOEVENT(record->event)) return;

if (record->event.pressed) {
/* Update the last keypress timer */
last_keypress_timer = seconds_timer_counter;

/* If the lighting was turned off by a timeout, turn it back on */
if (turned_off_by_timeout) {
anne_pro_lighting_on();
turned_off_by_timeout = false;
}
}
}

/* Update lighting, should be called every matrix scan */
void anne_pro_lighting_update(void) {
/* If the backlight in on, and the last keypress timeout is hit */
if (leds_enabled && (seconds_timer_counter - last_keypress_timer) >= BACKLIGHT_TIMEOUT) {
/* Turn off the backlight */
anne_pro_lighting_off();
turned_off_by_timeout = true;
}

if (!uart_tx_ringbuf_empty(&led_uart_ringbuf)) {
uart_tx_ringbuf_start_transmission(&led_uart_ringbuf);
}
Expand Down
1 change: 1 addition & 0 deletions keyboards/anne_pro/anne_pro_lighting.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
void anne_pro_lighting_init(void);
void anne_pro_lighting_update(void);
void anne_pro_lighting_update_dynamic(keyrecord_t *record);
void anne_pro_lighting_update_timeout(keyrecord_t *record);
void anne_pro_lighting_toggle(void);
void anne_pro_lighting_on(void);
void anne_pro_lighting_off(void);
Expand Down
6 changes: 6 additions & 0 deletions keyboards/anne_pro/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
#define DEBOUNCE 5

/*
* Timeout after which the backlight of the keyboard is disabled if no keypresses are received.
* If this feature is not needed you can set the timeout to UINT32_MAX, this is more than 100 years.
*/
#define BACKLIGHT_TIMEOUT 60

/*
* Force NKRO
*
Expand Down

0 comments on commit 1f21a15

Please sign in to comment.