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