diff --git a/docs/feature_rgblight.md b/docs/feature_rgblight.md index be4ddfa72956..f1d196579fbc 100644 --- a/docs/feature_rgblight.md +++ b/docs/feature_rgblight.md @@ -76,6 +76,7 @@ Your RGB lighting can be configured by placing these `#define`s in your `config. |`RGBLIGHT_LIMIT_VAL` |`255` |The maximum brightness level | |`RGBLIGHT_SLEEP` |*Not defined*|If defined, the RGB lighting will be switched off when the host goes to sleep| |`RGBLIGHT_SPLIT` |*Not defined*|If defined, synchronization functionality for split keyboards is added| +|`RGBLIGHT_IDLE_TIMEOUT`|*Not defined*|If defined, turn the backlight off after this many minutes of inactivity.| ## Effects and Animations diff --git a/quantum/quantum.c b/quantum/quantum.c index d98c601d991a..a3e5cc4d0ae0 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -297,6 +297,9 @@ bool process_record_quantum(keyrecord_t *record) { #endif #ifdef SPACE_CADET_ENABLE process_space_cadet(keycode, record) && + #endif + #ifdef RGBLIGHT_USE_PROCESS + process_rgblight(keycode, record) && #endif true)) { return false; diff --git a/quantum/rgblight.c b/quantum/rgblight.c index f569d6b9e3f4..aa36328d677d 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -93,12 +93,16 @@ bool is_rgblight_initialized = false; animation_status_t animation_status = {}; #endif +#ifdef RGBLIGHT_IDLE_ENABLE +bool rgblight_idle_timedout = false; +uint32_t rgblight_idle_timer; +#endif + #ifndef LED_ARRAY LED_TYPE led[RGBLED_NUM]; #define LED_ARRAY led #endif - static uint8_t clipping_start_pos = 0; static uint8_t clipping_num_leds = RGBLED_NUM; static uint8_t effect_start_pos = 0; @@ -220,6 +224,10 @@ void rgblight_init(void) { rgblight_mode_noeeprom(rgblight_config.mode); } + #ifdef RGBLIGHT_IDLE_ENABLE + rgblight_idle_timer = timer_read32(); + #endif + is_rgblight_initialized = true; } @@ -797,6 +805,15 @@ void rgblight_task(void) { // static light mode, do nothing here if ( 1 == 0 ) { //dummy } +#ifdef RGBLIGHT_IDLE_ENABLE + // exit early if we are timedout + else if (rgblight_idle_timedout) { + return; + } else if (timer_elapsed32(rgblight_idle_timer) >= (RGBLIGHT_IDLE_TIMEOUT * RGBLIGHT_IDLE_MULTIPLIER)) { + rgblight_idle_timedout = true; + rgblight_disable_noeeprom(); + } +#endif #ifdef RGBLIGHT_EFFECT_BREATHING else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) { // breathing mode @@ -885,6 +902,21 @@ void rgblight_task(void) { #endif /* RGBLIGHT_USE_TIMER */ +#ifdef RGBLIGHT_USE_PROCESS +bool process_rgblight(uint16_t keycode, keyrecord_t *record) { + #ifdef RGBLIGHT_IDLE_ENABLE + if (record->event.pressed) { + if (rgblight_idle_timedout) { + rgblight_idle_timedout = false; + rgblight_enable_noeeprom(); + } + rgblight_idle_timer = timer_read32(); + } + #endif + return true; +} +#endif + // Effects #ifdef RGBLIGHT_EFFECT_BREATHING diff --git a/quantum/rgblight.h b/quantum/rgblight.h index cba18ae72912..39ee062bce25 100644 --- a/quantum/rgblight.h +++ b/quantum/rgblight.h @@ -264,6 +264,15 @@ void rgblight_timer_enable(void); void rgblight_timer_disable(void); void rgblight_timer_toggle(void); +// if timeout is set, enable the rgblight idle feature +#ifdef RGBLIGHT_IDLE_TIMEOUT + #define RGBLIGHT_IDLE_ENABLE + #define RGBLIGHT_IDLE_MULTIPLIER 60000 +#endif + +#include "quantum.h" +bool process_rgblight(uint16_t keycode, keyrecord_t *record); + #ifdef RGBLIGHT_SPLIT #define RGBLIGHT_STATUS_CHANGE_MODE (1<<0) #define RGBLIGHT_STATUS_CHANGE_HSVS (1<<1) diff --git a/quantum/rgblight_reconfig.h b/quantum/rgblight_reconfig.h index 11bd4fd11820..10e7eb1f2eb6 100644 --- a/quantum/rgblight_reconfig.h +++ b/quantum/rgblight_reconfig.h @@ -18,6 +18,10 @@ #define RGBLIGHT_EFFECT_STATIC_GRADIENT #endif +#if defined(RGBLIGHT_IDLE_TIMEOUT) + #define RGBLIGHT_USE_PROCESS +#endif + // check dynamic animation effects chose ? #if defined(RGBLIGHT_EFFECT_BREATHING) || \ defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || \ @@ -26,7 +30,8 @@ defined(RGBLIGHT_EFFECT_KNIGHT) || \ defined(RGBLIGHT_EFFECT_CHRISTMAS) || \ defined(RGBLIGHT_EFFECT_RGB_TEST) || \ - defined(RGBLIGHT_EFFECT_ALTERNATING) + defined(RGBLIGHT_EFFECT_ALTERNATING) || \ + defined(RGBLIGHT_IDLE_ENABLE) #define RGBLIGHT_USE_TIMER #ifndef RGBLIGHT_ANIMATIONS #define RGBLIGHT_ANIMATIONS // for backward compatibility