Skip to content

Commit 345f630

Browse files
committed
Implement Candy Press effect
* Shift indicator hues with triwave
1 parent 9075e19 commit 345f630

3 files changed

Lines changed: 72 additions & 15 deletions

File tree

config.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@
4949
#endif
5050

5151
#ifdef RGB_MATRIX_ENABLE
52-
# define RGB_MATRIX_KEYPRESSES
5352
# define RGB_MATRIX_DEFAULT_SPD 128
5453
# define RGB_MATRIX_DEFAULT_HUE 180
55-
# define ENABLE_RGB_MATRIX_CUSTOM_CANDY_TAP
54+
# define ENABLE_RGB_MATRIX_CUSTOM_CANDY_PRESS
5655
# define ENABLE_RGB_MATRIX_CUSTOM_CANDY_MOSAIC
57-
# define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_CUSTOM_CANDY_TAP
58-
# define DEF_MODE RGB_MATRIX_CUSTOM_CANDY_TAP
56+
# define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_CUSTOM_CANDY_PRESS
57+
# define DEF_MODE RGB_MATRIX_CUSTOM_CANDY_PRESS
5958
# define CMK_MODE RGB_MATRIX_CUSTOM_CANDY_MOSAIC
59+
# if (defined(ENABLE_RGB_MATRIX_CUSTOM_CANDY_TAP) || \
60+
defined(ENABLE_RGB_MATRIX_CUSTOM_CANDY_SPLASH))
61+
# define RGB_MATRIX_KEYPRESSES
62+
# endif
6063
#endif
6164

6265
// Layout macros

features/rgb_matrix.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,28 @@ led_config_t g_led_config = {
2828

2929

3030
bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
31-
hsv_t hsv = {};
31+
hsv_t hsv = {};
32+
uint8_t time = scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed / 4, 1));
3233

3334
if (get_highest_layer(layer_state) > CMK) {
34-
// Scale active layer index to hue value
35+
// Scale active layer index to triwave hue range
3536
hsv = rgb_matrix_config.hsv;
36-
hsv.h = (get_highest_layer(layer_state) - 1) * 85;
37+
hsv.h = (triwave8(time) / 8) - ((get_highest_layer(layer_state) - 1) * 85);
3738
} else if (host_keyboard_led_state().caps_lock) {
3839
// Apply pulsing brightness to hue value
39-
hsv = (hsv_t){HSV_PINK};
40-
uint8_t time = scale16by8(g_rgb_timer, rgb_matrix_config.speed / 4);
41-
uint8_t wave = abs8(sin8(time) - 128) * 2;
42-
hsv.v = scale8(wave, hsv.v);
40+
hsv = (hsv_t){HSV_MAGENTA};
41+
hsv.v = scale8((abs8(sin8(time) - 128) * 2), hsv.v);
4342
}
4443

4544
for (uint8_t i = led_min; i < led_max; ++i) {
4645
uint8_t mods = IS_MODIFIER_FOR_LED(i);
47-
if (!mods && !hsv.v) continue;
48-
4946
if (mods) {
50-
// Combine modifier bits and scale to hue range
47+
// Combine modifier bits and scale to triwave hue range
5148
hsv = rgb_matrix_config.hsv;
52-
hsv.h = ((mods >> 4) | (mods & 0x0F)) * 17;
49+
hsv.h = (triwave8(time) / 8) - (((mods >> 4) | (mods & 0x0F)) * 17);
50+
} else if (!hsv.v) {
51+
// Skip LEDs if HSV value is zero
52+
continue;
5353
}
5454
rgb_t rgb = hsv_to_rgb(hsv);
5555
rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);

features/rgb_matrix_user.inc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,60 @@ static bool CANDY_SPLASH(effect_params_t* params) {
9595
#endif // ENABLE_RGB_MATRIX_CUSTOM_CANDY_SPLASH
9696

9797

98+
#ifdef ENABLE_RGB_MATRIX_CUSTOM_CANDY_PRESS
99+
RGB_MATRIX_EFFECT(CANDY_PRESS)
100+
# ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
101+
# include "matrix.h"
102+
103+
// Store current brightness per LED (0 = dark, 255 = full)
104+
static uint8_t candy_press_v[RGB_MATRIX_LED_COUNT];
105+
static uint32_t candy_press_timer;
106+
107+
static bool CANDY_PRESS(effect_params_t* params) {
108+
if (params->iter == 0) {
109+
if (params->init) memset(candy_press_v, 0, sizeof(candy_press_v));
110+
111+
// On the first iteration, calculate brightness decay based on time scale since last update
112+
uint16_t delta = (uint16_t)MIN(g_rgb_timer - candy_press_timer, 0xFFFFu);
113+
uint8_t decay = (uint8_t)MIN(scale16by8(delta, qadd8(rgb_matrix_config.speed, 1)), 255u);
114+
for (uint8_t i = 0; i < RGB_MATRIX_LED_COUNT; ++i) {
115+
if (candy_press_v[i]) candy_press_v[i] = qsub8(candy_press_v[i], decay);
116+
}
117+
candy_press_timer = g_rgb_timer;
118+
119+
// Scan the key matrix and set brightness to 255 for currently pressed keys
120+
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
121+
matrix_row_t row_state = matrix_get_row(row);
122+
if (!row_state) continue;
123+
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
124+
if (!((row_state >> col) & 1)) continue;
125+
uint8_t led = g_led_config.matrix_co[row][col];
126+
if (led < RGB_MATRIX_LED_COUNT) candy_press_v[led] = 255;
127+
}
128+
}
129+
}
130+
131+
// Apply a triwave colour effect to all LEDs with brightness based on current
132+
// candy_press_v value, creating a pulsing effect that responds to key presses
133+
RGB_MATRIX_USE_LIMITS(led_min, led_max);
134+
for (uint8_t i = led_min; i < led_max; i++) {
135+
if (!candy_press_v[i]) continue;
136+
RGB_MATRIX_TEST_LED_FLAGS();
137+
uint8_t now = scale16by8(g_rgb_timer, qadd8(rgb_matrix_config.speed / 16, 1));
138+
hsv_t hsv = rgb_matrix_config.hsv;
139+
hsv.h = (triwave8(now) / 4) + (g_rgb_timer / 1024);
140+
hsv.v = scale8(candy_press_v[i], hsv.v);
141+
rgb_t rgb = rgb_matrix_hsv_to_rgb(hsv);
142+
rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
143+
}
144+
145+
return rgb_matrix_check_finished_leds(led_max);
146+
}
147+
148+
# endif // RGB_MATRIX_CUSTOM_EFFECT_IMPLS
149+
#endif // ENABLE_RGB_MATRIX_CUSTOM_CANDY_PRESS
150+
151+
98152
#ifdef ENABLE_RGB_MATRIX_CUSTOM_CANDY_PULSE
99153
RGB_MATRIX_EFFECT(CANDY_PULSE)
100154
# ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS

0 commit comments

Comments
 (0)