From 439aeba64520157d751305fbe221ec2659132d5d Mon Sep 17 00:00:00 2001 From: Victor Toni Date: Tue, 26 Oct 2021 20:41:09 +0200 Subject: [PATCH] Add fade out before idle --- .../gmmk/pro/iso/keymaps/vitoni/config.h | 2 ++ .../gmmk/pro/iso/keymaps/vitoni/readme.adoc | 16 +++++++++ users/vitoni/rgb_matrix_effects.c | 23 +++++++++--- users/vitoni/rgb_matrix_effects.h | 36 +++++++++++++++++-- users/vitoni/vitoni.c | 30 +++++++++++++--- 5 files changed, 95 insertions(+), 12 deletions(-) diff --git a/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h b/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h index 81afc46b7a06..ba6008ee3111 100644 --- a/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h +++ b/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h @@ -11,6 +11,8 @@ // fading out is timed (depending on the rgb_matrix_config.speed) to have finished before reaching RGB_DISABLE_TIMEOUT #define RGB_DISABLE_WITH_FADE_OUT #define RGB_DISABLE_WHEN_USB_SUSPENDED + // number of milliseconds to wait until activating RGB idle effects + #define RGB_IDLE_TIMEOUT 4500 // 4.5 seconds // fade in when we have been suspended #define RGB_FADE_IN #endif diff --git a/keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc b/keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc index 558c97c83463..c00575facaf6 100644 --- a/keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc +++ b/keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc @@ -78,4 +78,20 @@ Parameters used to define the behavior are: | 200 (<= UNIT8_MAX) | Maximum assumed value for brightness. Used to calculate lead time for fade out before suspend timeout. + +|=== + +`RGB_IDLE_TIMEOUT` enables fading out after being idle for the defined time. + +[%header] +|=== +|Key | Default | Description + +|RGB_IDLE_TIMEOUT +|4500 +|Time in milliseconds without activity before considered to be idle. + +|RGB_IDLE_MINIMUM_BRIGHTNESS +|`RGB_MATRIX_MAXIMUM_BRIGHTNESS` / 5 +|Brightness value RGB is dimmed to when starting to idle. |=== diff --git a/users/vitoni/rgb_matrix_effects.c b/users/vitoni/rgb_matrix_effects.c index d6b3d7140170..199734d393b1 100644 --- a/users/vitoni/rgb_matrix_effects.c +++ b/users/vitoni/rgb_matrix_effects.c @@ -108,7 +108,7 @@ bool fade_out_ranged(const uint8_t time, const uint8_t range_min, const uint8_t * @see rgb_matrix_sethsv_noeeprom() */ void rgb_matrix_sethsv_noeeprom_user(const uint16_t hue, const uint8_t sat, const uint8_t val) { -#if defined(RGB_FADE_IN) +#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT) rgb_matrix_config.hsv.h = hue; rgb_matrix_config.hsv.s = sat; // omitting setting the value to avoid interfering with effects @@ -118,7 +118,7 @@ void rgb_matrix_sethsv_noeeprom_user(const uint16_t hue, const uint8_t sat, cons #endif } -#if defined(RGB_FADE_IN) +#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT) /** * @brief Calculates the time offset required by fade in. * @details Using an arbitrary timer any point on the sine curve might be pointed to. @@ -162,8 +162,7 @@ bool fade_in(const uint8_t time) { } #endif -#if defined(RGB_DISABLE_WITH_FADE_OUT) - +#if defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT) /** * @brief Calculates the time offset required by fade out. * @details Using an arbitrary timer any point on the Sinus curve might be pointed to. @@ -192,7 +191,9 @@ uint8_t calc_fade_out_offset(const uint8_t time) { return time_offset; } +#endif +#if defined(RGB_DISABLE_WITH_FADE_OUT) /** * @brief Decreases value/brightness until reaching 0 based on given timer. * @param[in] time A (usually scaled) timer @@ -205,3 +206,17 @@ bool fade_out(const uint8_t time) { return fade_out_ranged(time, range_min, range_max); } #endif + +#if defined(RGB_IDLE_TIMEOUT) +/** + * @brief Decreases value/brightness until reaching `RGB_IDLE_MINIMUM_BRIGHTNESS` based on given timer. + * @param[in] time A (usually scaled) timer + * @return Returns `true` if `RGB_IDLE_MINIMUM_BRIGHTNESS` has been reached, `false` otherwise. + */ +bool idle_fade_out(const uint8_t time) { + static const uint8_t range_min = RGB_IDLE_MINIMUM_BRIGHTNESS; + static const uint8_t range_max = RGB_MATRIX_MAXIMUM_BRIGHTNESS; + + return fade_out_ranged(time, range_min, range_max); +} +#endif // RGB_IDLE_TIMEOUT diff --git a/users/vitoni/rgb_matrix_effects.h b/users/vitoni/rgb_matrix_effects.h index 157a062f682e..cbdcd0a31cdb 100644 --- a/users/vitoni/rgb_matrix_effects.h +++ b/users/vitoni/rgb_matrix_effects.h @@ -12,7 +12,11 @@ */ enum states { REGULAR //!< when in regular use -#if defined(RGB_FADE_IN) +#if defined(RGB_IDLE_TIMEOUT) + ,IDLE_FADE_OUT //!< when started idling + ,IDLE //!< when idling +#endif +#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT) ,FADE_IN //!< when starting initially or before going back to REGULAR #endif #if defined(RGB_DISABLE_WITH_FADE_OUT) @@ -61,7 +65,7 @@ uint16_t scale_2_rgb_time(const uint8_t scaled_time); */ void rgb_matrix_sethsv_noeeprom_user(const uint16_t hue, const uint8_t sat, const uint8_t val); -#if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) +#if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT) # if defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) # if (RGB_MATRIX_MAXIMUM_BRIGHTNESS) < 1 # error "RGB_MATRIX_MAXIMUM_BRIGHTNESS must not be less than ONE" @@ -74,7 +78,7 @@ void rgb_matrix_sethsv_noeeprom_user(const uint16_t hue, const uint8_t sat, cons # endif #endif -#if defined(RGB_FADE_IN) +#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT) /** * @brief Calculates the time offset required by fade in. * @details Using an arbitrary timer any point on the sine curve might be pointed to. @@ -98,7 +102,9 @@ bool fade_in(const uint8_t time); # if !defined(RGB_DISABLE_TIMEOUT) # warning "RGB_DISABLE_WITH_FADE_OUT expects RGB_DISABLE_TIMEOUT to be defined" # endif +#endif +#if defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT) /** * @brief Calculates the time offset required by fade out. * @details Using an arbitrary timer any point on the Sinus curve might be pointed to. @@ -109,7 +115,9 @@ bool fade_in(const uint8_t time); * @return Offset required so that time matches the current brightness */ uint8_t calc_fade_out_offset(const uint8_t time); +#endif +#if defined(RGB_DISABLE_WITH_FADE_OUT) /** * @brief Decreases value/brightness until reaching 0 based on given timer. * @param[in] time A (usually scaled) timer @@ -118,3 +126,25 @@ uint8_t calc_fade_out_offset(const uint8_t time); bool fade_out(const uint8_t time); #endif +#if defined(RGB_IDLE_TIMEOUT) +# if RGB_IDLE_TIMEOUT < 0 +# error "RGB_IDLE_TIMEOUT must not be less than ZERO" +# endif +# if !defined(RGB_IDLE_MINIMUM_BRIGHTNESS) + // minimum brightness when idling +# define RGB_IDLE_MINIMUM_BRIGHTNESS (RGB_MATRIX_MAXIMUM_BRIGHTNESS/5) +# endif +# if RGB_IDLE_MINIMUM_BRIGHTNESS < 0 +# error "RGB_IDLE_MINIMUM_BRIGHTNESS must not be less than ZERO" +# endif // RGB_IDLE_MINIMUM_BRIGHTNESS < 0 +# if RGB_MATRIX_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MINIMUM_BRIGHTNESS +# error "RGB_IDLE_MINIMUM_BRIGHTNESS must be less than RGB_MATRIX_MAXIMUM_BRIGHTNESS" +# endif // RGB_MATRIX_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MINIMUM_BRIGHTNESS + +/** + * @brief Decreases value/brightness until reaching `RGB_IDLE_MINIMUM_BRIGHTNESS` based on given timer. + * @param[in] time A (usually scaled) timer + * @return Returns `true` if `RGB_IDLE_MINIMUM_BRIGHTNESS` has been reached, `false` otherwise. + */ +bool idle_fade_out(const uint8_t time); +#endif // RGB_IDLE_TIMEOUT diff --git a/users/vitoni/vitoni.c b/users/vitoni/vitoni.c index d356fa1aa886..9744b5359c2d 100644 --- a/users/vitoni/vitoni.c +++ b/users/vitoni/vitoni.c @@ -9,7 +9,7 @@ #include "rgb_matrix_effects.h" #include "utils.h" -#if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) +#if defined(RGB_FADE_IN) || defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT) static uint8_t state; // flag used to indicate that offset calculation is needed to adjust the timer, @@ -17,13 +17,18 @@ static uint8_t state; static bool calc_offset; void matrix_scan_user_rgb(void) { -#if defined(RGB_DISABLE_WITH_FADE_OUT) +#if defined(RGB_DISABLE_WITH_FADE_OUT) || defined(RGB_IDLE_TIMEOUT) const uint8_t time = rgb_time_2_scale(); #endif static uint8_t time_offset; const uint32_t inactivity_millis = last_input_activity_elapsed(); +#if defined(RGB_IDLE_TIMEOUT) + if (IDLE != state && RGB_IDLE_TIMEOUT <= inactivity_millis) { + update_value(&state, IDLE_FADE_OUT, &calc_offset); + } +#endif #if defined(RGB_DISABLE_WITH_FADE_OUT) const uint32_t fade_out_duration = scale_2_rgb_time(128); const uint32_t start_fade_out_after_millis = (RGB_DISABLE_TIMEOUT) > fade_out_duration @@ -33,7 +38,7 @@ void matrix_scan_user_rgb(void) { if (start_fade_out_after_millis <= inactivity_millis) { update_value(&state, FADE_OUT, &calc_offset); } -#else +#elif defined(RGB_DISABLE_TIMEOUT) // having to set brightness "manually" to black as starting point for fade in // for the time when returning from suspended state if (RGB_DISABLE_TIMEOUT <= inactivity_millis + 15) { @@ -43,6 +48,21 @@ void matrix_scan_user_rgb(void) { #endif switch(state) { +#if defined(RGB_IDLE_TIMEOUT) + case IDLE_FADE_OUT: + if (calc_offset) { + time_offset = calc_fade_out_offset(time); + + // resetting flag for subsequent calls + calc_offset = false; + } + if (idle_fade_out(time + time_offset)) { + update_value(&state, IDLE, &calc_offset); + } + break; + case IDLE: + break; +#endif #if defined(RGB_DISABLE_WITH_FADE_OUT) case FADE_OUT: if (calc_offset) { @@ -56,7 +76,7 @@ void matrix_scan_user_rgb(void) { } break; #endif -#if defined(RGB_FADE_IN) +#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT) case FADE_IN: { // since we want to be active, fade in should be faster than e.g. fading out @@ -78,7 +98,7 @@ void matrix_scan_user_rgb(void) { } } -#if defined(RGB_FADE_IN) +#if defined(RGB_FADE_IN) || defined(RGB_IDLE_TIMEOUT) bool process_record_user_rgb(const uint16_t keycode, const keyrecord_t *record) { // if we are in a non regular state we might have faded out (eventually partially) // so we restore brightness (to max as we don't keep track of manually changed brightness)