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