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