]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgb_matrix.c
rgb_matrix: continue calling rgb_matrix_indicators() when toggled off
[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         rgb_matrix_indicators();
625         toggle_enable_last = rgb_matrix_config.enable;
626         return;
627     }
628     // delay 1 second before driving LEDs or doing anything else
629     static uint8_t startup_tick = 0;
630     if ( startup_tick < 20 ) {
631         startup_tick++;
632         return;
633     }
634
635     g_tick++;
636
637     if ( g_any_key_hit < 0xFFFFFFFF ) {
638         g_any_key_hit++;
639     }
640
641     for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
642         if ( g_key_hit[led] < 255 ) {
643             if (g_key_hit[led] == 254)
644                 g_last_led_count = MAX(g_last_led_count - 1, 0);
645             g_key_hit[led]++;
646         }
647     }
648
649     // Factory default magic value
650     if ( rgb_matrix_config.mode == 255 ) {
651         rgb_matrix_test();
652         return;
653     }
654
655     // Ideally we would also stop sending zeros to the LED driver PWM buffers
656     // while suspended and just do a software shutdown. This is a cheap hack for now.
657     bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
658             (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
659     uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
660
661     // Keep track of the effect used last time,
662     // detect change in effect, so each effect can
663     // have an optional initialization.
664     static uint8_t effect_last = 255;
665     bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
666     effect_last = effect;
667     toggle_enable_last = rgb_matrix_config.enable;
668
669     // this gets ticked at 20 Hz.
670     // each effect can opt to do calculations
671     // and/or request PWM buffer updates.
672     switch ( effect ) {
673         case RGB_MATRIX_SOLID_COLOR:
674             rgb_matrix_solid_color();
675             break;
676         case RGB_MATRIX_ALPHAS_MODS:
677             rgb_matrix_alphas_mods();
678             break;
679         case RGB_MATRIX_DUAL_BEACON:
680             rgb_matrix_dual_beacon();
681             break;
682         case RGB_MATRIX_GRADIENT_UP_DOWN:
683             rgb_matrix_gradient_up_down();
684             break;
685         case RGB_MATRIX_RAINDROPS:
686             rgb_matrix_raindrops( initialize );
687             break;
688         case RGB_MATRIX_CYCLE_ALL:
689             rgb_matrix_cycle_all();
690             break;
691         case RGB_MATRIX_CYCLE_LEFT_RIGHT:
692             rgb_matrix_cycle_left_right();
693             break;
694         case RGB_MATRIX_CYCLE_UP_DOWN:
695             rgb_matrix_cycle_up_down();
696             break;
697         case RGB_MATRIX_RAINBOW_BEACON:
698             rgb_matrix_rainbow_beacon();
699             break;
700         case RGB_MATRIX_RAINBOW_PINWHEELS:
701             rgb_matrix_rainbow_pinwheels();
702             break;
703         case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
704             rgb_matrix_rainbow_moving_chevron();
705             break;
706         case RGB_MATRIX_JELLYBEAN_RAINDROPS:
707             rgb_matrix_jellybean_raindrops( initialize );
708             break;
709         case RGB_MATRIX_DIGITAL_RAIN:
710             rgb_matrix_digital_rain( initialize );
711             break;
712         #ifdef RGB_MATRIX_KEYPRESSES
713             case RGB_MATRIX_SOLID_REACTIVE:
714                 rgb_matrix_solid_reactive();
715                 break;
716             case RGB_MATRIX_SPLASH:
717                 rgb_matrix_splash();
718                 break;
719             case RGB_MATRIX_MULTISPLASH:
720                 rgb_matrix_multisplash();
721                 break;
722             case RGB_MATRIX_SOLID_SPLASH:
723                 rgb_matrix_solid_splash();
724                 break;
725             case RGB_MATRIX_SOLID_MULTISPLASH:
726                 rgb_matrix_solid_multisplash();
727                 break;
728         #endif
729         default:
730             rgb_matrix_custom();
731             break;
732     }
733
734     if ( ! suspend_backlight ) {
735         rgb_matrix_indicators();
736     }
737
738 }
739
740 void rgb_matrix_indicators(void) {
741     rgb_matrix_indicators_kb();
742     rgb_matrix_indicators_user();
743 }
744
745 __attribute__((weak))
746 void rgb_matrix_indicators_kb(void) {}
747
748 __attribute__((weak))
749 void rgb_matrix_indicators_user(void) {}
750
751
752 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
753 // {
754 //  if ( row >= MATRIX_ROWS )
755 //  {
756 //      // Special value, 255=none, 254=all
757 //      *index = row;
758 //  }
759 //  else
760 //  {
761 //      // This needs updated to something like
762 //      // uint8_t led[8], led_count;
763 //      // map_row_column_to_led(row,column,led,&led_count);
764 //      // for(uint8_t i = 0; i < led_count; i++)
765 //      map_row_column_to_led( row, column, index );
766 //  }
767 // }
768
769 void rgb_matrix_init(void) {
770   rgb_matrix_driver.init();
771
772   // TODO: put the 1 second startup delay here?
773
774   // clear the key hits
775   for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
776       g_key_hit[led] = 255;
777   }
778
779
780   if (!eeconfig_is_enabled()) {
781       dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
782       eeconfig_init();
783       eeconfig_update_rgb_matrix_default();
784   }
785   rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
786   if (!rgb_matrix_config.mode) {
787       dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
788       eeconfig_update_rgb_matrix_default();
789       rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
790   }
791   eeconfig_debug_rgb_matrix(); // display current eeprom values
792 }
793
794 // Deals with the messy details of incrementing an integer
795 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
796     int16_t new_value = value;
797     new_value += step;
798     return MIN( MAX( new_value, min ), max );
799 }
800
801 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
802     int16_t new_value = value;
803     new_value -= step;
804     return MIN( MAX( new_value, min ), max );
805 }
806
807 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
808 // {
809 //     // 3 bytes per color
810 //     return EECONFIG_RGB_MATRIX + ( led * 3 );
811 // }
812
813 // void backlight_get_key_color( uint8_t led, HSV *hsv )
814 // {
815 //     void *address = backlight_get_custom_key_color_eeprom_address( led );
816 //     hsv->h = eeprom_read_byte(address);
817 //     hsv->s = eeprom_read_byte(address+1);
818 //     hsv->v = eeprom_read_byte(address+2);
819 // }
820
821 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
822 // {
823 //     uint8_t led[8], led_count;
824 //     map_row_column_to_led(row,column,led,&led_count);
825 //     for(uint8_t i = 0; i < led_count; i++) {
826 //         if ( led[i] < DRIVER_LED_TOTAL )
827 //         {
828 //             void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
829 //             eeprom_update_byte(address, hsv.h);
830 //             eeprom_update_byte(address+1, hsv.s);
831 //             eeprom_update_byte(address+2, hsv.v);
832 //         }
833 //     }
834 // }
835
836 uint32_t rgb_matrix_get_tick(void) {
837     return g_tick;
838 }
839
840 void rgblight_toggle(void) {
841         rgb_matrix_config.enable ^= 1;
842     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
843 }
844
845 void rgblight_step(void) {
846     rgb_matrix_config.mode++;
847     if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
848         rgb_matrix_config.mode = 1;
849     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
850 }
851
852 void rgblight_step_reverse(void) {
853     rgb_matrix_config.mode--;
854     if (rgb_matrix_config.mode < 1)
855         rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
856     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
857 }
858
859 void rgblight_increase_hue(void) {
860     rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
861     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
862 }
863
864 void rgblight_decrease_hue(void) {
865     rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
866     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
867 }
868
869 void rgblight_increase_sat(void) {
870     rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
871     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
872 }
873
874 void rgblight_decrease_sat(void) {
875     rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
876     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
877 }
878
879 void rgblight_increase_val(void) {
880     rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
881     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
882 }
883
884 void rgblight_decrease_val(void) {
885     rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
886     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
887 }
888
889 void rgblight_increase_speed(void) {
890     rgb_matrix_config.speed = increment( 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_decrease_speed(void) {
895     rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
896     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
897 }
898
899 void rgblight_mode(uint8_t mode) {
900     rgb_matrix_config.mode = mode;
901     eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
902 }
903
904 uint32_t rgblight_get_mode(void) {
905     return rgb_matrix_config.mode;
906 }
907
908 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
909   rgb_matrix_config.hue = hue;
910   rgb_matrix_config.sat = sat;
911   rgb_matrix_config.val = val;
912   eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
913 }