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