]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgb_matrix.c
adds timeout to avr i2c
[qmk_firmware.git] / quantum / rgb_matrix.c
1 /* Copyright 2017 Jason Williams
2  * Copyright 2017 Jack Humbert
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18
19 #include "rgb_matrix.h"
20 #include <avr/io.h>
21 #include "i2c_master.h"
22 #include <util/delay.h>
23 #include <avr/interrupt.h>
24 #include "progmem.h"
25 #include "config.h"
26 #include "eeprom.h"
27 #include "lufa.h"
28 #include <math.h>
29
30 rgb_config_t rgb_matrix_config;
31
32 #ifndef RGB_DISABLE_AFTER_TIMEOUT
33     #define RGB_DISABLE_AFTER_TIMEOUT 0
34 #endif
35
36 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
37     #define RGB_DISABLE_WHEN_USB_SUSPENDED false
38 #endif
39
40 #ifndef EECONFIG_RGB_MATRIX
41     #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
42 #endif
43
44 bool g_suspend_state = false;
45
46 // Global tick at 20 Hz
47 uint32_t g_tick = 0;
48
49 // Ticks since this key was last hit.
50 uint8_t g_key_hit[DRIVER_LED_TOTAL];
51
52 // Ticks since any key was last hit.
53 uint32_t g_any_key_hit = 0;
54
55 #ifndef PI
56 #define PI 3.14159265
57 #endif
58
59 uint32_t eeconfig_read_rgb_matrix(void) {
60   return eeprom_read_dword(EECONFIG_RGB_MATRIX);
61 }
62 void eeconfig_update_rgb_matrix(uint32_t val) {
63   eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
64 }
65 void eeconfig_update_rgb_matrix_default(void) {
66   dprintf("eeconfig_update_rgb_matrix_default\n");
67   rgb_matrix_config.enable = 1;
68   rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT;
69   rgb_matrix_config.hue = 0;
70   rgb_matrix_config.sat = 255;
71   rgb_matrix_config.val = 255;
72   rgb_matrix_config.speed = 0;
73   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
74 }
75 void eeconfig_debug_rgb_matrix(void) {
76   dprintf("rgb_matrix_config eprom\n");
77   dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
78   dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
79   dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
80   dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
81   dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
82   dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
83 }
84
85 // Last led hit
86 #define LED_HITS_TO_REMEMBER 8
87 uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
88 uint8_t g_last_led_count = 0;
89
90 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
91     rgb_led led;
92     *led_count = 0;
93
94     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
95         // map_index_to_led(i, &led);
96         led = g_rgb_leds[i];
97         if (row == led.matrix_co.row && column == led.matrix_co.col) {
98             led_i[*led_count] = i;
99             (*led_count)++;
100         }
101     }
102 }
103
104 void rgb_matrix_update_pwm_buffers(void) {
105     uint8_t ret = IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
106     ret |= IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
107     if (ret == 2) {
108       wait_ms(1000);
109       i2c_stop();
110       rgb_matrix_setup_drivers();
111     }
112 }
113
114 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
115     IS31FL3731_set_color( index, red, green, blue );
116 }
117
118 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
119     IS31FL3731_set_color_all( red, green, blue );
120 }
121
122 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
123     if ( record->event.pressed ) {
124         uint8_t led[8], led_count;
125         map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
126         if (led_count > 0) {
127             for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
128                 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
129             }
130             g_last_led_hit[0] = led[0];
131             g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
132         }
133         for(uint8_t i = 0; i < led_count; i++)
134             g_key_hit[led[i]] = 0;
135         g_any_key_hit = 0;
136     } else {
137         #ifdef RGB_MATRIX_KEYRELEASES
138         uint8_t led[8], led_count;
139         map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
140         for(uint8_t i = 0; i < led_count; i++)
141             g_key_hit[led[i]] = 255;
142
143         g_any_key_hit = 255;
144         #endif
145     }
146     return true;
147 }
148
149 void rgb_matrix_set_suspend_state(bool state) {
150     g_suspend_state = state;
151 }
152
153 void rgb_matrix_test(void) {
154     // Mask out bits 4 and 5
155     // Increase the factor to make the test animation slower (and reduce to make it faster)
156     uint8_t factor = 10;
157     switch ( (g_tick & (0b11 << factor)) >> factor )
158     {
159         case 0:
160         {
161             rgb_matrix_set_color_all( 20, 0, 0 );
162             break;
163         }
164         case 1:
165         {
166             rgb_matrix_set_color_all( 0, 20, 0 );
167             break;
168         }
169         case 2:
170         {
171             rgb_matrix_set_color_all( 0, 0, 20 );
172             break;
173         }
174         case 3:
175         {
176             rgb_matrix_set_color_all( 20, 20, 20 );
177             break;
178         }
179     }
180 }
181
182 // This tests the LEDs
183 // Note that it will change the LED control registers
184 // in the LED drivers, and leave them in an invalid
185 // state for other backlight effects.
186 // ONLY USE THIS FOR TESTING LEDS!
187 void rgb_matrix_single_LED_test(void) {
188     static uint8_t color = 0; // 0,1,2 for R,G,B
189     static uint8_t row = 0;
190     static uint8_t column = 0;
191
192     static uint8_t tick = 0;
193     tick++;
194
195     if ( tick > 2 )
196     {
197         tick = 0;
198         column++;
199     }
200     if ( column > MATRIX_COLS )
201     {
202         column = 0;
203         row++;
204     }
205     if ( row > MATRIX_ROWS )
206     {
207         row = 0;
208         color++;
209     }
210     if ( color > 2 )
211     {
212         color = 0;
213     }
214
215     uint8_t led[8], led_count;
216     map_row_column_to_led(row,column,led,&led_count);
217     for(uint8_t i = 0; i < led_count; i++) {
218         rgb_matrix_set_color_all( 40, 40, 40 );
219         rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
220     }
221 }
222
223 // All LEDs off
224 void rgb_matrix_all_off(void) {
225     rgb_matrix_set_color_all( 0, 0, 0 );
226 }
227
228 // Solid color
229 void rgb_matrix_solid_color(void) {
230     HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
231     RGB rgb = hsv_to_rgb( hsv );
232     rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
233 }
234
235 void rgb_matrix_solid_reactive(void) {
236         // Relies on hue being 8-bit and wrapping
237         for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
238         {
239                 uint16_t offset2 = g_key_hit[i]<<2;
240                 offset2 = (offset2<=130) ? (130-offset2) : 0;
241
242                 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
243                 RGB rgb = hsv_to_rgb( hsv );
244                 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
245         }
246 }
247
248 // alphas = color1, mods = color2
249 void rgb_matrix_alphas_mods(void) {
250
251     RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
252     RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
253
254     rgb_led led;
255     for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
256         led = g_rgb_leds[i];
257         if ( led.matrix_co.raw < 0xFF ) {
258             if ( led.modifier )
259             {
260                 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
261             }
262             else
263             {
264                 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
265             }
266         }
267     }
268 }
269
270 void rgb_matrix_gradient_up_down(void) {
271     int16_t h1 = rgb_matrix_config.hue;
272     int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
273     int16_t deltaH = h2 - h1;
274
275     // Take the shortest path between hues
276     if ( deltaH > 127 )
277     {
278         deltaH -= 256;
279     }
280     else if ( deltaH < -127 )
281     {
282         deltaH += 256;
283     }
284     // Divide delta by 4, this gives the delta per row
285     deltaH /= 4;
286
287     int16_t s1 = rgb_matrix_config.sat;
288     int16_t s2 = rgb_matrix_config.hue;
289     int16_t deltaS = ( s2 - s1 ) / 4;
290
291     HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
292     RGB rgb;
293     Point point;
294     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
295     {
296         // map_led_to_point( i, &point );
297         point = g_rgb_leds[i].point;
298         // The y range will be 0..64, map this to 0..4
299         uint8_t y = (point.y>>4);
300         // Relies on hue being 8-bit and wrapping
301         hsv.h = rgb_matrix_config.hue + ( deltaH * y );
302         hsv.s = rgb_matrix_config.sat + ( deltaS * y );
303         rgb = hsv_to_rgb( hsv );
304         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
305     }
306 }
307
308 void rgb_matrix_raindrops(bool initialize) {
309     int16_t h1 = rgb_matrix_config.hue;
310     int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
311     int16_t deltaH = h2 - h1;
312     deltaH /= 4;
313
314     // Take the shortest path between hues
315     if ( deltaH > 127 )
316     {
317         deltaH -= 256;
318     }
319     else if ( deltaH < -127 )
320     {
321         deltaH += 256;
322     }
323
324     int16_t s1 = rgb_matrix_config.sat;
325     int16_t s2 = rgb_matrix_config.sat;
326     int16_t deltaS = ( s2 - s1 ) / 4;
327
328     HSV hsv;
329     RGB rgb;
330
331     // Change one LED every tick, make sure speed is not 0
332     uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
333
334     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
335     {
336         // If initialize, all get set to random colors
337         // If not, all but one will stay the same as before.
338         if ( initialize || i == led_to_change )
339         {
340             hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
341             hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
342             // Override brightness with global brightness control
343             hsv.v = rgb_matrix_config.val;
344
345             rgb = hsv_to_rgb( hsv );
346             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
347         }
348     }
349 }
350
351 void rgb_matrix_cycle_all(void) {
352     uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
353
354     rgb_led led;
355
356     // Relies on hue being 8-bit and wrapping
357     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
358     {
359         // map_index_to_led(i, &led);
360         led = g_rgb_leds[i];
361         if (led.matrix_co.raw < 0xFF) {
362             uint16_t offset2 = g_key_hit[i]<<2;
363             offset2 = (offset2<=63) ? (63-offset2) : 0;
364
365             HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
366             RGB rgb = hsv_to_rgb( hsv );
367             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
368         }
369     }
370 }
371
372 void rgb_matrix_cycle_left_right(void) {
373     uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
374     HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
375     RGB rgb;
376     Point point;
377     rgb_led led;
378     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
379     {
380         // map_index_to_led(i, &led);
381         led = g_rgb_leds[i];
382         if (led.matrix_co.raw < 0xFF) {
383             uint16_t offset2 = g_key_hit[i]<<2;
384             offset2 = (offset2<=63) ? (63-offset2) : 0;
385
386             // map_led_to_point( i, &point );
387             point = g_rgb_leds[i].point;
388             // Relies on hue being 8-bit and wrapping
389             hsv.h = point.x + offset + offset2;
390             rgb = hsv_to_rgb( hsv );
391             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
392         }
393     }
394 }
395
396 void rgb_matrix_cycle_up_down(void) {
397     uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
398     HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
399     RGB rgb;
400     Point point;
401     rgb_led led;
402     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
403     {
404         // map_index_to_led(i, &led);
405         led = g_rgb_leds[i];
406         if (led.matrix_co.raw < 0xFF) {
407             uint16_t offset2 = g_key_hit[i]<<2;
408             offset2 = (offset2<=63) ? (63-offset2) : 0;
409
410             // map_led_to_point( i, &point );
411             point = g_rgb_leds[i].point;
412             // Relies on hue being 8-bit and wrapping
413             hsv.h = point.y + offset + offset2;
414             rgb = hsv_to_rgb( hsv );
415             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
416         }
417     }
418 }
419
420
421 void rgb_matrix_dual_beacon(void) {
422     HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
423     RGB rgb;
424     rgb_led led;
425     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
426         led = g_rgb_leds[i];
427         hsv.h = ((led.point.y - 32.0)* cos(g_tick * PI / 128) / 32 + (led.point.x - 112.0) * sin(g_tick * PI / 128) / (112)) * (180) + rgb_matrix_config.hue;
428         rgb = hsv_to_rgb( hsv );
429         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
430     }
431 }
432
433 void rgb_matrix_rainbow_beacon(void) {
434     HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
435     RGB rgb;
436     rgb_led led;
437     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
438         led = g_rgb_leds[i];
439         hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - 112.0) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
440         rgb = hsv_to_rgb( hsv );
441         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
442     }
443 }
444
445 void rgb_matrix_rainbow_pinwheels(void) {
446     HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
447     RGB rgb;
448     rgb_led led;
449     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
450         led = g_rgb_leds[i];
451         hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(led.point.x - 112.0)) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
452         rgb = hsv_to_rgb( hsv );
453         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
454     }
455 }
456
457 void rgb_matrix_rainbow_moving_chevron(void) {
458     HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
459     RGB rgb;
460     rgb_led led;
461     for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
462         led = g_rgb_leds[i];
463         // uint8_t r = g_tick;
464         uint8_t r = 32;
465         hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(led.point.y - 32.0)* sin(r * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - (g_tick / 256.0 * 224)) * cos(r * PI / 128) + rgb_matrix_config.hue;
466         rgb = hsv_to_rgb( hsv );
467         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
468     }
469 }
470
471
472 void rgb_matrix_jellybean_raindrops( bool initialize ) {
473     HSV hsv;
474     RGB rgb;
475
476     // Change one LED every tick, make sure speed is not 0
477     uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
478
479     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
480     {
481         // If initialize, all get set to random colors
482         // If not, all but one will stay the same as before.
483         if ( initialize || i == led_to_change )
484         {
485             hsv.h = rand() & 0xFF;
486             hsv.s = rand() & 0xFF;
487             // Override brightness with global brightness control
488             hsv.v = rgb_matrix_config.val;
489
490             rgb = hsv_to_rgb( hsv );
491             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
492         }
493     }
494 }
495
496 void rgb_matrix_multisplash(void) {
497     // if (g_any_key_hit < 0xFF) {
498         HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
499         RGB rgb;
500         rgb_led led;
501         for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
502             led = g_rgb_leds[i];
503             uint16_t c = 0, d = 0;
504             rgb_led last_led;
505             // if (g_last_led_count) {
506                 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
507                     last_led = g_rgb_leds[g_last_led_hit[last_i]];
508                     uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
509                     uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
510                     c += MIN(MAX(effect, 0), 255);
511                     d += 255 - MIN(MAX(effect, 0), 255);
512                 }
513             // } else {
514             //     d = 255;
515             // }
516             hsv.h = (rgb_matrix_config.hue + c) % 256;
517             hsv.v = MAX(MIN(d, 255), 0);
518             rgb = hsv_to_rgb( hsv );
519             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
520         }
521     // } else {
522         // rgb_matrix_set_color_all( 0, 0, 0 );
523     // }
524 }
525
526
527 void rgb_matrix_splash(void) {
528     g_last_led_count = MIN(g_last_led_count, 1);
529     rgb_matrix_multisplash();
530 }
531
532
533 void rgb_matrix_solid_multisplash(void) {
534     // if (g_any_key_hit < 0xFF) {
535         HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
536         RGB rgb;
537         rgb_led led;
538         for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
539             led = g_rgb_leds[i];
540             uint16_t d = 0;
541             rgb_led last_led;
542             // if (g_last_led_count) {
543                 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
544                     last_led = g_rgb_leds[g_last_led_hit[last_i]];
545                     uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
546                     uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
547                     d += 255 - MIN(MAX(effect, 0), 255);
548                 }
549             // } else {
550             //     d = 255;
551             // }
552             hsv.v = MAX(MIN(d, 255), 0);
553             rgb = hsv_to_rgb( hsv );
554             rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
555         }
556     // } else {
557         // rgb_matrix_set_color_all( 0, 0, 0 );
558     // }
559 }
560
561
562 void rgb_matrix_solid_splash(void) {
563     g_last_led_count = MIN(g_last_led_count, 1);
564     rgb_matrix_solid_multisplash();
565 }
566
567
568 // Needs eeprom access that we don't have setup currently
569
570 void rgb_matrix_custom(void) {
571 //     HSV hsv;
572 //     RGB rgb;
573 //     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
574 //     {
575 //         backlight_get_key_color(i, &hsv);
576 //         // Override brightness with global brightness control
577 //         hsv.v = rgb_matrix_config.val;
578 //         rgb = hsv_to_rgb( hsv );
579 //         rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
580 //     }
581 }
582
583 void rgb_matrix_task(void) {
584     static uint8_t toggle_enable_last = 255;
585         if (!rgb_matrix_config.enable) {
586         rgb_matrix_all_off();
587         toggle_enable_last = rgb_matrix_config.enable;
588         return;
589     }
590     // delay 1 second before driving LEDs or doing anything else
591     static uint8_t startup_tick = 0;
592     if ( startup_tick < 20 ) {
593         startup_tick++;
594         return;
595     }
596
597     g_tick++;
598
599     if ( g_any_key_hit < 0xFFFFFFFF ) {
600         g_any_key_hit++;
601     }
602
603     for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
604         if ( g_key_hit[led] < 255 ) {
605             if (g_key_hit[led] == 254)
606                 g_last_led_count = MAX(g_last_led_count - 1, 0);
607             g_key_hit[led]++;
608         }
609     }
610
611     // Factory default magic value
612     if ( rgb_matrix_config.mode == 255 ) {
613         rgb_matrix_test();
614         return;
615     }
616
617     // Ideally we would also stop sending zeros to the LED driver PWM buffers
618     // while suspended and just do a software shutdown. This is a cheap hack for now.
619     bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
620             (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
621     uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
622
623     // Keep track of the effect used last time,
624     // detect change in effect, so each effect can
625     // have an optional initialization.
626     static uint8_t effect_last = 255;
627     bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
628     effect_last = effect;
629     toggle_enable_last = rgb_matrix_config.enable;
630
631     // this gets ticked at 20 Hz.
632     // each effect can opt to do calculations
633     // and/or request PWM buffer updates.
634     switch ( effect ) {
635         case RGB_MATRIX_SOLID_COLOR:
636             rgb_matrix_solid_color();
637             break;
638         case RGB_MATRIX_ALPHAS_MODS:
639             rgb_matrix_alphas_mods();
640             break;
641         case RGB_MATRIX_DUAL_BEACON:
642             rgb_matrix_dual_beacon();
643             break;
644         case RGB_MATRIX_GRADIENT_UP_DOWN:
645             rgb_matrix_gradient_up_down();
646             break;
647         case RGB_MATRIX_RAINDROPS:
648             rgb_matrix_raindrops( initialize );
649             break;
650         case RGB_MATRIX_CYCLE_ALL:
651             rgb_matrix_cycle_all();
652             break;
653         case RGB_MATRIX_CYCLE_LEFT_RIGHT:
654             rgb_matrix_cycle_left_right();
655             break;
656         case RGB_MATRIX_CYCLE_UP_DOWN:
657             rgb_matrix_cycle_up_down();
658             break;
659         case RGB_MATRIX_RAINBOW_BEACON:
660             rgb_matrix_rainbow_beacon();
661             break;
662         case RGB_MATRIX_RAINBOW_PINWHEELS:
663             rgb_matrix_rainbow_pinwheels();
664             break;
665         case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
666             rgb_matrix_rainbow_moving_chevron();
667             break;
668         case RGB_MATRIX_JELLYBEAN_RAINDROPS:
669             rgb_matrix_jellybean_raindrops( initialize );
670             break;
671         #ifdef RGB_MATRIX_KEYPRESSES
672             case RGB_MATRIX_SOLID_REACTIVE:
673                 rgb_matrix_solid_reactive();
674                 break;
675             case RGB_MATRIX_SPLASH:
676                 rgb_matrix_splash();
677                 break;
678             case RGB_MATRIX_MULTISPLASH:
679                 rgb_matrix_multisplash();
680                 break;
681             case RGB_MATRIX_SOLID_SPLASH:
682                 rgb_matrix_solid_splash();
683                 break;
684             case RGB_MATRIX_SOLID_MULTISPLASH:
685                 rgb_matrix_solid_multisplash();
686                 break;
687         #endif
688         default:
689             rgb_matrix_custom();
690             break;
691     }
692
693     if ( ! suspend_backlight ) {
694         rgb_matrix_indicators();
695     }
696
697 }
698
699 void rgb_matrix_indicators(void) {
700     rgb_matrix_indicators_kb();
701     rgb_matrix_indicators_user();
702 }
703
704 __attribute__((weak))
705 void rgb_matrix_indicators_kb(void) {}
706
707 __attribute__((weak))
708 void rgb_matrix_indicators_user(void) {}
709
710
711 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
712 // {
713 //  if ( row >= MATRIX_ROWS )
714 //  {
715 //      // Special value, 255=none, 254=all
716 //      *index = row;
717 //  }
718 //  else
719 //  {
720 //      // This needs updated to something like
721 //      // uint8_t led[8], led_count;
722 //      // map_row_column_to_led(row,column,led,&led_count);
723 //      // for(uint8_t i = 0; i < led_count; i++)
724 //      map_row_column_to_led( row, column, index );
725 //  }
726 // }
727
728 void rgb_matrix_init(void) {
729   rgb_matrix_setup_drivers();
730
731   // TODO: put the 1 second startup delay here?
732
733   // clear the key hits
734   for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
735       g_key_hit[led] = 255;
736   }
737
738
739   if (!eeconfig_is_enabled()) {
740       dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
741       eeconfig_init();
742       eeconfig_update_rgb_matrix_default();
743   }
744   rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
745   if (!rgb_matrix_config.mode) {
746       dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
747       eeconfig_update_rgb_matrix_default();
748       rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
749   }
750   eeconfig_debug_rgb_matrix(); // display current eeprom values
751 }
752
753 void rgb_matrix_setup_drivers(void) {
754   // Initialize TWI
755   i2c_init();
756   IS31FL3731_init( DRIVER_ADDR_1 );
757   IS31FL3731_init( DRIVER_ADDR_2 );
758
759   for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
760     bool enabled = true;
761     // This only caches it for later
762     IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
763   }
764   // This actually updates the LED drivers
765   IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
766 }
767
768 // Deals with the messy details of incrementing an integer
769 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
770     int16_t new_value = value;
771     new_value += step;
772     return MIN( MAX( new_value, min ), max );
773 }
774
775 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
776     int16_t new_value = value;
777     new_value -= step;
778     return MIN( MAX( new_value, min ), max );
779 }
780
781 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
782 // {
783 //     // 3 bytes per color
784 //     return EECONFIG_RGB_MATRIX + ( led * 3 );
785 // }
786
787 // void backlight_get_key_color( uint8_t led, HSV *hsv )
788 // {
789 //     void *address = backlight_get_custom_key_color_eeprom_address( led );
790 //     hsv->h = eeprom_read_byte(address);
791 //     hsv->s = eeprom_read_byte(address+1);
792 //     hsv->v = eeprom_read_byte(address+2);
793 // }
794
795 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
796 // {
797 //     uint8_t led[8], led_count;
798 //     map_row_column_to_led(row,column,led,&led_count);
799 //     for(uint8_t i = 0; i < led_count; i++) {
800 //         if ( led[i] < DRIVER_LED_TOTAL )
801 //         {
802 //             void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
803 //             eeprom_update_byte(address, hsv.h);
804 //             eeprom_update_byte(address+1, hsv.s);
805 //             eeprom_update_byte(address+2, hsv.v);
806 //         }
807 //     }
808 // }
809
810 void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
811     for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
812     {
813         if ( i == index )
814         {
815             IS31FL3731_set_led_control_register( i, red, green, blue );
816         }
817         else
818         {
819             IS31FL3731_set_led_control_register( i, false, false, false );
820         }
821     }
822 }
823
824 uint32_t rgb_matrix_get_tick(void) {
825     return g_tick;
826 }
827
828 void rgblight_toggle(void) {
829         rgb_matrix_config.enable ^= 1;
830     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
831 }
832
833 void rgblight_step(void) {
834     rgb_matrix_config.mode++;
835     if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
836         rgb_matrix_config.mode = 1;
837     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
838 }
839
840 void rgblight_step_reverse(void) {
841     rgb_matrix_config.mode--;
842     if (rgb_matrix_config.mode < 1)
843         rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
844     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
845 }
846
847 void rgblight_increase_hue(void) {
848     rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
849     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
850 }
851
852 void rgblight_decrease_hue(void) {
853     rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
854     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
855 }
856
857 void rgblight_increase_sat(void) {
858     rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
859     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
860 }
861
862 void rgblight_decrease_sat(void) {
863     rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
864     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
865 }
866
867 void rgblight_increase_val(void) {
868     rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 );
869     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
870 }
871
872 void rgblight_decrease_val(void) {
873     rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 );
874     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
875 }
876
877 void rgblight_increase_speed(void) {
878     rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
879     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
880 }
881
882 void rgblight_decrease_speed(void) {
883     rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
884     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
885 }
886
887 void rgblight_mode(uint8_t mode) {
888     rgb_matrix_config.mode = mode;
889     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
890 }
891
892 uint32_t rgblight_get_mode(void) {
893     return rgb_matrix_config.mode;
894 }