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