-
-
Notifications
You must be signed in to change notification settings - Fork 40.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RGB fade in when resuming after suspend
- Loading branch information
Showing
11 changed files
with
569 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
= User functions | ||
|
||
Functions are mostly related to changing the RGB lights depending on user interaction and when idling. | ||
|
||
== utils.h | ||
|
||
Common functions are declared in link:utils.h[]. These function are not directly RGB related but used to modify state and calculate values. | ||
|
||
== 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. | ||
|
||
== vitoni.h | ||
|
||
The functions declared in link:vitoni.h[] are used as entry points for usage of RGB effects. | ||
One entry point is `matrix_scan` based for regular task while the other is `process_record` based for user activity tasks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright 2021 Victor Toni (@vitoni) | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "rgb_matrix_effects.h" | ||
|
||
#include <rgb_matrix.h> | ||
#include <lib/lib8tion/lib8tion.h> | ||
|
||
#include "utils.h" | ||
|
||
/* | ||
Offset used to start at the right point in th curve to avoid big jumps in brightness | ||
0 => 0% (signed) => 50% (unsigned) | ||
64 => 100% (signed) => 100% (unsigned) | ||
128 => 0% (signed) => 50% (unsigned) | ||
192 => -100% (signed) => 0% (unsigned) | ||
*/ | ||
enum PHASE { | ||
PHASE_ZERO_RAISING | ||
,PHASE_HIGH | ||
,PHASE_ZERO_FALLING | ||
,PHASE_LOW | ||
}; | ||
|
||
/** | ||
* @brief Calculates the offset so that a specific time is aligned to a specific point in the sine curve. | ||
* @param[in] time The time for which the offset shopuld be calculated. | ||
* @param[in] phase Phase which should be reached with the offset | ||
* @see PHASE | ||
*/ | ||
uint8_t offset_for_time(const uint8_t time, const uint8_t phase) { | ||
switch (phase) { | ||
case PHASE_ZERO_RAISING: | ||
return 0 - time; | ||
case PHASE_HIGH: | ||
return 64 - time; | ||
case PHASE_ZERO_FALLING: | ||
return 128 - time; | ||
case PHASE_LOW: | ||
return 192 - time; | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
/** | ||
* @brief Scales down `g_rgb_timer` so that it can be used for RGB effects. | ||
* @return scaled down timer | ||
* @see rgb_time_2_scale_w_factor() | ||
*/ | ||
uint8_t rgb_time_2_scale(void) { | ||
static const uint8_t factor = 1; | ||
return rgb_time_2_scale_w_factor(factor); | ||
} | ||
|
||
/* | ||
* Used to slow down RGB speed. | ||
*/ | ||
static const uint8_t rgb_speed_divisor = 8; | ||
|
||
/** | ||
* @brief Scales down `g_rgb_timer` so that it can be used for RGB effects. | ||
* @details Usually these calculations aredone internally by some RGB effects. | ||
This method exposed to scaling so that all effects to have same timebase. If `rgb_matrix_config.speed` all effects are affected the same. | ||
* @param[in] factor The factor can be used to speed up some operations in relation to others. | ||
* @return scaled down timer taking into account the given factor | ||
* @see g_rgb_timer | ||
* @see rgb_matrix_config.speed | ||
*/ | ||
uint8_t rgb_time_2_scale_w_factor(const uint8_t rgb_speed_factor) { | ||
const uint8_t scaled_time = scale16by8(g_rgb_timer, rgb_matrix_config.speed * rgb_speed_factor / rgb_speed_divisor); | ||
|
||
return scaled_time; | ||
} | ||
|
||
/** | ||
* @brief Inverse function to calculate time required to execute `timer` steps. | ||
* @details This method allows calculation of the time needed to execute N `timer`steps. | ||
Usefull when using a scaled down time but requiring the time needed to perform these steps. | ||
* @param[in] scaled_time scaled down timer to inverse to time | ||
* @return time corresponding to scaled down time | ||
* @see rgb_time_2_scale() | ||
*/ | ||
uint16_t scale_2_rgb_time(const uint8_t scaled_time) { | ||
const uint16_t time = scaled_time * rgb_speed_divisor * UINT8_MAX / rgb_matrix_config.speed; | ||
|
||
return time; | ||
} | ||
|
||
bool fade_in_ranged(const uint8_t time, const uint8_t range_min, const uint8_t range_max) { | ||
static const uint8_t max_delta = 1; | ||
return scaled_sin_up(time, range_min, range_max, max_delta, &(rgb_matrix_config.hsv.v)); | ||
} | ||
|
||
/** | ||
* @brief Convenience method to eventually skip the value part when setting HSV. | ||
* @details When setting HSV this includes the value/brightness. | ||
As changing brightness might interfer with fading or breathing effects, | ||
this method can skip the value part of HSV (depending on the preprocessor flag: RGB_FADE_IN). | ||
* @param[in] hue Hue | ||
* @param[in] sat Saturation | ||
* @param[in] hue Value (brightness) | ||
* @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) | ||
rgb_matrix_config.hsv.h = hue; | ||
rgb_matrix_config.hsv.s = sat; | ||
// omitting setting the value to avoid interfering with effects | ||
// rgb_matrix_config.hsv.v = val; | ||
#else | ||
rgb_matrix_sethsv_noeeprom(hue, sat, val); | ||
#endif | ||
} | ||
|
||
#if defined(RGB_FADE_IN) | ||
/** | ||
* @brief Calculates the time offset required by fade in. | ||
* @details Using an arbitrary timer any point on the sine curve might be pointed to. | ||
* The offest is calculated so that | ||
* a) the point is at the lowest point in the curve and the curve is raising | ||
* b) the point is near the current brightness (eg. fade in might be called while fading out and the lowest value has not yet been reached). | ||
* @param[in] time Current time usually represented by (usually scaled) timer | ||
* @return Offset required so that time matches the current brightness | ||
*/ | ||
uint8_t calc_fade_in_offset(const uint8_t time) { | ||
static const uint8_t max_steps = UINT8_MAX/2; | ||
static const uint8_t range_min = 0; | ||
static const uint8_t range_max = RGB_MATRIX_MAXIMUM_BRIGHTNESS; | ||
|
||
// start at the right point in the sine curve | ||
uint8_t time_offset = offset_for_time(time, PHASE_LOW); | ||
|
||
// find the right offset to match the current brightness | ||
for (int i = 1; i < max_steps; i++) { | ||
const uint8_t value = scaled_sin(time + time_offset + 1, range_min, range_max); | ||
if (in_range(value, range_min, range_max) && value < rgb_matrix_config.hsv.v) { | ||
time_offset++; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return time_offset; | ||
} | ||
|
||
/** | ||
* @brief Increases value/brightness until reaching RGB_MATRIX_MAXIMUM_BRIGHTNESS based on given timer. | ||
* @param[in] time A (usually scaled) timer | ||
* @return Returns `true` if RGB_MATRIX_MAXIMUM_BRIGHTNESS has been reached, `false` otherwise. | ||
*/ | ||
bool fade_in(const uint8_t time) { | ||
static const uint8_t range_min = 0; | ||
static const uint8_t range_max = RGB_MATRIX_MAXIMUM_BRIGHTNESS; | ||
|
||
return fade_in_ranged(time, range_min, range_max); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2021 Victor Toni (@vitoni) | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
|
||
/** | ||
* States reflecting the state of the keyboard. | ||
* Dependeing on these states various effects can set for the RGB matrix. | ||
*/ | ||
enum states { | ||
REGULAR //!< when in regular use | ||
#if defined(RGB_FADE_IN) | ||
,FADE_IN //!< when starting initially or before going back to REGULAR | ||
#endif | ||
,SUSPENDED //!< expecting to be suspended by RGB_DISABLE_TIMEOUT any time | ||
}; | ||
|
||
/** | ||
* @brief Scales down `g_rgb_timer` so that it can be used for RGB effects. | ||
* @details Usually these calculations aredone internally by some RGB effects. | ||
This method exposed to scaling so that all effects to have same timebase. If `rgb_matrix_config.speed` all effects are affected the same. | ||
* @param[in] factor The factor can be used to speed up some operations in relation to others. | ||
* @return scaled down timer taking into account the given factor | ||
* @see g_rgb_timer | ||
* @see rgb_matrix_config.speed | ||
*/ | ||
uint8_t rgb_time_2_scale_w_factor(const uint8_t factor); | ||
|
||
/** | ||
* @brief Scales down `g_rgb_timer` so that it can be used for RGB effects. | ||
* @return scaled down timer | ||
* @see rgb_time_2_scale_w_factor() | ||
*/ | ||
uint8_t rgb_time_2_scale(void); | ||
|
||
/** | ||
* @brief Inverse function to calculate time required to execute `timer` steps. | ||
* @details This method allows calculation of the time needed to execute N `timer`steps. | ||
Usefull when using a scaled down time but requiring the time needed to perform these steps. | ||
* @param[in] scaled_time scaled down timer to inverse to time | ||
* @return time corresponding to scaled down time | ||
* @see rgb_time_2_scale() | ||
*/ | ||
uint16_t scale_2_rgb_time(const uint8_t scaled_time); | ||
|
||
/** | ||
* @brief Convenience method to eventually skip the value part when setting HSV. | ||
* @details When setting HSV this includes the value/brightness. | ||
As changing brightness might interfer with fading or breathing effects, | ||
this method can skip the value part of HSV (depending on the preprocessor flag: RGB_FADE_IN). | ||
* @param[in] hue Hue | ||
* @param[in] sat Saturation | ||
* @param[in] hue Value (brightness) | ||
* @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_MATRIX_MAXIMUM_BRIGHTNESS) | ||
# if (RGB_MATRIX_MAXIMUM_BRIGHTNESS) < 1 | ||
# error "RGB_MATRIX_MAXIMUM_BRIGHTNESS must not be less than ONE" | ||
# endif | ||
# if UINT8_MAX < (RGB_MATRIX_MAXIMUM_BRIGHTNESS) | ||
# error "RGB_MATRIX_MAXIMUM_BRIGHTNESS must not be larger than UINT8_MAX" | ||
# endif | ||
# else | ||
# define RGB_MATRIX_MAXIMUM_BRIGHTNESS 200 | ||
# endif | ||
|
||
/** | ||
* @brief Calculates the time offset required by fade in. | ||
* @details Using an arbitrary timer any point on the sine curve might be pointed to. | ||
* The offset is calculated so that | ||
* a) the point is at the lowest point in the curve and the curve is raising | ||
* b) the point is near the current brightness (eg. fade in might be called while fading out and the lowest value has not yet been reached). | ||
* @param[in] time Current time usually represented by a(usually scaled) timer | ||
* @return Offset required so that time matches the current brightness | ||
*/ | ||
uint8_t calc_fade_in_offset(const uint8_t time); | ||
|
||
/** | ||
* @brief Increases value/brightness until reaching RGB_MATRIX_MAXIMUM_BRIGHTNESS based on given timer. | ||
* @param[in] time A (usually scaled) timer | ||
* @return Returns `true` if RGB_MATRIX_MAXIMUM_BRIGHTNESS has been reached, `false` otherwise. | ||
*/ | ||
bool fade_in(const uint8_t time); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SRC += \ | ||
vitoni.c \ | ||
utils.c \ | ||
rgb_matrix_effects.c |
Oops, something went wrong.