]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgb_matrix.c
RGB Matrix Typing Heatmap (#5786)
[qmk_firmware.git] / quantum / rgb_matrix.c
1 /* Copyright 2017 Jason Williams
2  * Copyright 2017 Jack Humbert
3  * Copyright 2018 Yiancar
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19
20 #include "rgb_matrix.h"
21 #include "progmem.h"
22 #include "config.h"
23 #include "eeprom.h"
24 #include <string.h>
25 #include <math.h>
26
27 #include "lib/lib8tion/lib8tion.h"
28
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"
51
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"
57     #endif
58     #ifdef RGB_MATRIX_CUSTOM_USER
59       #include "rgb_matrix_user.inc"
60     #endif
61     #undef RGB_MATRIX_EFFECT
62   #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
63 #endif
64
65 #ifndef RGB_DISABLE_AFTER_TIMEOUT
66   #define RGB_DISABLE_AFTER_TIMEOUT 0
67 #endif
68
69 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
70   #define RGB_DISABLE_WHEN_USB_SUSPENDED false
71 #endif
72
73 #ifndef EECONFIG_RGB_MATRIX
74   #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
75 #endif
76
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
80 #endif
81
82 #if !defined(RGB_MATRIX_HUE_STEP)
83   #define RGB_MATRIX_HUE_STEP 8
84 #endif
85
86 #if !defined(RGB_MATRIX_SAT_STEP)
87   #define RGB_MATRIX_SAT_STEP 16
88 #endif
89
90 #if !defined(RGB_MATRIX_VAL_STEP)
91   #define RGB_MATRIX_VAL_STEP 16
92 #endif
93
94 #if !defined(RGB_MATRIX_SPD_STEP)
95   #define RGB_MATRIX_SPD_STEP 16
96 #endif
97
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
101   #else
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
104   #endif
105 #endif
106
107 bool g_suspend_state = false;
108
109 extern led_config_t g_led_config;
110 rgb_config_t rgb_matrix_config;
111
112 rgb_counters_t g_rgb_counters;
113 static uint32_t rgb_counters_buffer;
114
115 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
116 uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
117 #endif
118
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
123
124 uint32_t eeconfig_read_rgb_matrix(void) {
125   return eeprom_read_dword(EECONFIG_RGB_MATRIX);
126 }
127
128 void eeconfig_update_rgb_matrix(uint32_t val) {
129   eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
130 }
131
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);
141 }
142
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);
151 }
152
153 __attribute__ ((weak))
154 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
155   return 0;
156 }
157
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;
163     led_count++;
164   }
165   return led_count;
166 }
167
168 void rgb_matrix_update_pwm_buffers(void) {
169   rgb_matrix_driver.flush();
170 }
171
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);
174 }
175
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);
178 }
179
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;
184
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;
189   }
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;
194   }
195 #endif // defined(RGB_MATRIX_KEYRELEASES)
196
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--;
203   }
204
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++;
212   }
213 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
214
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);
218   }
219 #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
220
221   return true;
222 }
223
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)
227   uint8_t factor = 10;
228   switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
229   {
230     case 0: {
231       rgb_matrix_set_color_all( 20, 0, 0 );
232       break;
233     }
234     case 1: {
235       rgb_matrix_set_color_all( 0, 20, 0 );
236       break;
237     }
238     case 2: {
239       rgb_matrix_set_color_all( 0, 0, 20 );
240       break;
241     }
242     case 3: {
243       rgb_matrix_set_color_all( 20, 20, 20 );
244       break;
245     }
246   }
247 }
248
249 static bool rgb_matrix_none(effect_params_t* params) {
250   if (!params->init) {
251     return false;
252   }
253
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);
257   }
258   return led_max < DRIVER_LED_TOTAL;
259 }
260
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;
265
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;
273     } else {
274       g_rgb_counters.any_key_hit += deltaTime;
275     }
276   }
277
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--;
284       continue;
285     }
286     last_hit_buffer.tick[i] += deltaTime;
287   }
288 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
289 }
290
291 static void rgb_task_sync(void) {
292   // next task
293   if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
294     rgb_task_state = STARTING;
295 }
296
297 static void rgb_task_start(void) {
298   // reset iter
299   rgb_effect_params.iter = 0;
300
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
306
307   // next task
308   rgb_task_state = RENDERING;
309 }
310
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);
314
315   // each effect can opt to do calculations
316   // and/or request PWM buffer updates.
317   switch (effect) {
318     case RGB_MATRIX_NONE:
319       rendering = rgb_matrix_none(&rgb_effect_params);
320       break;
321
322     case RGB_MATRIX_SOLID_COLOR:
323       rendering = rgb_matrix_solid_color(&rgb_effect_params);           // Max 1ms Avg 0ms
324       break;
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
328       break;
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
333       break;
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
338       break;
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
343       break;
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
348       break;
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
353       break;
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
358       break;
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
363       break;
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
368       break;
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
373       break;
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
378       break;
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
383       break;
384 #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
385
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
390       break;
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
395       break;
396 #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
397 #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS
398
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
403       break;
404 #endif
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
408       break;
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
413       break;
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
418       break;
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
423       break;
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
428       break;
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
433       break;
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
438       break;
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
443       break;
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
448       break;
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
453       break;
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
458       break;
459 #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
460 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
461
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); \
466       break;
467   #ifdef RGB_MATRIX_CUSTOM_KB
468     #include "rgb_matrix_kb.inc"
469   #endif
470   #ifdef RGB_MATRIX_CUSTOM_USER
471     #include "rgb_matrix_user.inc"
472   #endif
473   #undef RGB_MATRIX_EFFECT
474 #endif
475
476     // Factory default magic value
477     case UINT8_MAX: {
478         rgb_matrix_test();
479         rgb_task_state = FLUSHING;
480       }
481       return;
482   }
483
484   rgb_effect_params.iter++;
485
486   // next task
487   if (!rendering) {
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;
492     }
493   }
494 }
495
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;
500
501   // update pwm buffers
502   rgb_matrix_update_pwm_buffers();
503
504   // next task
505   rgb_task_state = SYNCING;
506 }
507
508 void rgb_matrix_task(void) {
509   rgb_task_timers();
510
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;
515
516   switch (rgb_task_state) {
517     case STARTING:
518       rgb_task_start();
519       break;
520     case RENDERING:
521       rgb_task_render(effect);
522       break;
523     case FLUSHING:
524       rgb_task_flush(effect);
525       break;
526     case SYNCING:
527       rgb_task_sync();
528       break;
529   }
530
531   if (!suspend_backlight) {
532     rgb_matrix_indicators();
533   }
534 }
535
536 void rgb_matrix_indicators(void) {
537   rgb_matrix_indicators_kb();
538   rgb_matrix_indicators_user();
539 }
540
541 __attribute__((weak))
542 void rgb_matrix_indicators_kb(void) {}
543
544 __attribute__((weak))
545 void rgb_matrix_indicators_user(void) {}
546
547 void rgb_matrix_init(void) {
548   rgb_matrix_driver.init();
549
550   // TODO: put the 1 second startup delay here?
551
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;
556   }
557
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;
561   }
562 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
563
564   if (!eeconfig_is_enabled()) {
565     dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
566     eeconfig_init();
567     eeconfig_update_rgb_matrix_default();
568   }
569
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();
576   }
577   eeconfig_debug_rgb_matrix(); // display current eeprom values
578 }
579
580 void rgb_matrix_set_suspend_state(bool state) {
581   g_suspend_state = state;
582 }
583
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);
588 }
589
590 void rgb_matrix_enable(void) {
591   rgb_matrix_enable_noeeprom();
592   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
593 }
594
595 void rgb_matrix_enable_noeeprom(void) {
596   if (!rgb_matrix_config.enable)
597     rgb_task_state = STARTING;
598   rgb_matrix_config.enable = 1;
599 }
600
601 void rgb_matrix_disable(void) {
602   rgb_matrix_disable_noeeprom();
603   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
604 }
605
606 void rgb_matrix_disable_noeeprom(void) {
607   if (rgb_matrix_config.enable)
608     rgb_task_state = STARTING;
609   rgb_matrix_config.enable = 0;
610 }
611
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);
618 }
619
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);
626 }
627
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);
631 }
632
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);
636 }
637
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);
641 }
642
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);
646 }
647
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);
653 }
654
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);
658 }
659
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
663 }
664
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
668 }
669
670 led_flags_t rgb_matrix_get_flags(void) {
671   return rgb_effect_params.flags;
672 }
673
674 void rgb_matrix_set_flags(led_flags_t flags) {
675   rgb_effect_params.flags = flags;
676 }
677
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);
682 }
683
684 void rgb_matrix_mode_noeeprom(uint8_t mode) {
685   rgb_matrix_config.mode = mode;
686 }
687
688 uint8_t rgb_matrix_get_mode(void) {
689   return rgb_matrix_config.mode;
690 }
691
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);
695 }
696
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;
703 }