1 /* Copyright 2017 Jason Williams
2 * Copyright 2017 Jack Humbert
3 * Copyright 2018 Yiancar
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.
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.
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/>.
20 #include "rgb_matrix.h"
27 rgb_config_t rgb_matrix_config;
30 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
34 #define MIN(a,b) ((a) < (b)? (a): (b))
37 #ifndef RGB_DISABLE_AFTER_TIMEOUT
38 #define RGB_DISABLE_AFTER_TIMEOUT 0
41 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
42 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
45 #ifndef EECONFIG_RGB_MATRIX
46 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
49 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255
50 #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255
53 #ifndef RGB_DIGITAL_RAIN_DROPS
54 // lower the number for denser effect/wider keyboard
55 #define RGB_DIGITAL_RAIN_DROPS 24
58 bool g_suspend_state = false;
60 // Global tick at 20 Hz
63 // Ticks since this key was last hit.
64 uint8_t g_key_hit[DRIVER_LED_TOTAL];
66 // Ticks since any key was last hit.
67 uint32_t g_any_key_hit = 0;
73 uint32_t eeconfig_read_rgb_matrix(void) {
74 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
76 void eeconfig_update_rgb_matrix(uint32_t val) {
77 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
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);
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);
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;
104 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
108 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
109 // map_index_to_led(i, &led);
111 if (row == led.matrix_co.row && column == led.matrix_co.col) {
112 led_i[*led_count] = i;
118 void rgb_matrix_update_pwm_buffers(void) {
119 rgb_matrix_driver.flush();
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);
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);
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);
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];
138 g_last_led_hit[0] = led[0];
139 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
141 for(uint8_t i = 0; i < led_count; i++)
142 g_key_hit[led[i]] = 0;
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;
157 void rgb_matrix_set_suspend_state(bool state) {
158 g_suspend_state = state;
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)
165 switch ( (g_tick & (0b11 << factor)) >> factor )
169 rgb_matrix_set_color_all( 20, 0, 0 );
174 rgb_matrix_set_color_all( 0, 20, 0 );
179 rgb_matrix_set_color_all( 0, 0, 20 );
184 rgb_matrix_set_color_all( 20, 20, 20 );
191 void rgb_matrix_all_off(void) {
192 rgb_matrix_set_color_all( 0, 0, 0 );
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 );
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++ )
206 uint16_t offset2 = g_key_hit[i]<<2;
207 offset2 = (offset2<=130) ? (130-offset2) : 0;
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 );
215 // alphas = color1, mods = color2
216 void rgb_matrix_alphas_mods(void) {
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 } );
222 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
224 if ( led.matrix_co.raw < 0xFF ) {
227 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
231 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
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;
242 // Take the shortest path between hues
247 else if ( deltaH < -127 )
251 // Divide delta by 4, this gives the delta per row
254 int16_t s1 = rgb_matrix_config.sat;
255 int16_t s2 = rgb_matrix_config.hue;
256 int16_t deltaS = ( s2 - s1 ) / 4;
258 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
261 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
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 );
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;
281 // Take the shortest path between hues
286 else if ( deltaH < -127 )
291 int16_t s1 = rgb_matrix_config.sat;
292 int16_t s2 = rgb_matrix_config.sat;
293 int16_t deltaS = ( s2 - s1 ) / 4;
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;
301 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
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 )
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;
312 rgb = hsv_to_rgb( hsv );
313 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
318 void rgb_matrix_cycle_all(void) {
319 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
323 // Relies on hue being 8-bit and wrapping
324 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
326 // map_index_to_led(i, &led);
328 if (led.matrix_co.raw < 0xFF) {
329 uint16_t offset2 = g_key_hit[i]<<2;
330 offset2 = (offset2<=63) ? (63-offset2) : 0;
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 );
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 };
345 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
347 // map_index_to_led(i, &led);
349 if (led.matrix_co.raw < 0xFF) {
350 uint16_t offset2 = g_key_hit[i]<<2;
351 offset2 = (offset2<=63) ? (63-offset2) : 0;
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 );
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 };
369 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
371 // map_index_to_led(i, &led);
373 if (led.matrix_co.raw < 0xFF) {
374 uint16_t offset2 = g_key_hit[i]<<2;
375 offset2 = (offset2<=63) ? (63-offset2) : 0;
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 );
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 };
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 );
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 };
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 );
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 };
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 );
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 };
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 );
447 void rgb_matrix_jellybean_raindrops( bool initialize ) {
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;
454 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
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 )
460 hsv.h = rand() & 0xFF;
461 hsv.s = rand() & 0xFF;
462 // Override brightness with global brightness control
463 hsv.v = rgb_matrix_config.val;
465 rgb = hsv_to_rgb( hsv );
466 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
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;
478 static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}};
479 static uint8_t drop = 0;
482 rgb_matrix_set_color_all(0, 0, 0);
483 memset(map, 0, sizeof map);
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;
493 else if (map[col][row] > 0 && map[col][row] < max_intensity) {
494 // neither fully bright nor dark, decay it
497 // set the pixel colour
498 uint8_t led, led_count;
499 map_row_column_to_led(row, col, &led, &led_count);
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);
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);
512 if (++drop > drop_ticks) {
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) {
521 // check if the pixel above is bright
522 if (map[col][row - 1] == max_intensity) {
523 // allow old bright pixel to decay
525 // make this pixel bright
526 map[col][row] = max_intensity;
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 };
538 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
540 uint16_t c = 0, d = 0;
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);
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 );
559 // rgb_matrix_set_color_all( 0, 0, 0 );
564 void rgb_matrix_splash(void) {
565 g_last_led_count = MIN(g_last_led_count, 1);
566 rgb_matrix_multisplash();
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 };
575 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
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);
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 );
594 // rgb_matrix_set_color_all( 0, 0, 0 );
599 void rgb_matrix_solid_splash(void) {
600 g_last_led_count = MIN(g_last_led_count, 1);
601 rgb_matrix_solid_multisplash();
605 // Needs eeprom access that we don't have setup currently
607 void rgb_matrix_custom(void) {
610 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
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 );
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;
628 // delay 1 second before driving LEDs or doing anything else
629 static uint8_t startup_tick = 0;
630 if ( startup_tick < 20 ) {
637 if ( g_any_key_hit < 0xFFFFFFFF ) {
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);
649 // Factory default magic value
650 if ( rgb_matrix_config.mode == 255 ) {
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;
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;
669 // this gets ticked at 20 Hz.
670 // each effect can opt to do calculations
671 // and/or request PWM buffer updates.
673 case RGB_MATRIX_SOLID_COLOR:
674 rgb_matrix_solid_color();
676 case RGB_MATRIX_ALPHAS_MODS:
677 rgb_matrix_alphas_mods();
679 case RGB_MATRIX_DUAL_BEACON:
680 rgb_matrix_dual_beacon();
682 case RGB_MATRIX_GRADIENT_UP_DOWN:
683 rgb_matrix_gradient_up_down();
685 case RGB_MATRIX_RAINDROPS:
686 rgb_matrix_raindrops( initialize );
688 case RGB_MATRIX_CYCLE_ALL:
689 rgb_matrix_cycle_all();
691 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
692 rgb_matrix_cycle_left_right();
694 case RGB_MATRIX_CYCLE_UP_DOWN:
695 rgb_matrix_cycle_up_down();
697 case RGB_MATRIX_RAINBOW_BEACON:
698 rgb_matrix_rainbow_beacon();
700 case RGB_MATRIX_RAINBOW_PINWHEELS:
701 rgb_matrix_rainbow_pinwheels();
703 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
704 rgb_matrix_rainbow_moving_chevron();
706 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
707 rgb_matrix_jellybean_raindrops( initialize );
709 case RGB_MATRIX_DIGITAL_RAIN:
710 rgb_matrix_digital_rain( initialize );
712 #ifdef RGB_MATRIX_KEYPRESSES
713 case RGB_MATRIX_SOLID_REACTIVE:
714 rgb_matrix_solid_reactive();
716 case RGB_MATRIX_SPLASH:
719 case RGB_MATRIX_MULTISPLASH:
720 rgb_matrix_multisplash();
722 case RGB_MATRIX_SOLID_SPLASH:
723 rgb_matrix_solid_splash();
725 case RGB_MATRIX_SOLID_MULTISPLASH:
726 rgb_matrix_solid_multisplash();
734 if ( ! suspend_backlight ) {
735 rgb_matrix_indicators();
740 void rgb_matrix_indicators(void) {
741 rgb_matrix_indicators_kb();
742 rgb_matrix_indicators_user();
745 __attribute__((weak))
746 void rgb_matrix_indicators_kb(void) {}
748 __attribute__((weak))
749 void rgb_matrix_indicators_user(void) {}
752 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
754 // if ( row >= MATRIX_ROWS )
756 // // Special value, 255=none, 254=all
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 );
769 void rgb_matrix_init(void) {
770 rgb_matrix_driver.init();
772 // TODO: put the 1 second startup delay here?
774 // clear the key hits
775 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
776 g_key_hit[led] = 255;
780 if (!eeconfig_is_enabled()) {
781 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
783 eeconfig_update_rgb_matrix_default();
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();
791 eeconfig_debug_rgb_matrix(); // display current eeprom values
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;
798 return MIN( MAX( new_value, min ), max );
801 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
802 int16_t new_value = value;
804 return MIN( MAX( new_value, min ), max );
807 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
809 // // 3 bytes per color
810 // return EECONFIG_RGB_MATRIX + ( led * 3 );
813 // void backlight_get_key_color( uint8_t led, HSV *hsv )
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);
821 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
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 )
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);
836 uint32_t rgb_matrix_get_tick(void) {
840 void rgblight_toggle(void) {
841 rgb_matrix_config.enable ^= 1;
842 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
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);
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);
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);
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);
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);
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);
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);
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);
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
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
899 void rgblight_mode(uint8_t mode) {
900 rgb_matrix_config.mode = mode;
901 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
904 uint32_t rgblight_get_mode(void) {
905 return rgb_matrix_config.mode;
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);