From 0e451982a0a4030ae9c37dd3a67a49ac51cd0b11 Mon Sep 17 00:00:00 2001 From: Victor Toni Date: Thu, 28 Oct 2021 20:48:46 +0200 Subject: [PATCH] Add breathe effect when idle --- .../gmmk/pro/iso/keymaps/vitoni/config.h | 2 ++ .../gmmk/pro/iso/keymaps/vitoni/readme.adoc | 11 +++++++-- users/vitoni/readme.adoc | 2 +- users/vitoni/rgb_matrix_effects.c | 14 +++++++++++ users/vitoni/rgb_matrix_effects.h | 24 +++++++++++++++++++ users/vitoni/vitoni.c | 8 +++++++ 6 files changed, 58 insertions(+), 3 deletions(-) diff --git a/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h b/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h index ba6008ee3111..fd8f1d6859e7 100644 --- a/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h +++ b/keyboards/gmmk/pro/iso/keymaps/vitoni/config.h @@ -13,6 +13,8 @@ #define RGB_DISABLE_WHEN_USB_SUSPENDED // number of milliseconds to wait until activating RGB idle effects #define RGB_IDLE_TIMEOUT 4500 // 4.5 seconds + // activate breathe effect when idle + #define RGB_IDLE_BREATHE // 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 c00575facaf6..38a74a568bf0 100644 --- a/keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc +++ b/keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc @@ -81,7 +81,8 @@ Used to calculate lead time for fade out before suspend timeout. |=== -`RGB_IDLE_TIMEOUT` enables fading out after being idle for the defined time. +`RGB_IDLE_TIMEOUT` enables fading out after being idle for the defined time and allows +* `RGB_IDLE_BREATHE` also activates a brethe effect while idling. [%header] |=== @@ -93,5 +94,11 @@ Used to calculate lead time for fade out before suspend timeout. |RGB_IDLE_MINIMUM_BRIGHTNESS |`RGB_MATRIX_MAXIMUM_BRIGHTNESS` / 5 -|Brightness value RGB is dimmed to when starting to idle. +|Brightness value RGB is dimmed to when starting to idle. + +When breathing used as the lower bound of the brightness value. + +|RGB_IDLE_MAXIMUM_BRIGHTNESS +|`RGB_MATRIX_MAXIMUM_BRIGHTNESS` * 2/5 +|Upper bound of brightness value of the RGB light while breathing. + |=== diff --git a/users/vitoni/readme.adoc b/users/vitoni/readme.adoc index 69ccfa9e682b..acf65793d2de 100644 --- a/users/vitoni/readme.adoc +++ b/users/vitoni/readme.adoc @@ -8,7 +8,7 @@ Common functions are declared in link:utils.h[]. These function are not directly == rgb_matrix_effects.h -Functions in link:rgb_matrix_effects.h[] make use of common function in `utils.h` and are used to create to RGB matrix effects such as fading. +Functions in link:rgb_matrix_effects.h[] make use of common function in `utils.h` and are used to create to RGB matrix effects such as fading or breathing. == vitoni.h diff --git a/users/vitoni/rgb_matrix_effects.c b/users/vitoni/rgb_matrix_effects.c index 199734d393b1..3a13e99bc7e2 100644 --- a/users/vitoni/rgb_matrix_effects.c +++ b/users/vitoni/rgb_matrix_effects.c @@ -219,4 +219,18 @@ bool idle_fade_out(const uint8_t time) { return fade_out_ranged(time, range_min, range_max); } + +#if defined(RGB_IDLE_BREATHE) +/** + * @brief Changes value/brightness to create a breathing effect based on given timer. + * @details Brightness will breathe in the range starting from `RGB_IDLE_MINIMUM_BRIGHTNESS` to `RGB_IDLE_MAXIMUM_BRIGHTNESS`. + * @param[in] time A (usually scaled) timer + */ +void idle_breathe(const uint8_t time) { + static const uint8_t range_min = RGB_IDLE_MINIMUM_BRIGHTNESS; + static const uint8_t range_max = RGB_IDLE_MAXIMUM_BRIGHTNESS; + + rgb_matrix_config.hsv.v = scaled_sin(time, range_min, range_max); +} +#endif // RGB_IDLE_BREATHE #endif // RGB_IDLE_TIMEOUT diff --git a/users/vitoni/rgb_matrix_effects.h b/users/vitoni/rgb_matrix_effects.h index cbdcd0a31cdb..ed74500b1827 100644 --- a/users/vitoni/rgb_matrix_effects.h +++ b/users/vitoni/rgb_matrix_effects.h @@ -147,4 +147,28 @@ bool fade_out(const uint8_t time); * @return Returns `true` if `RGB_IDLE_MINIMUM_BRIGHTNESS` has been reached, `false` otherwise. */ bool idle_fade_out(const uint8_t time); + +#if defined(RGB_IDLE_BREATHE) +# if !defined(RGB_IDLE_MAXIMUM_BRIGHTNESS) + // maximum brightness when idling +# define RGB_IDLE_MAXIMUM_BRIGHTNESS (RGB_MATRIX_MAXIMUM_BRIGHTNESS*3/5) +# endif +# if !(0 <= RGB_IDLE_MAXIMUM_BRIGHTNESS) +# error "RGB_IDLE_MINIMUM_BRIGHTNESS must not be less than ZERO, was: " RGB_IDLE_MAXIMUM_BRIGHTNESS +# endif // RGB_IDLE_MAXIMUM_BRIGHTNESS < 0 +# if !(RGB_IDLE_MINIMUM_BRIGHTNESS < RGB_IDLE_MAXIMUM_BRIGHTNESS) +# error "RGB_IDLE_MINIMUM_BRIGHTNESS must be less than RGB_IDLE_MAXIMUM_BRIGHTNESS" +# endif // RGB_IDLE_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MINIMUM_BRIGHTNESS +# if !(RGB_IDLE_MAXIMUM_BRIGHTNESS <= RGB_MATRIX_MAXIMUM_BRIGHTNESS) +# error "RGB_IDLE_MAXIMUM_BRIGHTNESS must be less than or equal to RGB_MATRIX_MAXIMUM_BRIGHTNESS" +# endif // RGB_MATRIX_MAXIMUM_BRIGHTNESS <= RGB_IDLE_MAXIMUM_BRIGHTNESS + +/** + * @brief Changes value/brightness to create a breathing effect based on given timer. + * @details Brightness will breathe in the range starting from `RGB_IDLE_MINIMUM_BRIGHTNESS` to `RGB_IDLE_MAXIMUM_BRIGHTNESS`. + * @param[in] time A (usually scaled) timer + */ +void idle_breathe(const uint8_t time); +#endif // RGB_IDLE_BREATHE + #endif // RGB_IDLE_TIMEOUT diff --git a/users/vitoni/vitoni.c b/users/vitoni/vitoni.c index 9744b5359c2d..2a0ff5c46f54 100644 --- a/users/vitoni/vitoni.c +++ b/users/vitoni/vitoni.c @@ -61,6 +61,14 @@ void matrix_scan_user_rgb(void) { } break; case IDLE: +#if defined(RGB_IDLE_BREATHE) + if (calc_offset) { + // no need to calculate time_offset since we are aligned already due to IDLE_FADE_OUT + // resetting flag for subsequent calls + calc_offset = false; + } + idle_breathe(time + time_offset); +#endif break; #endif #if defined(RGB_DISABLE_WITH_FADE_OUT)