]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgb_matrix.c
rgb_led struct conversion (aka: Per led (key) type rgb matrix effects - part 2) ...
[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/digital_rain_anim.h"
42 #include "rgb_matrix_animations/solid_reactive_simple_anim.h"
43 #include "rgb_matrix_animations/solid_reactive_anim.h"
44 #include "rgb_matrix_animations/solid_reactive_wide.h"
45 #include "rgb_matrix_animations/solid_reactive_cross.h"
46 #include "rgb_matrix_animations/solid_reactive_nexus.h"
47 #include "rgb_matrix_animations/splash_anim.h"
48 #include "rgb_matrix_animations/solid_splash_anim.h"
49 #include "rgb_matrix_animations/breathing_anim.h"
50
51 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
52   #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
53     #define RGB_MATRIX_EFFECT(name, ...)
54     #ifdef RGB_MATRIX_CUSTOM_KB
55       #include "rgb_matrix_kb.inc"
56     #endif
57     #ifdef RGB_MATRIX_CUSTOM_USER
58       #include "rgb_matrix_user.inc"
59     #endif
60     #undef RGB_MATRIX_EFFECT
61   #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
62 #endif
63
64 #ifndef RGB_DISABLE_AFTER_TIMEOUT
65   #define RGB_DISABLE_AFTER_TIMEOUT 0
66 #endif
67
68 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
69   #define RGB_DISABLE_WHEN_USB_SUSPENDED false
70 #endif
71
72 #ifndef EECONFIG_RGB_MATRIX
73   #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
74 #endif
75
76 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
77   #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
78   #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
79 #endif
80
81 #if !defined(RGB_MATRIX_HUE_STEP)
82   #define RGB_MATRIX_HUE_STEP 8
83 #endif
84
85 #if !defined(RGB_MATRIX_SAT_STEP)
86   #define RGB_MATRIX_SAT_STEP 16
87 #endif
88
89 #if !defined(RGB_MATRIX_VAL_STEP)
90   #define RGB_MATRIX_VAL_STEP 16
91 #endif
92
93 #if !defined(RGB_MATRIX_SPD_STEP)
94   #define RGB_MATRIX_SPD_STEP 16
95 #endif
96
97 #if !defined(RGB_MATRIX_STARTUP_MODE)
98   #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
99     #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
100   #else
101     // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
102     #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
103   #endif
104 #endif
105
106 bool g_suspend_state = false;
107
108 extern led_config_t g_led_config;
109 rgb_config_t rgb_matrix_config;
110
111 rgb_counters_t g_rgb_counters;
112 static uint32_t rgb_counters_buffer;
113
114 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
115   last_hit_t g_last_hit_tracker;
116   static last_hit_t last_hit_buffer;
117 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
118
119 uint32_t eeconfig_read_rgb_matrix(void) {
120   return eeprom_read_dword(EECONFIG_RGB_MATRIX);
121 }
122
123 void eeconfig_update_rgb_matrix(uint32_t val) {
124   eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
125 }
126
127 void eeconfig_update_rgb_matrix_default(void) {
128   dprintf("eeconfig_update_rgb_matrix_default\n");
129   rgb_matrix_config.enable = 1;
130   rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
131   rgb_matrix_config.hue = 0;
132   rgb_matrix_config.sat = UINT8_MAX;
133   rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
134   rgb_matrix_config.speed = UINT8_MAX / 2;
135   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
136 }
137
138 void eeconfig_debug_rgb_matrix(void) {
139   dprintf("rgb_matrix_config eprom\n");
140   dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
141   dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
142   dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
143   dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
144   dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
145   dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
146 }
147
148 __attribute__ ((weak))
149 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
150   return 0;
151 }
152
153 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
154   uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
155   uint8_t led_index = g_led_config.matrix_co[row][column];
156   if (led_index != NO_LED) {
157     led_i[led_count] = led_index;
158     led_count++;
159   }
160   return led_count;
161 }
162
163 void rgb_matrix_update_pwm_buffers(void) {
164   rgb_matrix_driver.flush();
165 }
166
167 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
168   rgb_matrix_driver.set_color(index, red, green, blue);
169 }
170
171 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
172   rgb_matrix_driver.set_color_all(red, green, blue);
173 }
174
175 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
176 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
177   uint8_t led[LED_HITS_TO_REMEMBER];
178   uint8_t led_count = 0;
179
180 #if defined(RGB_MATRIX_KEYRELEASES)
181   if (!record->event.pressed) {
182     led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
183     g_rgb_counters.any_key_hit = 0;
184   }
185 #elif defined(RGB_MATRIX_KEYPRESSES)
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 #endif // defined(RGB_MATRIX_KEYRELEASES)
191
192   if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
193     memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
194     memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
195     memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
196     memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
197     last_hit_buffer.count--;
198   }
199
200   for(uint8_t i = 0; i < led_count; i++) {
201     uint8_t index = last_hit_buffer.count;
202     last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
203     last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
204     last_hit_buffer.index[index] = led[i];
205     last_hit_buffer.tick[index] = 0;
206     last_hit_buffer.count++;
207   }
208 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
209   return true;
210 }
211
212 void rgb_matrix_test(void) {
213   // Mask out bits 4 and 5
214   // Increase the factor to make the test animation slower (and reduce to make it faster)
215   uint8_t factor = 10;
216   switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
217   {
218     case 0: {
219       rgb_matrix_set_color_all( 20, 0, 0 );
220       break;
221     }
222     case 1: {
223       rgb_matrix_set_color_all( 0, 20, 0 );
224       break;
225     }
226     case 2: {
227       rgb_matrix_set_color_all( 0, 0, 20 );
228       break;
229     }
230     case 3: {
231       rgb_matrix_set_color_all( 20, 20, 20 );
232       break;
233     }
234   }
235 }
236
237 static bool rgb_matrix_none(effect_params_t* params) {
238   if (!params->init) {
239     return false;
240   }
241
242   RGB_MATRIX_USE_LIMITS(led_min, led_max);
243   for (uint8_t i = led_min; i < led_max; i++) {
244     rgb_matrix_set_color(i, 0, 0, 0);
245   }
246   return led_max < DRIVER_LED_TOTAL;
247 }
248
249 static uint8_t rgb_last_enable = UINT8_MAX;
250 static uint8_t rgb_last_effect = UINT8_MAX;
251 static effect_params_t rgb_effect_params = { 0, 0xFF };
252 static rgb_task_states rgb_task_state = SYNCING;
253
254 static void rgb_task_timers(void) {
255   // Update double buffer timers
256   uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
257   rgb_counters_buffer = timer_read32();
258   if (g_rgb_counters.any_key_hit < UINT32_MAX) {
259     if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
260       g_rgb_counters.any_key_hit = UINT32_MAX;
261     } else {
262       g_rgb_counters.any_key_hit += deltaTime;
263     }
264   }
265
266   // Update double buffer last hit timers
267 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
268   uint8_t count = last_hit_buffer.count;
269   for (uint8_t i = 0; i < count; ++i) {
270     if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
271       last_hit_buffer.count--;
272       continue;
273     }
274     last_hit_buffer.tick[i] += deltaTime;
275   }
276 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
277 }
278
279 static void rgb_task_sync(void) {
280   // next task
281   if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
282     rgb_task_state = STARTING;
283 }
284
285 static void rgb_task_start(void) {
286   // reset iter
287   rgb_effect_params.iter = 0;
288
289   // update double buffers
290   g_rgb_counters.tick = rgb_counters_buffer;
291 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
292   g_last_hit_tracker = last_hit_buffer;
293 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
294
295   // next task
296   rgb_task_state = RENDERING;
297 }
298
299 static void rgb_task_render(uint8_t effect) {
300   bool rendering = false;
301   rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
302
303   // each effect can opt to do calculations
304   // and/or request PWM buffer updates.
305   switch (effect) {
306     case RGB_MATRIX_NONE:
307       rendering = rgb_matrix_none(&rgb_effect_params);
308       break;
309
310     case RGB_MATRIX_SOLID_COLOR:
311       rendering = rgb_matrix_solid_color(&rgb_effect_params);           // Max 1ms Avg 0ms
312       break;
313 #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS
314     case RGB_MATRIX_ALPHAS_MODS:
315       rendering = rgb_matrix_alphas_mods(&rgb_effect_params);           // Max 2ms Avg 1ms
316       break;
317 #endif // DISABLE_RGB_MATRIX_ALPHAS_MODS
318 #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
319     case RGB_MATRIX_GRADIENT_UP_DOWN:
320       rendering = rgb_matrix_gradient_up_down(&rgb_effect_params);      // Max 4ms Avg 3ms
321       break;
322 #endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
323 #ifndef DISABLE_RGB_MATRIX_BREATHING
324     case RGB_MATRIX_BREATHING:
325       rendering = rgb_matrix_breathing(&rgb_effect_params);             // Max 1ms Avg 0ms
326       break;
327 #endif // DISABLE_RGB_MATRIX_BREATHING
328 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
329     case RGB_MATRIX_CYCLE_ALL:
330       rendering = rgb_matrix_cycle_all(&rgb_effect_params);             // Max 4ms Avg 3ms
331       break;
332 #endif // DISABLE_RGB_MATRIX_CYCLE_ALL
333 #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
334     case RGB_MATRIX_CYCLE_LEFT_RIGHT:
335       rendering = rgb_matrix_cycle_left_right(&rgb_effect_params);      // Max 4ms Avg 3ms
336       break;
337 #endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
338 #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
339     case RGB_MATRIX_CYCLE_UP_DOWN:
340       rendering = rgb_matrix_cycle_up_down(&rgb_effect_params);         // Max 4ms Avg 3ms
341       break;
342 #endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
343 #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
344     case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
345       rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms
346       break;
347 #endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
348 #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON
349     case RGB_MATRIX_DUAL_BEACON:
350       rendering = rgb_matrix_dual_beacon(&rgb_effect_params);           // Max 4ms Avg 3ms
351       break;
352 #endif // DISABLE_RGB_MATRIX_DUAL_BEACON
353 #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON
354     case RGB_MATRIX_RAINBOW_BEACON:
355       rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params);        // Max 4ms Avg 3ms
356       break;
357 #endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON
358 #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
359     case RGB_MATRIX_RAINBOW_PINWHEELS:
360       rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params);     // Max 4ms Avg 3ms
361       break;
362 #endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
363 #ifndef DISABLE_RGB_MATRIX_RAINDROPS
364     case RGB_MATRIX_RAINDROPS:
365       rendering = rgb_matrix_raindrops(&rgb_effect_params);             // Max 1ms Avg 0ms
366       break;
367 #endif // DISABLE_RGB_MATRIX_RAINDROPS
368 #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
369     case RGB_MATRIX_JELLYBEAN_RAINDROPS:
370       rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params);   // Max 1ms Avg 0ms
371       break;
372 #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
373 #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
374     case RGB_MATRIX_DIGITAL_RAIN:
375       rendering = rgb_matrix_digital_rain(&rgb_effect_params);         // Max 9ms Avg 8ms | this is expensive, fix it
376       break;
377 #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
378 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
379 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE
380     case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
381       rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms
382       break;
383 #endif
384 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE
385     case RGB_MATRIX_SOLID_REACTIVE:
386       rendering = rgb_matrix_solid_reactive(&rgb_effect_params);       // Max 4ms Avg 3ms
387       break;
388 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE
389 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
390     case RGB_MATRIX_SOLID_REACTIVE_WIDE:
391       rendering = rgb_matrix_solid_reactive_wide(&rgb_effect_params);       // Max ?? ms Avg ?? ms
392       break;
393 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
394 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
395     case RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE:
396       rendering = rgb_matrix_solid_reactive_multiwide(&rgb_effect_params);       // Max ?? ms Avg ?? ms
397       break;
398 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
399 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
400     case RGB_MATRIX_SOLID_REACTIVE_CROSS:
401       rendering = rgb_matrix_solid_reactive_cross(&rgb_effect_params);       // Max ?? ms Avg ?? ms
402       break;
403 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
404 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
405     case RGB_MATRIX_SOLID_REACTIVE_MULTICROSS:
406       rendering = rgb_matrix_solid_reactive_multicross(&rgb_effect_params);       // Max ?? ms Avg ?? ms
407       break;
408 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
409 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
410     case RGB_MATRIX_SOLID_REACTIVE_NEXUS:
411       rendering = rgb_matrix_solid_reactive_nexus(&rgb_effect_params);       // Max ?? ms Avg ?? ms
412       break;
413 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
414 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
415     case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
416       rendering = rgb_matrix_solid_reactive_multinexus(&rgb_effect_params);       // Max ?? ms Avg ?? ms
417       break;
418 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
419 #ifndef DISABLE_RGB_MATRIX_SPLASH
420     case RGB_MATRIX_SPLASH:
421       rendering = rgb_matrix_splash(&rgb_effect_params);               // Max 5ms Avg 3ms
422       break;
423 #endif // DISABLE_RGB_MATRIX_SPLASH
424 #ifndef DISABLE_RGB_MATRIX_MULTISPLASH
425     case RGB_MATRIX_MULTISPLASH:
426       rendering = rgb_matrix_multisplash(&rgb_effect_params);          // Max 10ms Avg 5ms
427       break;
428 #endif // DISABLE_RGB_MATRIX_MULTISPLASH
429 #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH
430     case RGB_MATRIX_SOLID_SPLASH:
431       rendering = rgb_matrix_solid_splash(&rgb_effect_params);         // Max 5ms Avg 3ms
432       break;
433 #endif // DISABLE_RGB_MATRIX_SOLID_SPLASH
434 #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
435     case RGB_MATRIX_SOLID_MULTISPLASH:
436       rendering = rgb_matrix_solid_multisplash(&rgb_effect_params);    // Max 10ms Avg 5ms
437       break;
438 #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
439 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
440
441 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
442   #define RGB_MATRIX_EFFECT(name, ...) \
443     case RGB_MATRIX_CUSTOM_##name: \
444       rendering = name(&rgb_effect_params); \
445       break;
446   #ifdef RGB_MATRIX_CUSTOM_KB
447     #include "rgb_matrix_kb.inc"
448   #endif
449   #ifdef RGB_MATRIX_CUSTOM_USER
450     #include "rgb_matrix_user.inc"
451   #endif
452   #undef RGB_MATRIX_EFFECT
453 #endif
454
455     // Factory default magic value
456     case UINT8_MAX: {
457         rgb_matrix_test();
458         rgb_task_state = FLUSHING;
459       }
460       return;
461   }
462
463   rgb_effect_params.iter++;
464
465   // next task
466   if (!rendering) {
467     rgb_task_state = FLUSHING;
468     if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
469       // We only need to flush once if we are RGB_MATRIX_NONE
470       rgb_task_state = SYNCING;
471     }
472   }
473 }
474
475 static void rgb_task_flush(uint8_t effect) {
476   // update last trackers after the first full render so we can init over several frames
477   rgb_last_effect = effect;
478   rgb_last_enable = rgb_matrix_config.enable;
479
480   // update pwm buffers
481   rgb_matrix_update_pwm_buffers();
482
483   // next task
484   rgb_task_state = SYNCING;
485 }
486
487 void rgb_matrix_task(void) {
488   rgb_task_timers();
489
490   // Ideally we would also stop sending zeros to the LED driver PWM buffers
491   // while suspended and just do a software shutdown. This is a cheap hack for now.
492   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));
493   uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
494
495   switch (rgb_task_state) {
496     case STARTING:
497       rgb_task_start();
498       break;
499     case RENDERING:
500       rgb_task_render(effect);
501       break;
502     case FLUSHING:
503       rgb_task_flush(effect);
504       break;
505     case SYNCING:
506       rgb_task_sync();
507       break;
508   }
509
510   if (!suspend_backlight) {
511     rgb_matrix_indicators();
512   }
513 }
514
515 void rgb_matrix_indicators(void) {
516   rgb_matrix_indicators_kb();
517   rgb_matrix_indicators_user();
518 }
519
520 __attribute__((weak))
521 void rgb_matrix_indicators_kb(void) {}
522
523 __attribute__((weak))
524 void rgb_matrix_indicators_user(void) {}
525
526 void rgb_matrix_init(void) {
527   rgb_matrix_driver.init();
528
529   // TODO: put the 1 second startup delay here?
530
531 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
532   g_last_hit_tracker.count = 0;
533   for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
534     g_last_hit_tracker.tick[i] = UINT16_MAX;
535   }
536
537   last_hit_buffer.count = 0;
538   for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
539     last_hit_buffer.tick[i] = UINT16_MAX;
540   }
541 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
542
543   if (!eeconfig_is_enabled()) {
544     dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
545     eeconfig_init();
546     eeconfig_update_rgb_matrix_default();
547   }
548
549   rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
550   rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
551   if (!rgb_matrix_config.mode) {
552     dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
553     eeconfig_update_rgb_matrix_default();
554     rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
555   }
556   eeconfig_debug_rgb_matrix(); // display current eeprom values
557 }
558
559 void rgb_matrix_set_suspend_state(bool state) {
560   g_suspend_state = state;
561 }
562
563 void rgb_matrix_toggle(void) {
564   rgb_matrix_config.enable ^= 1;
565   rgb_task_state = STARTING;
566   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
567 }
568
569 void rgb_matrix_enable(void) {
570   rgb_matrix_enable_noeeprom();
571   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
572 }
573
574 void rgb_matrix_enable_noeeprom(void) {
575   if (!rgb_matrix_config.enable)
576     rgb_task_state = STARTING;
577   rgb_matrix_config.enable = 1;
578 }
579
580 void rgb_matrix_disable(void) {
581   rgb_matrix_disable_noeeprom();
582   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
583 }
584
585 void rgb_matrix_disable_noeeprom(void) {
586   if (rgb_matrix_config.enable)
587     rgb_task_state = STARTING;
588   rgb_matrix_config.enable = 0;
589 }
590
591 void rgb_matrix_step(void) {
592   rgb_matrix_config.mode++;
593   if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
594     rgb_matrix_config.mode = 1;
595   rgb_task_state = STARTING;
596   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
597 }
598
599 void rgb_matrix_step_reverse(void) {
600   rgb_matrix_config.mode--;
601   if (rgb_matrix_config.mode < 1)
602     rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
603   rgb_task_state = STARTING;
604   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
605 }
606
607 void rgb_matrix_increase_hue(void) {
608   rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
609   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
610 }
611
612 void rgb_matrix_decrease_hue(void) {
613   rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
614   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
615 }
616
617 void rgb_matrix_increase_sat(void) {
618   rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
619   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
620 }
621
622 void rgb_matrix_decrease_sat(void) {
623   rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
624   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
625 }
626
627 void rgb_matrix_increase_val(void) {
628   rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
629   if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
630     rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
631   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
632 }
633
634 void rgb_matrix_decrease_val(void) {
635   rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
636   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
637 }
638
639 void rgb_matrix_increase_speed(void) {
640   rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
641   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
642 }
643
644 void rgb_matrix_decrease_speed(void) {
645   rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
646   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
647 }
648
649 led_flags_t rgb_matrix_get_flags(void) {
650   return rgb_effect_params.flags;
651 }
652
653 void rgb_matrix_set_flags(led_flags_t flags) {
654   rgb_effect_params.flags = flags;
655 }
656
657 void rgb_matrix_mode(uint8_t mode) {
658   rgb_matrix_config.mode = mode;
659   rgb_task_state = STARTING;
660   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
661 }
662
663 void rgb_matrix_mode_noeeprom(uint8_t mode) {
664   rgb_matrix_config.mode = mode;
665 }
666
667 uint8_t rgb_matrix_get_mode(void) {
668   return rgb_matrix_config.mode;
669 }
670
671 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
672   rgb_matrix_sethsv_noeeprom(hue, sat, val);
673   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
674 }
675
676 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
677   rgb_matrix_config.hue = hue;
678   rgb_matrix_config.sat = sat;
679   rgb_matrix_config.val = val;
680   if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
681     rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
682 }