aboutsummaryrefslogtreecommitdiff
path: root/users/curry/rgb_stuff.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/curry/rgb_stuff.c')
-rw-r--r--users/curry/rgb_stuff.c471
1 files changed, 471 insertions, 0 deletions
diff --git a/users/curry/rgb_stuff.c b/users/curry/rgb_stuff.c
new file mode 100644
index 000000000..1129f70be
--- /dev/null
+++ b/users/curry/rgb_stuff.c
@@ -0,0 +1,471 @@
1#include "curry.h"
2#include "rgb_stuff.h"
3#include "eeprom.h"
4
5#if defined(RGBLIGHT_ENABLE)
6extern rgblight_config_t rgblight_config;
7bool has_initialized;
8
9void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, index); }
10#endif // RGBLIGHT_ENABLE
11
12#if defined(RGB_MATRIX_ENABLE)
13static uint32_t hypno_timer;
14# if defined(SPLIT_KEYBOARD) || defined(KEYBOARD_ergodox_ez) || defined(KEYBOARD_crkbd)
15# define RGB_MATRIX_REST_MODE RGB_MATRIX_CYCLE_OUT_IN_DUAL
16# else
17# define RGB_MATRIX_REST_MODE RGB_MATRIX_CYCLE_OUT_IN
18# endif
19
20void suspend_power_down_keymap(void) { rgb_matrix_set_suspend_state(true); }
21
22void suspend_wakeup_init_keymap(void) { rgb_matrix_set_suspend_state(false); }
23
24void check_default_layer(uint8_t mode, uint8_t type) {
25 switch (get_highest_layer(default_layer_state)) {
26 case _QWERTY:
27 rgb_matrix_layer_helper(HSV_CYAN, mode, rgb_matrix_config.speed, type);
28 break;
29 case _COLEMAK:
30 rgb_matrix_layer_helper(HSV_MAGENTA, mode, rgb_matrix_config.speed, type);
31 break;
32 case _DVORAK:
33 rgb_matrix_layer_helper(HSV_SPRINGGREEN, mode, rgb_matrix_config.speed, type);
34 break;
35 }
36}
37
38void rgb_matrix_indicators_user(void) {
39 if (userspace_config.rgb_layer_change &&
40# ifdef RGB_DISABLE_WHEN_USB_SUSPENDED
41 !g_suspend_state &&
42# endif
43# if defined(RGBLIGHT_ENABLE)
44 (!rgblight_config.enable && rgb_matrix_config.enable)
45# else
46 rgb_matrix_config.enable
47# endif
48 ) {
49 switch (get_highest_layer(layer_state)) {
50 case _RAISE:
51 rgb_matrix_layer_helper(HSV_YELLOW, 0, rgb_matrix_config.speed, LED_FLAG_UNDERGLOW);
52 break;
53 case _LOWER:
54 rgb_matrix_layer_helper(HSV_GREEN, 0, rgb_matrix_config.speed, LED_FLAG_UNDERGLOW);
55 break;
56 case _ADJUST:
57 rgb_matrix_layer_helper(HSV_RED, 0, rgb_matrix_config.speed, LED_FLAG_UNDERGLOW);
58 break;
59 default: {
60 check_default_layer(IS_LAYER_ON(_MODS), LED_FLAG_UNDERGLOW);
61 break;
62 }
63 }
64 check_default_layer(0, LED_FLAG_MODIFIER);
65 }
66}
67#endif
68
69/* Custom indicators for modifiers.
70 * This allows for certain lights to be lit up, based on what mods are active, giving some visual feedback.
71 * This is especially useful for One Shot Mods, since it's not always obvious if they're still lit up.
72 */
73#ifdef RGBLIGHT_ENABLE
74# ifdef INDICATOR_LIGHTS
75void set_rgb_indicators(uint8_t this_mod, uint8_t this_led, uint8_t this_osm) {
76 if (userspace_config.rgb_layer_change && get_highest_layer(layer_state) == 0) {
77 if ((this_mod | this_osm) & MOD_MASK_SHIFT || this_led & (1 << USB_LED_CAPS_LOCK)) {
78# ifdef SHFT_LED1
79 rgblight_sethsv_at(120, 255, 255, SHFT_LED1);
80# endif // SHFT_LED1
81# ifdef SHFT_LED2
82 rgblight_sethsv_at(120, 255, 255, SHFT_LED2);
83# endif // SHFT_LED2
84 } else {
85# ifdef SHFT_LED1
86 rgblight_sethsv_default_helper(SHFT_LED1);
87# endif // SHFT_LED1
88# ifdef SHFT_LED2
89 rgblight_sethsv_default_helper(SHFT_LED2);
90# endif // SHFT_LED2
91 }
92 if ((this_mod | this_osm) & MOD_MASK_CTRL) {
93# ifdef CTRL_LED1
94 rgblight_sethsv_at(0, 255, 255, CTRL_LED1);
95# endif // CTRL_LED1
96# ifdef CTRL_LED2
97 rgblight_sethsv_at(0, 255, 255, CTRL_LED2);
98# endif // CTRL_LED2
99 } else {
100# ifdef CTRL_LED1
101 rgblight_sethsv_default_helper(CTRL_LED1);
102# endif // CTRL_LED1
103# ifdef CTRL_LED2
104 rgblight_sethsv_default_helper(CTRL_LED2);
105# endif // CTRL_LED2
106 }
107 if ((this_mod | this_osm) & MOD_MASK_GUI) {
108# ifdef GUI_LED1
109 rgblight_sethsv_at(51, 255, 255, GUI_LED1);
110# endif // GUI_LED1
111# ifdef GUI_LED2
112 rgblight_sethsv_at(51, 255, 255, GUI_LED2);
113# endif // GUI_LED2
114 } else {
115# ifdef GUI_LED1
116 rgblight_sethsv_default_helper(GUI_LED1);
117# endif // GUI_LED1
118# ifdef GUI_LED2
119 rgblight_sethsv_default_helper(GUI_LED2);
120# endif // GUI_LED2
121 }
122 if ((this_mod | this_osm) & MOD_MASK_ALT) {
123# ifdef ALT_LED1
124 rgblight_sethsv_at(240, 255, 255, ALT_LED1);
125# endif // ALT_LED1
126# ifdef GUI_LED2
127 rgblight_sethsv_at(240, 255, 255, ALT_LED2);
128# endif // GUI_LED2
129 } else {
130# ifdef GUI_LED1
131 rgblight_sethsv_default_helper(ALT_LED1);
132# endif // GUI_LED1
133# ifdef GUI_LED2
134 rgblight_sethsv_default_helper(ALT_LED2);
135# endif // GUI_LED2
136 }
137 }
138}
139
140/* Function for the indicators */
141void matrix_scan_indicator(void) {
142 if (has_initialized) {
143 set_rgb_indicators(get_mods(), host_keyboard_leds(), get_oneshot_mods());
144 }
145}
146# endif // INDICATOR_LIGHTS
147
148# ifdef RGBLIGHT_TWINKLE
149static rgblight_fadeout lights[RGBLED_NUM];
150
151__attribute__((weak)) bool rgblight_twinkle_is_led_used_keymap(uint8_t index) { return false; }
152
153/* This function checks for used LEDs. This way, collisions don't occur and cause weird rendering */
154bool rgblight_twinkle_is_led_used(uint8_t index) {
155 switch (index) {
156# ifdef INDICATOR_LIGHTS
157# ifdef SHFT_LED1
158 case SHFT_LED1:
159 return true;
160# endif // SHFT_LED1
161# ifdef SHFT_LED2
162 case SHFT_LED2:
163 return true;
164# endif // SHFT_LED2
165# ifdef CTRL_LED1
166 case CTRL_LED1:
167 return true;
168# endif // CTRL_LED1
169# ifdef CTRL_LED2
170 case CTRL_LED2:
171 return true;
172# endif // CTRL_LED2
173# ifdef GUI_LED1
174 case GUI_LED1:
175 return true;
176# endif // GUI_LED1
177# ifdef GUI_LED2
178 case GUI_LED2:
179 return true;
180# endif // GUI_LED2
181# ifdef ALT_LED1
182 case ALT_LED1:
183 return true;
184# endif // ALT_LED1
185# ifdef ALT_LED2
186 case ALT_LED2:
187 return true;
188# endif // ALT_LED2
189# endif // INDICATOR_LIGHTS
190 default:
191 return rgblight_twinkle_is_led_used_keymap(index);
192 }
193}
194
195/* Handler for fading/twinkling effect */
196void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
197 bool litup = false;
198 for (uint8_t light_index = 0; light_index < RGBLED_NUM; ++light_index) {
199 if (lights[light_index].enabled && timer_elapsed(lights[light_index].timer) > 10) {
200 rgblight_fadeout *light = &lights[light_index];
201 litup = true;
202
203 if (light->life) {
204 light->life -= 1;
205 if (get_highest_layer(layer_state) == 0) {
206 sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
207 }
208 light->timer = timer_read();
209 } else {
210 if (light->enabled && get_highest_layer(layer_state) == 0) {
211 rgblight_sethsv_default_helper(light_index);
212 }
213 litup = light->enabled = false;
214 }
215 }
216 }
217 if (litup && get_highest_layer(layer_state) == 0) {
218 rgblight_set();
219 }
220}
221
222/* Triggers a LED to fade/twinkle.
223 * This function handles the selection of the LED and prepres for it to be used.
224 */
225void start_rgb_light(void) {
226 uint8_t indices[RGBLED_NUM];
227 uint8_t indices_count = 0;
228 uint8_t min_life = 0xFF;
229 uint8_t min_life_index = -1;
230 for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
231 if (rgblight_twinkle_is_led_used(index)) {
232 continue;
233 }
234 if (lights[index].enabled) {
235 if (min_life_index == -1 || lights[index].life < min_life) {
236 min_life = lights[index].life;
237 min_life_index = index;
238 }
239 continue;
240 }
241
242 indices[indices_count] = index;
243 ++indices_count;
244 }
245
246 uint8_t light_index;
247 if (!indices_count) {
248 light_index = min_life_index;
249 } else {
250 light_index = indices[rand() % indices_count];
251 }
252
253 rgblight_fadeout *light = &lights[light_index];
254 light->enabled = true;
255 light->timer = timer_read();
256 light->life = 0xC0 + rand() % 0x40;
257
258 light->hue = rgblight_config.hue + (rand() % 0xB4) - 0x54;
259
260 rgblight_sethsv_at(light->hue, 255, light->life, light_index);
261}
262# endif
263#endif // RGBLIGHT_ENABLE
264
265bool process_record_user_rgb(uint16_t keycode, keyrecord_t *record) {
266 uint16_t temp_keycode = keycode;
267 // Filter out the actual keycode from MT and LT keys.
268 if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
269 temp_keycode &= 0xFF;
270 }
271
272#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
273 hypno_timer = timer_read32();
274 if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_REST_MODE) {
275 rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP);
276 }
277#endif
278
279 switch (temp_keycode) {
280#ifdef RGBLIGHT_TWINKLE
281 case KC_A ... KC_SLASH:
282 case KC_F1 ... KC_F12:
283 case KC_INSERT ... KC_UP:
284 case KC_KP_SLASH ... KC_KP_DOT:
285 case KC_F13 ... KC_F24:
286 case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
287 if (record->event.pressed) {
288 start_rgb_light();
289 }
290 break;
291#endif // RGBLIGHT_TWINKLE
292 case KC_RGB_T: // This allows me to use underglow as layer indication, or as normal
293#if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
294 if (record->event.pressed) {
295 userspace_config.rgb_layer_change ^= 1;
296 dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
297 eeconfig_update_user(userspace_config.raw);
298 if (userspace_config.rgb_layer_change) {
299 layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
300 }
301 }
302#endif // RGBLIGHT_ENABLE
303 break;
304 case RGB_IDL: // This allows me to use underglow as layer indication, or as normal
305#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
306 if (record->event.pressed) {
307 userspace_config.rgb_matrix_idle_anim ^= 1;
308 dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
309 eeconfig_update_user(userspace_config.raw);
310 if (userspace_config.rgb_matrix_idle_anim) {
311 rgb_matrix_mode_noeeprom(RGB_MATRIX_TYPING_HEATMAP);
312 }
313 }
314#endif
315 break;
316 case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // quantum_keycodes.h L400 for definitions
317 if (record->event.pressed) {
318 bool is_eeprom_updated = false;
319#ifdef RGBLIGHT_ENABLE
320 // This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
321 if (userspace_config.rgb_layer_change) {
322 userspace_config.rgb_layer_change = false;
323 dprintf("rgblight layer change [EEPROM]: %u\n", userspace_config.rgb_layer_change);
324 is_eeprom_updated = true;
325 }
326#endif
327#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
328 if (userspace_config.rgb_matrix_idle_anim) {
329 userspace_config.rgb_matrix_idle_anim = false;
330 dprintf("RGB Matrix Idle Animation [EEPROM]: %u\n", userspace_config.rgb_matrix_idle_anim);
331 is_eeprom_updated = true;
332 }
333#endif
334 if (is_eeprom_updated) {
335 eeconfig_update_user(userspace_config.raw);
336 }
337 }
338 break;
339 }
340 return true;
341}
342
343void keyboard_post_init_rgb(void) {
344#if defined(RGBLIGHT_ENABLE)
345# if defined(RGBLIGHT_STARTUP_ANIMATION)
346 bool is_enabled = rgblight_config.enable;
347 if (userspace_config.rgb_layer_change) {
348 rgblight_enable_noeeprom();
349 }
350 if (rgblight_config.enable) {
351 layer_state_set_user(layer_state);
352 uint16_t old_hue = rgblight_config.hue;
353 rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
354 for (uint16_t i = 255; i > 0; i--) {
355 rgblight_sethsv_noeeprom((i + old_hue) % 255, 255, 255);
356 matrix_scan();
357 wait_ms(10);
358 }
359 }
360 if (!is_enabled) {
361 rgblight_disable_noeeprom();
362 }
363
364# endif
365 layer_state_set_user(layer_state);
366#endif
367#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
368 if (userspace_config.rgb_matrix_idle_anim) {
369 rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
370 }
371#endif
372}
373
374void matrix_scan_rgb(void) {
375#ifdef RGBLIGHT_ENABLE
376# ifdef RGBLIGHT_TWINKLE
377 scan_rgblight_fadeout();
378# endif // RGBLIGHT_ENABLE
379
380# ifdef INDICATOR_LIGHTS
381 matrix_scan_indicator();
382# endif
383#endif
384
385#if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS)
386 if (userspace_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == RGB_MATRIX_TYPING_HEATMAP && timer_elapsed32(hypno_timer) > 15000) {
387 rgb_matrix_mode_noeeprom(RGB_MATRIX_REST_MODE);
388 }
389#endif
390}
391
392#ifdef RGBLIGHT_ENABLE
393void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
394 rgblight_sethsv_noeeprom(hue, sat, val);
395 wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
396 rgblight_mode_noeeprom(mode);
397}
398#endif
399
400layer_state_t layer_state_set_rgb(layer_state_t state) {
401#ifdef RGBLIGHT_ENABLE
402 if (userspace_config.rgb_layer_change) {
403 switch (get_highest_layer(state)) {
404 case _RAISE:
405 rgblight_set_hsv_and_mode(HSV_YELLOW, RGBLIGHT_MODE_BREATHING + 3);
406 break;
407 case _LOWER:
408 rgblight_set_hsv_and_mode(HSV_GREEN, RGBLIGHT_MODE_BREATHING + 3);
409 break;
410 case _ADJUST:
411 rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_KNIGHT + 2);
412 break;
413 default: // for any other layers, or the default layer
414 {
415 uint8_t mode = get_highest_layer(state) == _MODS ? RGBLIGHT_MODE_BREATHING : RGBLIGHT_MODE_STATIC_LIGHT;
416 switch (get_highest_layer(default_layer_state)) {
417 case _COLEMAK:
418 rgblight_set_hsv_and_mode(HSV_MAGENTA, mode);
419 break;
420 case _DVORAK:
421 rgblight_set_hsv_and_mode(HSV_SPRINGGREEN, mode);
422 break;
423 default:
424 rgblight_set_hsv_and_mode(HSV_CYAN, mode);
425 break;
426 }
427 break;
428 }
429 }
430 }
431#endif // RGBLIGHT_ENABLE
432
433 return state;
434}
435
436#ifdef RGB_MATRIX_ENABLE
437# include "lib/lib8tion/lib8tion.h"
438extern led_config_t g_led_config;
439
440void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode, uint8_t speed, uint8_t led_type) {
441 HSV hsv = {hue, sat, val};
442 if (hsv.v > rgb_matrix_config.hsv.v) {
443 hsv.v = rgb_matrix_config.hsv.v;
444 }
445
446 switch (mode) {
447 case 1: // breathing
448 {
449 uint16_t time = scale16by8(g_rgb_counters.tick, speed / 8);
450 hsv.v = scale8(abs8(sin8(time) - 128) * 2, hsv.v);
451 RGB rgb = hsv_to_rgb(hsv);
452 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
453 if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
454 rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
455 }
456 }
457 break;
458 }
459 default: // Solid Color
460 {
461 RGB rgb = hsv_to_rgb(hsv);
462 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
463 if (HAS_FLAGS(g_led_config.flags[i], led_type)) {
464 rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
465 }
466 }
467 break;
468 }
469 }
470}
471#endif