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