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