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