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 bool g_suspend_state = false;
55 // Global tick at 20 Hz
58 // Ticks since this key was last hit.
59 uint8_t g_key_hit[DRIVER_LED_TOTAL];
61 // Ticks since any key was last hit.
62 uint32_t g_any_key_hit = 0;
68 uint32_t eeconfig_read_rgb_matrix(void) {
69 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
71 void eeconfig_update_rgb_matrix(uint32_t val) {
72 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
74 void eeconfig_update_rgb_matrix_default(void) {
75 dprintf("eeconfig_update_rgb_matrix_default\n");
76 rgb_matrix_config.enable = 1;
77 rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT;
78 rgb_matrix_config.hue = 0;
79 rgb_matrix_config.sat = 255;
80 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
81 rgb_matrix_config.speed = 0;
82 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
84 void eeconfig_debug_rgb_matrix(void) {
85 dprintf("rgb_matrix_config eprom\n");
86 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
87 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
88 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
89 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
90 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
91 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
95 #define LED_HITS_TO_REMEMBER 8
96 uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
97 uint8_t g_last_led_count = 0;
99 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
103 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
104 // map_index_to_led(i, &led);
106 if (row == led.matrix_co.row && column == led.matrix_co.col) {
107 led_i[*led_count] = i;
113 void rgb_matrix_update_pwm_buffers(void) {
114 rgb_matrix_driver.flush();
117 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
118 rgb_matrix_driver.set_color(index, red, green, blue);
121 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
122 rgb_matrix_driver.set_color_all(red, green, blue);
125 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
126 if ( record->event.pressed ) {
127 uint8_t led[8], led_count;
128 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
130 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
131 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
133 g_last_led_hit[0] = led[0];
134 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
136 for(uint8_t i = 0; i < led_count; i++)
137 g_key_hit[led[i]] = 0;
140 #ifdef RGB_MATRIX_KEYRELEASES
141 uint8_t led[8], led_count;
142 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
143 for(uint8_t i = 0; i < led_count; i++)
144 g_key_hit[led[i]] = 255;
152 void rgb_matrix_set_suspend_state(bool state) {
153 g_suspend_state = state;
156 void rgb_matrix_test(void) {
157 // Mask out bits 4 and 5
158 // Increase the factor to make the test animation slower (and reduce to make it faster)
160 switch ( (g_tick & (0b11 << factor)) >> factor )
164 rgb_matrix_set_color_all( 20, 0, 0 );
169 rgb_matrix_set_color_all( 0, 20, 0 );
174 rgb_matrix_set_color_all( 0, 0, 20 );
179 rgb_matrix_set_color_all( 20, 20, 20 );
186 void rgb_matrix_all_off(void) {
187 rgb_matrix_set_color_all( 0, 0, 0 );
191 void rgb_matrix_solid_color(void) {
192 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
193 RGB rgb = hsv_to_rgb( hsv );
194 rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
197 void rgb_matrix_solid_reactive(void) {
198 // Relies on hue being 8-bit and wrapping
199 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
201 uint16_t offset2 = g_key_hit[i]<<2;
202 offset2 = (offset2<=130) ? (130-offset2) : 0;
204 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
205 RGB rgb = hsv_to_rgb( hsv );
206 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
210 // alphas = color1, mods = color2
211 void rgb_matrix_alphas_mods(void) {
213 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
214 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
217 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
219 if ( led.matrix_co.raw < 0xFF ) {
222 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
226 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
232 void rgb_matrix_gradient_up_down(void) {
233 int16_t h1 = rgb_matrix_config.hue;
234 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
235 int16_t deltaH = h2 - h1;
237 // Take the shortest path between hues
242 else if ( deltaH < -127 )
246 // Divide delta by 4, this gives the delta per row
249 int16_t s1 = rgb_matrix_config.sat;
250 int16_t s2 = rgb_matrix_config.hue;
251 int16_t deltaS = ( s2 - s1 ) / 4;
253 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
256 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
258 // map_led_to_point( i, &point );
259 point = g_rgb_leds[i].point;
260 // The y range will be 0..64, map this to 0..4
261 uint8_t y = (point.y>>4);
262 // Relies on hue being 8-bit and wrapping
263 hsv.h = rgb_matrix_config.hue + ( deltaH * y );
264 hsv.s = rgb_matrix_config.sat + ( deltaS * y );
265 rgb = hsv_to_rgb( hsv );
266 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
270 void rgb_matrix_raindrops(bool initialize) {
271 int16_t h1 = rgb_matrix_config.hue;
272 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
273 int16_t deltaH = h2 - h1;
276 // Take the shortest path between hues
281 else if ( deltaH < -127 )
286 int16_t s1 = rgb_matrix_config.sat;
287 int16_t s2 = rgb_matrix_config.sat;
288 int16_t deltaS = ( s2 - s1 ) / 4;
293 // Change one LED every tick, make sure speed is not 0
294 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
296 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
298 // If initialize, all get set to random colors
299 // If not, all but one will stay the same as before.
300 if ( initialize || i == led_to_change )
302 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
303 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
304 // Override brightness with global brightness control
305 hsv.v = rgb_matrix_config.val;
307 rgb = hsv_to_rgb( hsv );
308 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
313 void rgb_matrix_cycle_all(void) {
314 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
318 // Relies on hue being 8-bit and wrapping
319 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
321 // map_index_to_led(i, &led);
323 if (led.matrix_co.raw < 0xFF) {
324 uint16_t offset2 = g_key_hit[i]<<2;
325 offset2 = (offset2<=63) ? (63-offset2) : 0;
327 HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
328 RGB rgb = hsv_to_rgb( hsv );
329 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
334 void rgb_matrix_cycle_left_right(void) {
335 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
336 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
340 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
342 // map_index_to_led(i, &led);
344 if (led.matrix_co.raw < 0xFF) {
345 uint16_t offset2 = g_key_hit[i]<<2;
346 offset2 = (offset2<=63) ? (63-offset2) : 0;
348 // map_led_to_point( i, &point );
349 point = g_rgb_leds[i].point;
350 // Relies on hue being 8-bit and wrapping
351 hsv.h = point.x + offset + offset2;
352 rgb = hsv_to_rgb( hsv );
353 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
358 void rgb_matrix_cycle_up_down(void) {
359 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
360 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
364 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
366 // map_index_to_led(i, &led);
368 if (led.matrix_co.raw < 0xFF) {
369 uint16_t offset2 = g_key_hit[i]<<2;
370 offset2 = (offset2<=63) ? (63-offset2) : 0;
372 // map_led_to_point( i, &point );
373 point = g_rgb_leds[i].point;
374 // Relies on hue being 8-bit and wrapping
375 hsv.h = point.y + offset + offset2;
376 rgb = hsv_to_rgb( hsv );
377 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
383 void rgb_matrix_dual_beacon(void) {
384 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
387 double cos_value = cos(g_tick * PI / 128) / 32;
388 double sin_value = sin(g_tick * PI / 128) / 112;
389 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
390 point = g_rgb_leds[i].point;
391 hsv.h = ((point.y - 32.0)* cos_value + (point.x - 112.0) * sin_value) * (180) + rgb_matrix_config.hue;
392 rgb = hsv_to_rgb( hsv );
393 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
397 void rgb_matrix_rainbow_beacon(void) {
398 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
401 double cos_value = cos(g_tick * PI / 128);
402 double sin_value = sin(g_tick * PI / 128);
403 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
404 point = g_rgb_leds[i].point;
405 hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.y - 32.0)* cos_value + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.x - 112.0) * sin_value + rgb_matrix_config.hue;
406 rgb = hsv_to_rgb( hsv );
407 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
411 void rgb_matrix_rainbow_pinwheels(void) {
412 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
415 double cos_value = cos(g_tick * PI / 128);
416 double sin_value = sin(g_tick * PI / 128);
417 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
418 point = g_rgb_leds[i].point;
419 hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.y - 32.0)* cos_value + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(point.x - 112.0)) * sin_value + rgb_matrix_config.hue;
420 rgb = hsv_to_rgb( hsv );
421 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
425 void rgb_matrix_rainbow_moving_chevron(void) {
426 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
430 double cos_value = cos(r * PI / 128);
431 double sin_value = sin(r * PI / 128);
432 double multiplier = (g_tick / 256.0 * 224);
433 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
434 point = g_rgb_leds[i].point;
435 hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(point.y - 32.0)* sin_value + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (point.x - multiplier) * cos_value + rgb_matrix_config.hue;
436 rgb = hsv_to_rgb( hsv );
437 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
442 void rgb_matrix_jellybean_raindrops( bool initialize ) {
446 // Change one LED every tick, make sure speed is not 0
447 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
449 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
451 // If initialize, all get set to random colors
452 // If not, all but one will stay the same as before.
453 if ( initialize || i == led_to_change )
455 hsv.h = rand() & 0xFF;
456 hsv.s = rand() & 0xFF;
457 // Override brightness with global brightness control
458 hsv.v = rgb_matrix_config.val;
460 rgb = hsv_to_rgb( hsv );
461 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
466 void rgb_matrix_digital_rain( const bool initialize ) {
467 // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
468 const uint8_t drop_ticks = 28;
469 const uint8_t new_drop_probability = 24;
470 const uint8_t pure_green_intensity = 0xd0;
471 const uint8_t max_brightness_boost = 0xc0;
472 const uint8_t max_intensity = 0xff;
474 static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}};
475 static uint8_t drop = 0;
478 rgb_matrix_set_color_all(0, 0, 0);
479 memset(map, 0, sizeof map);
482 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
483 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
484 if (row == 0 && drop == 0 && rand() < RAND_MAX / new_drop_probability) {
485 // top row, pixels have just fallen and we're
486 // making a new rain drop in this column
487 map[col][row] = max_intensity;
489 else if (map[col][row] > 0 && map[col][row] < max_intensity) {
490 // neither fully bright nor dark, decay it
493 // set the pixel colour
494 uint8_t led, led_count;
495 map_row_column_to_led(row, col, &led, &led_count);
497 if (map[col][row] > pure_green_intensity) {
498 const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost
499 * (map[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity));
500 rgb_matrix_set_color(led, boost, max_intensity, boost);
503 const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity);
504 rgb_matrix_set_color(led, 0, green, 0);
508 if (++drop > drop_ticks) {
511 for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) {
512 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
513 // if ths is on the bottom row and bright allow decay
514 if (row == MATRIX_ROWS - 1 && map[col][row] == max_intensity) {
517 // check if the pixel above is bright
518 if (map[col][row - 1] == max_intensity) {
519 // allow old bright pixel to decay
521 // make this pixel bright
522 map[col][row] = max_intensity;
529 void rgb_matrix_multisplash(void) {
530 // if (g_any_key_hit < 0xFF) {
531 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
534 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
536 uint16_t c = 0, d = 0;
538 // if (g_last_led_count) {
539 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
540 last_led = g_rgb_leds[g_last_led_hit[last_i]];
541 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
542 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
543 c += MIN(MAX(effect, 0), 255);
544 d += 255 - MIN(MAX(effect, 0), 255);
549 hsv.h = (rgb_matrix_config.hue + c) % 256;
550 hsv.v = MAX(MIN(d, 255), 0);
551 rgb = hsv_to_rgb( hsv );
552 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
555 // rgb_matrix_set_color_all( 0, 0, 0 );
560 void rgb_matrix_splash(void) {
561 g_last_led_count = MIN(g_last_led_count, 1);
562 rgb_matrix_multisplash();
566 void rgb_matrix_solid_multisplash(void) {
567 // if (g_any_key_hit < 0xFF) {
568 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
571 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
575 // if (g_last_led_count) {
576 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
577 last_led = g_rgb_leds[g_last_led_hit[last_i]];
578 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
579 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
580 d += 255 - MIN(MAX(effect, 0), 255);
585 hsv.v = MAX(MIN(d, 255), 0);
586 rgb = hsv_to_rgb( hsv );
587 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
590 // rgb_matrix_set_color_all( 0, 0, 0 );
595 void rgb_matrix_solid_splash(void) {
596 g_last_led_count = MIN(g_last_led_count, 1);
597 rgb_matrix_solid_multisplash();
601 // Needs eeprom access that we don't have setup currently
603 void rgb_matrix_custom(void) {
606 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
608 // backlight_get_key_color(i, &hsv);
609 // // Override brightness with global brightness control
610 // hsv.v = rgb_matrix_config.val;
611 // rgb = hsv_to_rgb( hsv );
612 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
616 void rgb_matrix_task(void) {
617 static uint8_t toggle_enable_last = 255;
618 if (!rgb_matrix_config.enable) {
619 rgb_matrix_all_off();
620 toggle_enable_last = rgb_matrix_config.enable;
623 // delay 1 second before driving LEDs or doing anything else
624 static uint8_t startup_tick = 0;
625 if ( startup_tick < 20 ) {
632 if ( g_any_key_hit < 0xFFFFFFFF ) {
636 for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
637 if ( g_key_hit[led] < 255 ) {
638 if (g_key_hit[led] == 254)
639 g_last_led_count = MAX(g_last_led_count - 1, 0);
644 // Factory default magic value
645 if ( rgb_matrix_config.mode == 255 ) {
650 // Ideally we would also stop sending zeros to the LED driver PWM buffers
651 // while suspended and just do a software shutdown. This is a cheap hack for now.
652 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
653 (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
654 uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
656 // Keep track of the effect used last time,
657 // detect change in effect, so each effect can
658 // have an optional initialization.
659 static uint8_t effect_last = 255;
660 bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
661 effect_last = effect;
662 toggle_enable_last = rgb_matrix_config.enable;
664 // this gets ticked at 20 Hz.
665 // each effect can opt to do calculations
666 // and/or request PWM buffer updates.
668 case RGB_MATRIX_SOLID_COLOR:
669 rgb_matrix_solid_color();
671 case RGB_MATRIX_ALPHAS_MODS:
672 rgb_matrix_alphas_mods();
674 case RGB_MATRIX_DUAL_BEACON:
675 rgb_matrix_dual_beacon();
677 case RGB_MATRIX_GRADIENT_UP_DOWN:
678 rgb_matrix_gradient_up_down();
680 case RGB_MATRIX_RAINDROPS:
681 rgb_matrix_raindrops( initialize );
683 case RGB_MATRIX_CYCLE_ALL:
684 rgb_matrix_cycle_all();
686 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
687 rgb_matrix_cycle_left_right();
689 case RGB_MATRIX_CYCLE_UP_DOWN:
690 rgb_matrix_cycle_up_down();
692 case RGB_MATRIX_RAINBOW_BEACON:
693 rgb_matrix_rainbow_beacon();
695 case RGB_MATRIX_RAINBOW_PINWHEELS:
696 rgb_matrix_rainbow_pinwheels();
698 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
699 rgb_matrix_rainbow_moving_chevron();
701 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
702 rgb_matrix_jellybean_raindrops( initialize );
704 case RGB_MATRIX_DIGITAL_RAIN:
705 rgb_matrix_digital_rain( initialize );
707 #ifdef RGB_MATRIX_KEYPRESSES
708 case RGB_MATRIX_SOLID_REACTIVE:
709 rgb_matrix_solid_reactive();
711 case RGB_MATRIX_SPLASH:
714 case RGB_MATRIX_MULTISPLASH:
715 rgb_matrix_multisplash();
717 case RGB_MATRIX_SOLID_SPLASH:
718 rgb_matrix_solid_splash();
720 case RGB_MATRIX_SOLID_MULTISPLASH:
721 rgb_matrix_solid_multisplash();
729 if ( ! suspend_backlight ) {
730 rgb_matrix_indicators();
735 void rgb_matrix_indicators(void) {
736 rgb_matrix_indicators_kb();
737 rgb_matrix_indicators_user();
740 __attribute__((weak))
741 void rgb_matrix_indicators_kb(void) {}
743 __attribute__((weak))
744 void rgb_matrix_indicators_user(void) {}
747 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
749 // if ( row >= MATRIX_ROWS )
751 // // Special value, 255=none, 254=all
756 // // This needs updated to something like
757 // // uint8_t led[8], led_count;
758 // // map_row_column_to_led(row,column,led,&led_count);
759 // // for(uint8_t i = 0; i < led_count; i++)
760 // map_row_column_to_led( row, column, index );
764 void rgb_matrix_init(void) {
765 rgb_matrix_driver.init();
767 // TODO: put the 1 second startup delay here?
769 // clear the key hits
770 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
771 g_key_hit[led] = 255;
775 if (!eeconfig_is_enabled()) {
776 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
778 eeconfig_update_rgb_matrix_default();
780 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
781 if (!rgb_matrix_config.mode) {
782 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
783 eeconfig_update_rgb_matrix_default();
784 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
786 eeconfig_debug_rgb_matrix(); // display current eeprom values
789 // Deals with the messy details of incrementing an integer
790 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
791 int16_t new_value = value;
793 return MIN( MAX( new_value, min ), max );
796 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
797 int16_t new_value = value;
799 return MIN( MAX( new_value, min ), max );
802 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
804 // // 3 bytes per color
805 // return EECONFIG_RGB_MATRIX + ( led * 3 );
808 // void backlight_get_key_color( uint8_t led, HSV *hsv )
810 // void *address = backlight_get_custom_key_color_eeprom_address( led );
811 // hsv->h = eeprom_read_byte(address);
812 // hsv->s = eeprom_read_byte(address+1);
813 // hsv->v = eeprom_read_byte(address+2);
816 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
818 // uint8_t led[8], led_count;
819 // map_row_column_to_led(row,column,led,&led_count);
820 // for(uint8_t i = 0; i < led_count; i++) {
821 // if ( led[i] < DRIVER_LED_TOTAL )
823 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
824 // eeprom_update_byte(address, hsv.h);
825 // eeprom_update_byte(address+1, hsv.s);
826 // eeprom_update_byte(address+2, hsv.v);
831 uint32_t rgb_matrix_get_tick(void) {
835 void rgblight_toggle(void) {
836 rgb_matrix_config.enable ^= 1;
837 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
840 void rgblight_step(void) {
841 rgb_matrix_config.mode++;
842 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
843 rgb_matrix_config.mode = 1;
844 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
847 void rgblight_step_reverse(void) {
848 rgb_matrix_config.mode--;
849 if (rgb_matrix_config.mode < 1)
850 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
851 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
854 void rgblight_increase_hue(void) {
855 rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
856 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
859 void rgblight_decrease_hue(void) {
860 rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
861 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
864 void rgblight_increase_sat(void) {
865 rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
866 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
869 void rgblight_decrease_sat(void) {
870 rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
871 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
874 void rgblight_increase_val(void) {
875 rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
876 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
879 void rgblight_decrease_val(void) {
880 rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
881 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
884 void rgblight_increase_speed(void) {
885 rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
886 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
889 void rgblight_decrease_speed(void) {
890 rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
891 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
894 void rgblight_mode(uint8_t mode) {
895 rgb_matrix_config.mode = mode;
896 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
899 uint32_t rgblight_get_mode(void) {
900 return rgb_matrix_config.mode;