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"
21 #include "i2c_master.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) {
115 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
116 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
117 #elif defined(IS31FL3733)
118 IS31FL3733_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
119 IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
123 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
125 IS31FL3731_set_color( index, red, green, blue );
126 #elif defined(IS31FL3733)
127 IS31FL3733_set_color( index, red, green, blue );
131 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
133 IS31FL3731_set_color_all( red, green, blue );
134 #elif defined(IS31FL3733)
135 IS31FL3733_set_color_all( red, green, blue );
139 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
140 if ( record->event.pressed ) {
141 uint8_t led[8], led_count;
142 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
144 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
145 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
147 g_last_led_hit[0] = led[0];
148 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
150 for(uint8_t i = 0; i < led_count; i++)
151 g_key_hit[led[i]] = 0;
154 #ifdef RGB_MATRIX_KEYRELEASES
155 uint8_t led[8], led_count;
156 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
157 for(uint8_t i = 0; i < led_count; i++)
158 g_key_hit[led[i]] = 255;
166 void rgb_matrix_set_suspend_state(bool state) {
167 g_suspend_state = state;
170 void rgb_matrix_test(void) {
171 // Mask out bits 4 and 5
172 // Increase the factor to make the test animation slower (and reduce to make it faster)
174 switch ( (g_tick & (0b11 << factor)) >> factor )
178 rgb_matrix_set_color_all( 20, 0, 0 );
183 rgb_matrix_set_color_all( 0, 20, 0 );
188 rgb_matrix_set_color_all( 0, 0, 20 );
193 rgb_matrix_set_color_all( 20, 20, 20 );
199 // This tests the LEDs
200 // Note that it will change the LED control registers
201 // in the LED drivers, and leave them in an invalid
202 // state for other backlight effects.
203 // ONLY USE THIS FOR TESTING LEDS!
204 void rgb_matrix_single_LED_test(void) {
205 static uint8_t color = 0; // 0,1,2 for R,G,B
206 static uint8_t row = 0;
207 static uint8_t column = 0;
209 static uint8_t tick = 0;
217 if ( column > MATRIX_COLS )
222 if ( row > MATRIX_ROWS )
232 uint8_t led[8], led_count;
233 map_row_column_to_led(row,column,led,&led_count);
234 for(uint8_t i = 0; i < led_count; i++) {
235 rgb_matrix_set_color_all( 40, 40, 40 );
236 rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
241 void rgb_matrix_all_off(void) {
242 rgb_matrix_set_color_all( 0, 0, 0 );
246 void rgb_matrix_solid_color(void) {
247 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
248 RGB rgb = hsv_to_rgb( hsv );
249 rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
252 void rgb_matrix_solid_reactive(void) {
253 // Relies on hue being 8-bit and wrapping
254 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
256 uint16_t offset2 = g_key_hit[i]<<2;
257 offset2 = (offset2<=130) ? (130-offset2) : 0;
259 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
260 RGB rgb = hsv_to_rgb( hsv );
261 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
265 // alphas = color1, mods = color2
266 void rgb_matrix_alphas_mods(void) {
268 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
269 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
272 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
274 if ( led.matrix_co.raw < 0xFF ) {
277 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
281 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
287 void rgb_matrix_gradient_up_down(void) {
288 int16_t h1 = rgb_matrix_config.hue;
289 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
290 int16_t deltaH = h2 - h1;
292 // Take the shortest path between hues
297 else if ( deltaH < -127 )
301 // Divide delta by 4, this gives the delta per row
304 int16_t s1 = rgb_matrix_config.sat;
305 int16_t s2 = rgb_matrix_config.hue;
306 int16_t deltaS = ( s2 - s1 ) / 4;
308 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
311 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
313 // map_led_to_point( i, &point );
314 point = g_rgb_leds[i].point;
315 // The y range will be 0..64, map this to 0..4
316 uint8_t y = (point.y>>4);
317 // Relies on hue being 8-bit and wrapping
318 hsv.h = rgb_matrix_config.hue + ( deltaH * y );
319 hsv.s = rgb_matrix_config.sat + ( deltaS * y );
320 rgb = hsv_to_rgb( hsv );
321 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
325 void rgb_matrix_raindrops(bool initialize) {
326 int16_t h1 = rgb_matrix_config.hue;
327 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
328 int16_t deltaH = h2 - h1;
331 // Take the shortest path between hues
336 else if ( deltaH < -127 )
341 int16_t s1 = rgb_matrix_config.sat;
342 int16_t s2 = rgb_matrix_config.sat;
343 int16_t deltaS = ( s2 - s1 ) / 4;
348 // Change one LED every tick, make sure speed is not 0
349 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
351 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
353 // If initialize, all get set to random colors
354 // If not, all but one will stay the same as before.
355 if ( initialize || i == led_to_change )
357 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
358 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
359 // Override brightness with global brightness control
360 hsv.v = rgb_matrix_config.val;
362 rgb = hsv_to_rgb( hsv );
363 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
368 void rgb_matrix_cycle_all(void) {
369 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
373 // Relies on hue being 8-bit and wrapping
374 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
376 // map_index_to_led(i, &led);
378 if (led.matrix_co.raw < 0xFF) {
379 uint16_t offset2 = g_key_hit[i]<<2;
380 offset2 = (offset2<=63) ? (63-offset2) : 0;
382 HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
383 RGB rgb = hsv_to_rgb( hsv );
384 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
389 void rgb_matrix_cycle_left_right(void) {
390 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
391 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
395 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
397 // map_index_to_led(i, &led);
399 if (led.matrix_co.raw < 0xFF) {
400 uint16_t offset2 = g_key_hit[i]<<2;
401 offset2 = (offset2<=63) ? (63-offset2) : 0;
403 // map_led_to_point( i, &point );
404 point = g_rgb_leds[i].point;
405 // Relies on hue being 8-bit and wrapping
406 hsv.h = point.x + offset + offset2;
407 rgb = hsv_to_rgb( hsv );
408 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
413 void rgb_matrix_cycle_up_down(void) {
414 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
415 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
419 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
421 // map_index_to_led(i, &led);
423 if (led.matrix_co.raw < 0xFF) {
424 uint16_t offset2 = g_key_hit[i]<<2;
425 offset2 = (offset2<=63) ? (63-offset2) : 0;
427 // map_led_to_point( i, &point );
428 point = g_rgb_leds[i].point;
429 // Relies on hue being 8-bit and wrapping
430 hsv.h = point.y + offset + offset2;
431 rgb = hsv_to_rgb( hsv );
432 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
438 void rgb_matrix_dual_beacon(void) {
439 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
442 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
444 hsv.h = ((led.point.y - 32.0)* cos(g_tick * PI / 128) / 32 + (led.point.x - 112.0) * sin(g_tick * PI / 128) / (112)) * (180) + rgb_matrix_config.hue;
445 rgb = hsv_to_rgb( hsv );
446 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
450 void rgb_matrix_rainbow_beacon(void) {
451 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
454 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
456 hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - 112.0) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
457 rgb = hsv_to_rgb( hsv );
458 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
462 void rgb_matrix_rainbow_pinwheels(void) {
463 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
466 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
468 hsv.h = (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.y - 32.0)* cos(g_tick * PI / 128) + (2 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (66 - abs(led.point.x - 112.0)) * sin(g_tick * PI / 128) + rgb_matrix_config.hue;
469 rgb = hsv_to_rgb( hsv );
470 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
474 void rgb_matrix_rainbow_moving_chevron(void) {
475 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
478 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
480 // uint8_t r = g_tick;
482 hsv.h = (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * abs(led.point.y - 32.0)* sin(r * PI / 128) + (1.5 * (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed)) * (led.point.x - (g_tick / 256.0 * 224)) * cos(r * PI / 128) + rgb_matrix_config.hue;
483 rgb = hsv_to_rgb( hsv );
484 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
489 void rgb_matrix_jellybean_raindrops( bool initialize ) {
493 // Change one LED every tick, make sure speed is not 0
494 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
496 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
498 // If initialize, all get set to random colors
499 // If not, all but one will stay the same as before.
500 if ( initialize || i == led_to_change )
502 hsv.h = rand() & 0xFF;
503 hsv.s = rand() & 0xFF;
504 // Override brightness with global brightness control
505 hsv.v = rgb_matrix_config.val;
507 rgb = hsv_to_rgb( hsv );
508 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
513 void rgb_matrix_digital_rain( const bool initialize ) {
514 // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
515 const uint8_t drop_ticks = 28;
516 const uint8_t new_drop_probability = 24;
517 const uint8_t pure_green_intensity = 0xd0;
518 const uint8_t max_brightness_boost = 0xc0;
519 const uint8_t max_intensity = 0xff;
521 static uint8_t map[MATRIX_COLS][MATRIX_ROWS] = {{0}};
522 static uint8_t drop = 0;
525 rgb_matrix_set_color_all(0, 0, 0);
526 memset(map, 0, sizeof map);
529 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
530 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
531 if (row == 0 && drop == 0 && rand() < RAND_MAX / new_drop_probability) {
532 // top row, pixels have just fallen and we're
533 // making a new rain drop in this column
534 map[col][row] = max_intensity;
536 else if (map[col][row] > 0 && map[col][row] < max_intensity) {
537 // neither fully bright nor dark, decay it
540 // set the pixel colour
541 uint8_t led, led_count;
542 map_row_column_to_led(row, col, &led, &led_count);
544 if (map[col][row] > pure_green_intensity) {
545 const uint8_t boost = (uint8_t) ((uint16_t) max_brightness_boost
546 * (map[col][row] - pure_green_intensity) / (max_intensity - pure_green_intensity));
547 rgb_matrix_set_color(led, boost, max_intensity, boost);
550 const uint8_t green = (uint8_t) ((uint16_t) max_intensity * map[col][row] / pure_green_intensity);
551 rgb_matrix_set_color(led, 0, green, 0);
555 if (++drop > drop_ticks) {
558 for (uint8_t row = MATRIX_ROWS - 1; row > 0; row--) {
559 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
560 // if ths is on the bottom row and bright allow decay
561 if (row == MATRIX_ROWS - 1 && map[col][row] == max_intensity) {
564 // check if the pixel above is bright
565 if (map[col][row - 1] == max_intensity) {
566 // allow old bright pixel to decay
568 // make this pixel bright
569 map[col][row] = max_intensity;
576 void rgb_matrix_multisplash(void) {
577 // if (g_any_key_hit < 0xFF) {
578 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
581 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
583 uint16_t c = 0, d = 0;
585 // if (g_last_led_count) {
586 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
587 last_led = g_rgb_leds[g_last_led_hit[last_i]];
588 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
589 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
590 c += MIN(MAX(effect, 0), 255);
591 d += 255 - MIN(MAX(effect, 0), 255);
596 hsv.h = (rgb_matrix_config.hue + c) % 256;
597 hsv.v = MAX(MIN(d, 255), 0);
598 rgb = hsv_to_rgb( hsv );
599 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
602 // rgb_matrix_set_color_all( 0, 0, 0 );
607 void rgb_matrix_splash(void) {
608 g_last_led_count = MIN(g_last_led_count, 1);
609 rgb_matrix_multisplash();
613 void rgb_matrix_solid_multisplash(void) {
614 // if (g_any_key_hit < 0xFF) {
615 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
618 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
622 // if (g_last_led_count) {
623 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
624 last_led = g_rgb_leds[g_last_led_hit[last_i]];
625 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
626 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
627 d += 255 - MIN(MAX(effect, 0), 255);
632 hsv.v = MAX(MIN(d, 255), 0);
633 rgb = hsv_to_rgb( hsv );
634 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
637 // rgb_matrix_set_color_all( 0, 0, 0 );
642 void rgb_matrix_solid_splash(void) {
643 g_last_led_count = MIN(g_last_led_count, 1);
644 rgb_matrix_solid_multisplash();
648 // Needs eeprom access that we don't have setup currently
650 void rgb_matrix_custom(void) {
653 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
655 // backlight_get_key_color(i, &hsv);
656 // // Override brightness with global brightness control
657 // hsv.v = rgb_matrix_config.val;
658 // rgb = hsv_to_rgb( hsv );
659 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
663 void rgb_matrix_task(void) {
664 static uint8_t toggle_enable_last = 255;
665 if (!rgb_matrix_config.enable) {
666 rgb_matrix_all_off();
667 toggle_enable_last = rgb_matrix_config.enable;
670 // delay 1 second before driving LEDs or doing anything else
671 static uint8_t startup_tick = 0;
672 if ( startup_tick < 20 ) {
679 if ( g_any_key_hit < 0xFFFFFFFF ) {
683 for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
684 if ( g_key_hit[led] < 255 ) {
685 if (g_key_hit[led] == 254)
686 g_last_led_count = MAX(g_last_led_count - 1, 0);
691 // Factory default magic value
692 if ( rgb_matrix_config.mode == 255 ) {
697 // Ideally we would also stop sending zeros to the LED driver PWM buffers
698 // while suspended and just do a software shutdown. This is a cheap hack for now.
699 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
700 (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
701 uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
703 // Keep track of the effect used last time,
704 // detect change in effect, so each effect can
705 // have an optional initialization.
706 static uint8_t effect_last = 255;
707 bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
708 effect_last = effect;
709 toggle_enable_last = rgb_matrix_config.enable;
711 // this gets ticked at 20 Hz.
712 // each effect can opt to do calculations
713 // and/or request PWM buffer updates.
715 case RGB_MATRIX_SOLID_COLOR:
716 rgb_matrix_solid_color();
718 case RGB_MATRIX_ALPHAS_MODS:
719 rgb_matrix_alphas_mods();
721 case RGB_MATRIX_DUAL_BEACON:
722 rgb_matrix_dual_beacon();
724 case RGB_MATRIX_GRADIENT_UP_DOWN:
725 rgb_matrix_gradient_up_down();
727 case RGB_MATRIX_RAINDROPS:
728 rgb_matrix_raindrops( initialize );
730 case RGB_MATRIX_CYCLE_ALL:
731 rgb_matrix_cycle_all();
733 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
734 rgb_matrix_cycle_left_right();
736 case RGB_MATRIX_CYCLE_UP_DOWN:
737 rgb_matrix_cycle_up_down();
739 case RGB_MATRIX_RAINBOW_BEACON:
740 rgb_matrix_rainbow_beacon();
742 case RGB_MATRIX_RAINBOW_PINWHEELS:
743 rgb_matrix_rainbow_pinwheels();
745 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
746 rgb_matrix_rainbow_moving_chevron();
748 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
749 rgb_matrix_jellybean_raindrops( initialize );
751 case RGB_MATRIX_DIGITAL_RAIN:
752 rgb_matrix_digital_rain( initialize );
754 #ifdef RGB_MATRIX_KEYPRESSES
755 case RGB_MATRIX_SOLID_REACTIVE:
756 rgb_matrix_solid_reactive();
758 case RGB_MATRIX_SPLASH:
761 case RGB_MATRIX_MULTISPLASH:
762 rgb_matrix_multisplash();
764 case RGB_MATRIX_SOLID_SPLASH:
765 rgb_matrix_solid_splash();
767 case RGB_MATRIX_SOLID_MULTISPLASH:
768 rgb_matrix_solid_multisplash();
776 if ( ! suspend_backlight ) {
777 rgb_matrix_indicators();
782 void rgb_matrix_indicators(void) {
783 rgb_matrix_indicators_kb();
784 rgb_matrix_indicators_user();
787 __attribute__((weak))
788 void rgb_matrix_indicators_kb(void) {}
790 __attribute__((weak))
791 void rgb_matrix_indicators_user(void) {}
794 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
796 // if ( row >= MATRIX_ROWS )
798 // // Special value, 255=none, 254=all
803 // // This needs updated to something like
804 // // uint8_t led[8], led_count;
805 // // map_row_column_to_led(row,column,led,&led_count);
806 // // for(uint8_t i = 0; i < led_count; i++)
807 // map_row_column_to_led( row, column, index );
811 void rgb_matrix_init(void) {
812 rgb_matrix_setup_drivers();
814 // TODO: put the 1 second startup delay here?
816 // clear the key hits
817 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
818 g_key_hit[led] = 255;
822 if (!eeconfig_is_enabled()) {
823 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
825 eeconfig_update_rgb_matrix_default();
827 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
828 if (!rgb_matrix_config.mode) {
829 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
830 eeconfig_update_rgb_matrix_default();
831 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
833 eeconfig_debug_rgb_matrix(); // display current eeprom values
836 void rgb_matrix_setup_drivers(void) {
840 IS31FL3731_init( DRIVER_ADDR_1 );
841 IS31FL3731_init( DRIVER_ADDR_2 );
842 #elif defined (IS31FL3733)
843 IS31FL3733_init( DRIVER_ADDR_1 );
846 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
848 // This only caches it for later
850 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
851 #elif defined (IS31FL3733)
852 IS31FL3733_set_led_control_register( index, enabled, enabled, enabled );
855 // This actually updates the LED drivers
857 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
858 #elif defined (IS31FL3733)
859 IS31FL3733_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
863 // Deals with the messy details of incrementing an integer
864 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
865 int16_t new_value = value;
867 return MIN( MAX( new_value, min ), max );
870 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
871 int16_t new_value = value;
873 return MIN( MAX( new_value, min ), max );
876 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
878 // // 3 bytes per color
879 // return EECONFIG_RGB_MATRIX + ( led * 3 );
882 // void backlight_get_key_color( uint8_t led, HSV *hsv )
884 // void *address = backlight_get_custom_key_color_eeprom_address( led );
885 // hsv->h = eeprom_read_byte(address);
886 // hsv->s = eeprom_read_byte(address+1);
887 // hsv->v = eeprom_read_byte(address+2);
890 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
892 // uint8_t led[8], led_count;
893 // map_row_column_to_led(row,column,led,&led_count);
894 // for(uint8_t i = 0; i < led_count; i++) {
895 // if ( led[i] < DRIVER_LED_TOTAL )
897 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
898 // eeprom_update_byte(address, hsv.h);
899 // eeprom_update_byte(address+1, hsv.s);
900 // eeprom_update_byte(address+2, hsv.v);
905 void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
906 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
911 IS31FL3731_set_led_control_register( i, red, green, blue );
912 #elif defined (IS31FL3733)
913 IS31FL3733_set_led_control_register( i, red, green, blue );
919 IS31FL3731_set_led_control_register( i, false, false, false );
920 #elif defined (IS31FL3733)
921 IS31FL3733_set_led_control_register( i, false, false, false );
927 uint32_t rgb_matrix_get_tick(void) {
931 void rgblight_toggle(void) {
932 rgb_matrix_config.enable ^= 1;
933 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
936 void rgblight_step(void) {
937 rgb_matrix_config.mode++;
938 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
939 rgb_matrix_config.mode = 1;
940 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
943 void rgblight_step_reverse(void) {
944 rgb_matrix_config.mode--;
945 if (rgb_matrix_config.mode < 1)
946 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
947 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
950 void rgblight_increase_hue(void) {
951 rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
952 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
955 void rgblight_decrease_hue(void) {
956 rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
957 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
960 void rgblight_increase_sat(void) {
961 rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
962 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
965 void rgblight_decrease_sat(void) {
966 rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
967 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
970 void rgblight_increase_val(void) {
971 rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
972 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
975 void rgblight_decrease_val(void) {
976 rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
977 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
980 void rgblight_increase_speed(void) {
981 rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
982 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
985 void rgblight_decrease_speed(void) {
986 rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
987 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
990 void rgblight_mode(uint8_t mode) {
991 rgb_matrix_config.mode = mode;
992 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
995 uint32_t rgblight_get_mode(void) {
996 return rgb_matrix_config.mode;