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