diff --git a/keyboards/anne_pro/anne_pro.c b/keyboards/anne_pro/anne_pro.c index 36f21b1195..0fd6b304cc 100644 --- a/keyboards/anne_pro/anne_pro.c +++ b/keyboards/anne_pro/anne_pro.c @@ -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: diff --git a/keyboards/anne_pro/anne_pro_lighting.c b/keyboards/anne_pro/anne_pro_lighting.c index 886350bf09..6659dd1a53 100644 --- a/keyboards/anne_pro/anne_pro_lighting.c +++ b/keyboards/anne_pro/anne_pro_lighting.c @@ -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; @@ -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 */ @@ -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); } diff --git a/keyboards/anne_pro/anne_pro_lighting.h b/keyboards/anne_pro/anne_pro_lighting.h index 29b93b74a8..749ab5b6c2 100644 --- a/keyboards/anne_pro/anne_pro_lighting.h +++ b/keyboards/anne_pro/anne_pro_lighting.h @@ -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); diff --git a/keyboards/anne_pro/config.h b/keyboards/anne_pro/config.h index 6381084dca..fdd468edb0 100644 --- a/keyboards/anne_pro/config.h +++ b/keyboards/anne_pro/config.h @@ -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 *