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