Skip to content

Commit

Permalink
Add fade out before idle
Browse files Browse the repository at this point in the history
  • Loading branch information
ViToni committed Dec 13, 2021
1 parent ab21c4d commit 439aeba
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
2 changes: 2 additions & 0 deletions keyboards/gmmk/pro/iso/keymaps/vitoni/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions keyboards/gmmk/pro/iso/keymaps/vitoni/readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
|===
23 changes: 19 additions & 4 deletions users/vitoni/rgb_matrix_effects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
36 changes: 33 additions & 3 deletions users/vitoni/rgb_matrix_effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
30 changes: 25 additions & 5 deletions users/vitoni/vitoni.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@
#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,
// so that it matches the index used for sine calculation
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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 439aeba

Please sign in to comment.