1 /* Copyright 2017 Jason Williams
2 * Copyright 2017 Jack Humbert
3 * Copyright 2018 Yiancar
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "rgb_matrix.h"
27 #include "lib/lib8tion/lib8tion.h"
29 #include "rgb_matrix_animations/solid_color_anim.h"
30 #include "rgb_matrix_animations/alpha_mods_anim.h"
31 #include "rgb_matrix_animations/dual_beacon_anim.h"
32 #include "rgb_matrix_animations/gradient_up_down_anim.h"
33 #include "rgb_matrix_animations/raindrops_anim.h"
34 #include "rgb_matrix_animations/cycle_all_anim.h"
35 #include "rgb_matrix_animations/cycle_left_right_anim.h"
36 #include "rgb_matrix_animations/cycle_up_down_anim.h"
37 #include "rgb_matrix_animations/rainbow_beacon_anim.h"
38 #include "rgb_matrix_animations/rainbow_pinwheels_anim.h"
39 #include "rgb_matrix_animations/rainbow_moving_chevron_anim.h"
40 #include "rgb_matrix_animations/jellybean_raindrops_anim.h"
41 #include "rgb_matrix_animations/typing_heatmap_anim.h"
42 #include "rgb_matrix_animations/digital_rain_anim.h"
43 #include "rgb_matrix_animations/solid_reactive_simple_anim.h"
44 #include "rgb_matrix_animations/solid_reactive_anim.h"
45 #include "rgb_matrix_animations/solid_reactive_wide.h"
46 #include "rgb_matrix_animations/solid_reactive_cross.h"
47 #include "rgb_matrix_animations/solid_reactive_nexus.h"
48 #include "rgb_matrix_animations/splash_anim.h"
49 #include "rgb_matrix_animations/solid_splash_anim.h"
50 #include "rgb_matrix_animations/breathing_anim.h"
52 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
53 #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
54 #define RGB_MATRIX_EFFECT(name, ...)
55 #ifdef RGB_MATRIX_CUSTOM_KB
56 #include "rgb_matrix_kb.inc"
58 #ifdef RGB_MATRIX_CUSTOM_USER
59 #include "rgb_matrix_user.inc"
61 #undef RGB_MATRIX_EFFECT
62 #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
65 #ifndef RGB_DISABLE_AFTER_TIMEOUT
66 #define RGB_DISABLE_AFTER_TIMEOUT 0
69 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
70 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
73 #ifndef EECONFIG_RGB_MATRIX
74 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
77 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
78 #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
79 #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
82 #if !defined(RGB_MATRIX_HUE_STEP)
83 #define RGB_MATRIX_HUE_STEP 8
86 #if !defined(RGB_MATRIX_SAT_STEP)
87 #define RGB_MATRIX_SAT_STEP 16
90 #if !defined(RGB_MATRIX_VAL_STEP)
91 #define RGB_MATRIX_VAL_STEP 16
94 #if !defined(RGB_MATRIX_SPD_STEP)
95 #define RGB_MATRIX_SPD_STEP 16
98 #if !defined(RGB_MATRIX_STARTUP_MODE)
99 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
100 #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
102 // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
103 #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
107 bool g_suspend_state = false;
109 extern led_config_t g_led_config;
110 rgb_config_t rgb_matrix_config;
112 rgb_counters_t g_rgb_counters;
113 static uint32_t rgb_counters_buffer;
115 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
116 uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
119 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
120 last_hit_t g_last_hit_tracker;
121 static last_hit_t last_hit_buffer;
122 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
124 uint32_t eeconfig_read_rgb_matrix(void) {
125 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
128 void eeconfig_update_rgb_matrix(uint32_t val) {
129 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
132 void eeconfig_update_rgb_matrix_default(void) {
133 dprintf("eeconfig_update_rgb_matrix_default\n");
134 rgb_matrix_config.enable = 1;
135 rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
136 rgb_matrix_config.hue = 0;
137 rgb_matrix_config.sat = UINT8_MAX;
138 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
139 rgb_matrix_config.speed = UINT8_MAX / 2;
140 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
143 void eeconfig_debug_rgb_matrix(void) {
144 dprintf("rgb_matrix_config eprom\n");
145 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
146 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
147 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
148 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
149 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
150 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
153 __attribute__ ((weak))
154 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
158 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
159 uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
160 uint8_t led_index = g_led_config.matrix_co[row][column];
161 if (led_index != NO_LED) {
162 led_i[led_count] = led_index;
168 void rgb_matrix_update_pwm_buffers(void) {
169 rgb_matrix_driver.flush();
172 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
173 rgb_matrix_driver.set_color(index, red, green, blue);
176 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
177 rgb_matrix_driver.set_color_all(red, green, blue);
180 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
181 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
182 uint8_t led[LED_HITS_TO_REMEMBER];
183 uint8_t led_count = 0;
185 #if defined(RGB_MATRIX_KEYRELEASES)
186 if (!record->event.pressed) {
187 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
188 g_rgb_counters.any_key_hit = 0;
190 #elif defined(RGB_MATRIX_KEYPRESSES)
191 if (record->event.pressed) {
192 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
193 g_rgb_counters.any_key_hit = 0;
195 #endif // defined(RGB_MATRIX_KEYRELEASES)
197 if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
198 memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
199 memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
200 memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
201 memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
202 last_hit_buffer.count--;
205 for(uint8_t i = 0; i < led_count; i++) {
206 uint8_t index = last_hit_buffer.count;
207 last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
208 last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
209 last_hit_buffer.index[index] = led[i];
210 last_hit_buffer.tick[index] = 0;
211 last_hit_buffer.count++;
213 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
215 #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
216 if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
217 process_rgb_matrix_typing_heatmap(record);
219 #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
224 void rgb_matrix_test(void) {
225 // Mask out bits 4 and 5
226 // Increase the factor to make the test animation slower (and reduce to make it faster)
228 switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
231 rgb_matrix_set_color_all( 20, 0, 0 );
235 rgb_matrix_set_color_all( 0, 20, 0 );
239 rgb_matrix_set_color_all( 0, 0, 20 );
243 rgb_matrix_set_color_all( 20, 20, 20 );
249 static bool rgb_matrix_none(effect_params_t* params) {
254 RGB_MATRIX_USE_LIMITS(led_min, led_max);
255 for (uint8_t i = led_min; i < led_max; i++) {
256 rgb_matrix_set_color(i, 0, 0, 0);
258 return led_max < DRIVER_LED_TOTAL;
261 static uint8_t rgb_last_enable = UINT8_MAX;
262 static uint8_t rgb_last_effect = UINT8_MAX;
263 static effect_params_t rgb_effect_params = { 0, 0xFF };
264 static rgb_task_states rgb_task_state = SYNCING;
266 static void rgb_task_timers(void) {
267 // Update double buffer timers
268 uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
269 rgb_counters_buffer = timer_read32();
270 if (g_rgb_counters.any_key_hit < UINT32_MAX) {
271 if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
272 g_rgb_counters.any_key_hit = UINT32_MAX;
274 g_rgb_counters.any_key_hit += deltaTime;
278 // Update double buffer last hit timers
279 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
280 uint8_t count = last_hit_buffer.count;
281 for (uint8_t i = 0; i < count; ++i) {
282 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
283 last_hit_buffer.count--;
286 last_hit_buffer.tick[i] += deltaTime;
288 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
291 static void rgb_task_sync(void) {
293 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
294 rgb_task_state = STARTING;
297 static void rgb_task_start(void) {
299 rgb_effect_params.iter = 0;
301 // update double buffers
302 g_rgb_counters.tick = rgb_counters_buffer;
303 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
304 g_last_hit_tracker = last_hit_buffer;
305 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
308 rgb_task_state = RENDERING;
311 static void rgb_task_render(uint8_t effect) {
312 bool rendering = false;
313 rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
315 // each effect can opt to do calculations
316 // and/or request PWM buffer updates.
318 case RGB_MATRIX_NONE:
319 rendering = rgb_matrix_none(&rgb_effect_params);
322 case RGB_MATRIX_SOLID_COLOR:
323 rendering = rgb_matrix_solid_color(&rgb_effect_params); // Max 1ms Avg 0ms
325 #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS
326 case RGB_MATRIX_ALPHAS_MODS:
327 rendering = rgb_matrix_alphas_mods(&rgb_effect_params); // Max 2ms Avg 1ms
329 #endif // DISABLE_RGB_MATRIX_ALPHAS_MODS
330 #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
331 case RGB_MATRIX_GRADIENT_UP_DOWN:
332 rendering = rgb_matrix_gradient_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
334 #endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
335 #ifndef DISABLE_RGB_MATRIX_BREATHING
336 case RGB_MATRIX_BREATHING:
337 rendering = rgb_matrix_breathing(&rgb_effect_params); // Max 1ms Avg 0ms
339 #endif // DISABLE_RGB_MATRIX_BREATHING
340 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
341 case RGB_MATRIX_CYCLE_ALL:
342 rendering = rgb_matrix_cycle_all(&rgb_effect_params); // Max 4ms Avg 3ms
344 #endif // DISABLE_RGB_MATRIX_CYCLE_ALL
345 #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
346 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
347 rendering = rgb_matrix_cycle_left_right(&rgb_effect_params); // Max 4ms Avg 3ms
349 #endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
350 #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
351 case RGB_MATRIX_CYCLE_UP_DOWN:
352 rendering = rgb_matrix_cycle_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
354 #endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
355 #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
356 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
357 rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms
359 #endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
360 #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON
361 case RGB_MATRIX_DUAL_BEACON:
362 rendering = rgb_matrix_dual_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
364 #endif // DISABLE_RGB_MATRIX_DUAL_BEACON
365 #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON
366 case RGB_MATRIX_RAINBOW_BEACON:
367 rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
369 #endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON
370 #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
371 case RGB_MATRIX_RAINBOW_PINWHEELS:
372 rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params); // Max 4ms Avg 3ms
374 #endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
375 #ifndef DISABLE_RGB_MATRIX_RAINDROPS
376 case RGB_MATRIX_RAINDROPS:
377 rendering = rgb_matrix_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
379 #endif // DISABLE_RGB_MATRIX_RAINDROPS
380 #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
381 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
382 rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
384 #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
386 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
387 #ifndef DISABLE_RGB_MATRIX_TYPING_HEATMAP
388 case RGB_MATRIX_TYPING_HEATMAP:
389 rendering = rgb_matrix_typing_heatmap(&rgb_effect_params); // Max 4ms Avg 3ms
391 #endif // DISABLE_RGB_MATRIX_TYPING_HEATMAP
392 #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
393 case RGB_MATRIX_DIGITAL_RAIN:
394 rendering = rgb_matrix_digital_rain(&rgb_effect_params); // Max 9ms Avg 8ms | this is expensive, fix it
396 #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
397 #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS
399 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
400 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE
401 case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
402 rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms
405 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE
406 case RGB_MATRIX_SOLID_REACTIVE:
407 rendering = rgb_matrix_solid_reactive(&rgb_effect_params); // Max 4ms Avg 3ms
409 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE
410 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
411 case RGB_MATRIX_SOLID_REACTIVE_WIDE:
412 rendering = rgb_matrix_solid_reactive_wide(&rgb_effect_params); // Max ?? ms Avg ?? ms
414 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
415 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
416 case RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE:
417 rendering = rgb_matrix_solid_reactive_multiwide(&rgb_effect_params); // Max ?? ms Avg ?? ms
419 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
420 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
421 case RGB_MATRIX_SOLID_REACTIVE_CROSS:
422 rendering = rgb_matrix_solid_reactive_cross(&rgb_effect_params); // Max ?? ms Avg ?? ms
424 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
425 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
426 case RGB_MATRIX_SOLID_REACTIVE_MULTICROSS:
427 rendering = rgb_matrix_solid_reactive_multicross(&rgb_effect_params); // Max ?? ms Avg ?? ms
429 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
430 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
431 case RGB_MATRIX_SOLID_REACTIVE_NEXUS:
432 rendering = rgb_matrix_solid_reactive_nexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
434 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
435 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
436 case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
437 rendering = rgb_matrix_solid_reactive_multinexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
439 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
440 #ifndef DISABLE_RGB_MATRIX_SPLASH
441 case RGB_MATRIX_SPLASH:
442 rendering = rgb_matrix_splash(&rgb_effect_params); // Max 5ms Avg 3ms
444 #endif // DISABLE_RGB_MATRIX_SPLASH
445 #ifndef DISABLE_RGB_MATRIX_MULTISPLASH
446 case RGB_MATRIX_MULTISPLASH:
447 rendering = rgb_matrix_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
449 #endif // DISABLE_RGB_MATRIX_MULTISPLASH
450 #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH
451 case RGB_MATRIX_SOLID_SPLASH:
452 rendering = rgb_matrix_solid_splash(&rgb_effect_params); // Max 5ms Avg 3ms
454 #endif // DISABLE_RGB_MATRIX_SOLID_SPLASH
455 #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
456 case RGB_MATRIX_SOLID_MULTISPLASH:
457 rendering = rgb_matrix_solid_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
459 #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
460 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
462 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
463 #define RGB_MATRIX_EFFECT(name, ...) \
464 case RGB_MATRIX_CUSTOM_##name: \
465 rendering = name(&rgb_effect_params); \
467 #ifdef RGB_MATRIX_CUSTOM_KB
468 #include "rgb_matrix_kb.inc"
470 #ifdef RGB_MATRIX_CUSTOM_USER
471 #include "rgb_matrix_user.inc"
473 #undef RGB_MATRIX_EFFECT
476 // Factory default magic value
479 rgb_task_state = FLUSHING;
484 rgb_effect_params.iter++;
488 rgb_task_state = FLUSHING;
489 if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
490 // We only need to flush once if we are RGB_MATRIX_NONE
491 rgb_task_state = SYNCING;
496 static void rgb_task_flush(uint8_t effect) {
497 // update last trackers after the first full render so we can init over several frames
498 rgb_last_effect = effect;
499 rgb_last_enable = rgb_matrix_config.enable;
501 // update pwm buffers
502 rgb_matrix_update_pwm_buffers();
505 rgb_task_state = SYNCING;
508 void rgb_matrix_task(void) {
511 // Ideally we would also stop sending zeros to the LED driver PWM buffers
512 // while suspended and just do a software shutdown. This is a cheap hack for now.
513 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
514 uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
516 switch (rgb_task_state) {
521 rgb_task_render(effect);
524 rgb_task_flush(effect);
531 if (!suspend_backlight) {
532 rgb_matrix_indicators();
536 void rgb_matrix_indicators(void) {
537 rgb_matrix_indicators_kb();
538 rgb_matrix_indicators_user();
541 __attribute__((weak))
542 void rgb_matrix_indicators_kb(void) {}
544 __attribute__((weak))
545 void rgb_matrix_indicators_user(void) {}
547 void rgb_matrix_init(void) {
548 rgb_matrix_driver.init();
550 // TODO: put the 1 second startup delay here?
552 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
553 g_last_hit_tracker.count = 0;
554 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
555 g_last_hit_tracker.tick[i] = UINT16_MAX;
558 last_hit_buffer.count = 0;
559 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
560 last_hit_buffer.tick[i] = UINT16_MAX;
562 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
564 if (!eeconfig_is_enabled()) {
565 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
567 eeconfig_update_rgb_matrix_default();
570 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
571 rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
572 if (!rgb_matrix_config.mode) {
573 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
574 eeconfig_update_rgb_matrix_default();
575 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
577 eeconfig_debug_rgb_matrix(); // display current eeprom values
580 void rgb_matrix_set_suspend_state(bool state) {
581 g_suspend_state = state;
584 void rgb_matrix_toggle(void) {
585 rgb_matrix_config.enable ^= 1;
586 rgb_task_state = STARTING;
587 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
590 void rgb_matrix_enable(void) {
591 rgb_matrix_enable_noeeprom();
592 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
595 void rgb_matrix_enable_noeeprom(void) {
596 if (!rgb_matrix_config.enable)
597 rgb_task_state = STARTING;
598 rgb_matrix_config.enable = 1;
601 void rgb_matrix_disable(void) {
602 rgb_matrix_disable_noeeprom();
603 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
606 void rgb_matrix_disable_noeeprom(void) {
607 if (rgb_matrix_config.enable)
608 rgb_task_state = STARTING;
609 rgb_matrix_config.enable = 0;
612 void rgb_matrix_step(void) {
613 rgb_matrix_config.mode++;
614 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
615 rgb_matrix_config.mode = 1;
616 rgb_task_state = STARTING;
617 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
620 void rgb_matrix_step_reverse(void) {
621 rgb_matrix_config.mode--;
622 if (rgb_matrix_config.mode < 1)
623 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
624 rgb_task_state = STARTING;
625 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
628 void rgb_matrix_increase_hue(void) {
629 rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
630 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
633 void rgb_matrix_decrease_hue(void) {
634 rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
635 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
638 void rgb_matrix_increase_sat(void) {
639 rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
640 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
643 void rgb_matrix_decrease_sat(void) {
644 rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
645 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
648 void rgb_matrix_increase_val(void) {
649 rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
650 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
651 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
652 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
655 void rgb_matrix_decrease_val(void) {
656 rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
657 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
660 void rgb_matrix_increase_speed(void) {
661 rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
662 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
665 void rgb_matrix_decrease_speed(void) {
666 rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
667 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
670 led_flags_t rgb_matrix_get_flags(void) {
671 return rgb_effect_params.flags;
674 void rgb_matrix_set_flags(led_flags_t flags) {
675 rgb_effect_params.flags = flags;
678 void rgb_matrix_mode(uint8_t mode) {
679 rgb_matrix_config.mode = mode;
680 rgb_task_state = STARTING;
681 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
684 void rgb_matrix_mode_noeeprom(uint8_t mode) {
685 rgb_matrix_config.mode = mode;
688 uint8_t rgb_matrix_get_mode(void) {
689 return rgb_matrix_config.mode;
692 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
693 rgb_matrix_sethsv_noeeprom(hue, sat, val);
694 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
697 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
698 rgb_matrix_config.hue = hue;
699 rgb_matrix_config.sat = sat;
700 rgb_matrix_config.val = val;
701 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
702 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;