1 /* Copyright 2017 Jason Williams
2 * Copyright 2017 Jack Humbert
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "rgb_matrix.h"
21 #include "i2c_master.h"
22 #include <util/delay.h>
23 #include <avr/interrupt.h>
30 rgb_config_t rgb_matrix_config;
32 #ifndef RGB_DISABLE_AFTER_TIMEOUT
33 #define RGB_DISABLE_AFTER_TIMEOUT 0
36 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
37 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
40 #ifndef EECONFIG_RGB_MATRIX
41 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
44 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255
45 #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255
48 bool g_suspend_state = false;
50 // Global tick at 20 Hz
53 // Ticks since this key was last hit.
54 uint8_t g_key_hit[DRIVER_LED_TOTAL];
56 // Ticks since any key was last hit.
57 uint32_t g_any_key_hit = 0;
63 uint32_t eeconfig_read_rgb_matrix(void) {
64 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
66 void eeconfig_update_rgb_matrix(uint32_t val) {
67 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
69 void eeconfig_update_rgb_matrix_default(void) {
70 dprintf("eeconfig_update_rgb_matrix_default\n");
71 rgb_matrix_config.enable = 1;
72 rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT;
73 rgb_matrix_config.hue = 0;
74 rgb_matrix_config.sat = 255;
75 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
76 rgb_matrix_config.speed = 0;
77 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
79 void eeconfig_debug_rgb_matrix(void) {
80 dprintf("rgb_matrix_config eprom\n");
81 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
82 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
83 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
84 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
85 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
86 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
90 #define LED_HITS_TO_REMEMBER 8
91 uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
92 uint8_t g_last_led_count = 0;
94 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
98 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
99 // map_index_to_led(i, &led);
101 if (row == led.matrix_co.row && column == led.matrix_co.col) {
102 led_i[*led_count] = i;
108 void rgb_matrix_update_pwm_buffers(void) {
109 IS31_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
110 IS31_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
113 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
114 IS31_set_color( index, red, green, blue );
117 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
118 IS31_set_color_all( red, green, blue );
121 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
122 if ( record->event.pressed ) {
123 uint8_t led[8], led_count;
124 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
126 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
127 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
129 g_last_led_hit[0] = led[0];
130 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
132 for(uint8_t i = 0; i < led_count; i++)
133 g_key_hit[led[i]] = 0;
136 #ifdef RGB_MATRIX_KEYRELEASES
137 uint8_t led[8], led_count;
138 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
139 for(uint8_t i = 0; i < led_count; i++)
140 g_key_hit[led[i]] = 255;
148 void rgb_matrix_set_suspend_state(bool state) {
149 g_suspend_state = state;
152 void rgb_matrix_test(void) {
153 // Mask out bits 4 and 5
154 // Increase the factor to make the test animation slower (and reduce to make it faster)
156 switch ( (g_tick & (0b11 << factor)) >> factor )
160 rgb_matrix_set_color_all( 20, 0, 0 );
165 rgb_matrix_set_color_all( 0, 20, 0 );
170 rgb_matrix_set_color_all( 0, 0, 20 );
175 rgb_matrix_set_color_all( 20, 20, 20 );
181 // This tests the LEDs
182 // Note that it will change the LED control registers
183 // in the LED drivers, and leave them in an invalid
184 // state for other backlight effects.
185 // ONLY USE THIS FOR TESTING LEDS!
186 void rgb_matrix_single_LED_test(void) {
187 static uint8_t color = 0; // 0,1,2 for R,G,B
188 static uint8_t row = 0;
189 static uint8_t column = 0;
191 static uint8_t tick = 0;
199 if ( column > MATRIX_COLS )
204 if ( row > MATRIX_ROWS )
214 uint8_t led[8], led_count;
215 map_row_column_to_led(row,column,led,&led_count);
216 for(uint8_t i = 0; i < led_count; i++) {
217 rgb_matrix_set_color_all( 40, 40, 40 );
218 rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
223 void rgb_matrix_all_off(void) {
224 rgb_matrix_set_color_all( 0, 0, 0 );
228 void rgb_matrix_solid_color(void) {
229 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
230 RGB rgb = hsv_to_rgb( hsv );
231 rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
234 void rgb_matrix_solid_reactive(void) {
235 // Relies on hue being 8-bit and wrapping
236 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
238 uint16_t offset2 = g_key_hit[i]<<2;
239 offset2 = (offset2<=130) ? (130-offset2) : 0;
241 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
242 RGB rgb = hsv_to_rgb( hsv );
243 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
247 // alphas = color1, mods = color2
248 void rgb_matrix_alphas_mods(void) {
250 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
251 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
254 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
256 if ( led.matrix_co.raw < 0xFF ) {
259 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
263 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
269 void rgb_matrix_gradient_up_down(void) {
270 int16_t h1 = rgb_matrix_config.hue;
271 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
272 int16_t deltaH = h2 - h1;
274 // Take the shortest path between hues
279 else if ( deltaH < -127 )
283 // Divide delta by 4, this gives the delta per row
286 int16_t s1 = rgb_matrix_config.sat;
287 int16_t s2 = rgb_matrix_config.hue;
288 int16_t deltaS = ( s2 - s1 ) / 4;
290 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
293 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
295 // map_led_to_point( i, &point );
296 point = g_rgb_leds[i].point;
297 // The y range will be 0..64, map this to 0..4
298 uint8_t y = (point.y>>4);
299 // Relies on hue being 8-bit and wrapping
300 hsv.h = rgb_matrix_config.hue + ( deltaH * y );
301 hsv.s = rgb_matrix_config.sat + ( deltaS * y );
302 rgb = hsv_to_rgb( hsv );
303 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
307 void rgb_matrix_raindrops(bool initialize) {
308 int16_t h1 = rgb_matrix_config.hue;
309 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
310 int16_t deltaH = h2 - h1;
313 // Take the shortest path between hues
318 else if ( deltaH < -127 )
323 int16_t s1 = rgb_matrix_config.sat;
324 int16_t s2 = rgb_matrix_config.sat;
325 int16_t deltaS = ( s2 - s1 ) / 4;
330 // Change one LED every tick, make sure speed is not 0
331 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
333 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
335 // If initialize, all get set to random colors
336 // If not, all but one will stay the same as before.
337 if ( initialize || i == led_to_change )
339 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
340 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
341 // Override brightness with global brightness control
342 hsv.v = rgb_matrix_config.val;
344 rgb = hsv_to_rgb( hsv );
345 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
350 void rgb_matrix_cycle_all(void) {
351 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
355 // Relies on hue being 8-bit and wrapping
356 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
358 // map_index_to_led(i, &led);
360 if (led.matrix_co.raw < 0xFF) {
361 uint16_t offset2 = g_key_hit[i]<<2;
362 offset2 = (offset2<=63) ? (63-offset2) : 0;
364 HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
365 RGB rgb = hsv_to_rgb( hsv );
366 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
371 void rgb_matrix_cycle_left_right(void) {
372 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
373 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
377 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
379 // map_index_to_led(i, &led);
381 if (led.matrix_co.raw < 0xFF) {
382 uint16_t offset2 = g_key_hit[i]<<2;
383 offset2 = (offset2<=63) ? (63-offset2) : 0;
385 // map_led_to_point( i, &point );
386 point = g_rgb_leds[i].point;
387 // Relies on hue being 8-bit and wrapping
388 hsv.h = point.x + offset + offset2;
389 rgb = hsv_to_rgb( hsv );
390 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
395 void rgb_matrix_cycle_up_down(void) {
396 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
397 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
401 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
403 // map_index_to_led(i, &led);
405 if (led.matrix_co.raw < 0xFF) {
406 uint16_t offset2 = g_key_hit[i]<<2;
407 offset2 = (offset2<=63) ? (63-offset2) : 0;
409 // map_led_to_point( i, &point );
410 point = g_rgb_leds[i].point;
411 // Relies on hue being 8-bit and wrapping
412 hsv.h = point.y + offset + offset2;
413 rgb = hsv_to_rgb( hsv );
414 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
420 void rgb_matrix_dual_beacon(void) {
421 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
424 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
426 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;
427 rgb = hsv_to_rgb( hsv );
428 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
432 void rgb_matrix_rainbow_beacon(void) {
433 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
436 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
438 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;
439 rgb = hsv_to_rgb( hsv );
440 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
444 void rgb_matrix_rainbow_pinwheels(void) {
445 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
448 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
450 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;
451 rgb = hsv_to_rgb( hsv );
452 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
456 void rgb_matrix_rainbow_moving_chevron(void) {
457 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
460 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
462 // uint8_t r = g_tick;
464 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;
465 rgb = hsv_to_rgb( hsv );
466 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
471 void rgb_matrix_jellybean_raindrops( bool initialize ) {
475 // Change one LED every tick, make sure speed is not 0
476 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
478 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
480 // If initialize, all get set to random colors
481 // If not, all but one will stay the same as before.
482 if ( initialize || i == led_to_change )
484 hsv.h = rand() & 0xFF;
485 hsv.s = rand() & 0xFF;
486 // Override brightness with global brightness control
487 hsv.v = rgb_matrix_config.val;
489 rgb = hsv_to_rgb( hsv );
490 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
495 void rgb_matrix_multisplash(void) {
496 // if (g_any_key_hit < 0xFF) {
497 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
500 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
502 uint16_t c = 0, d = 0;
504 // if (g_last_led_count) {
505 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
506 last_led = g_rgb_leds[g_last_led_hit[last_i]];
507 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
508 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
509 c += MIN(MAX(effect, 0), 255);
510 d += 255 - MIN(MAX(effect, 0), 255);
515 hsv.h = (rgb_matrix_config.hue + c) % 256;
516 hsv.v = MAX(MIN(d, 255), 0);
517 rgb = hsv_to_rgb( hsv );
518 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
521 // rgb_matrix_set_color_all( 0, 0, 0 );
526 void rgb_matrix_splash(void) {
527 g_last_led_count = MIN(g_last_led_count, 1);
528 rgb_matrix_multisplash();
532 void rgb_matrix_solid_multisplash(void) {
533 // if (g_any_key_hit < 0xFF) {
534 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
537 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
541 // if (g_last_led_count) {
542 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
543 last_led = g_rgb_leds[g_last_led_hit[last_i]];
544 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
545 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
546 d += 255 - MIN(MAX(effect, 0), 255);
551 hsv.v = MAX(MIN(d, 255), 0);
552 rgb = hsv_to_rgb( hsv );
553 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
556 // rgb_matrix_set_color_all( 0, 0, 0 );
561 void rgb_matrix_solid_splash(void) {
562 g_last_led_count = MIN(g_last_led_count, 1);
563 rgb_matrix_solid_multisplash();
567 // Needs eeprom access that we don't have setup currently
569 void rgb_matrix_custom(void) {
572 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
574 // backlight_get_key_color(i, &hsv);
575 // // Override brightness with global brightness control
576 // hsv.v = rgb_matrix_config.val;
577 // rgb = hsv_to_rgb( hsv );
578 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
582 void rgb_matrix_task(void) {
583 static uint8_t toggle_enable_last = 255;
584 if (!rgb_matrix_config.enable) {
585 rgb_matrix_all_off();
586 toggle_enable_last = rgb_matrix_config.enable;
589 // delay 1 second before driving LEDs or doing anything else
590 static uint8_t startup_tick = 0;
591 if ( startup_tick < 20 ) {
598 if ( g_any_key_hit < 0xFFFFFFFF ) {
602 for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
603 if ( g_key_hit[led] < 255 ) {
604 if (g_key_hit[led] == 254)
605 g_last_led_count = MAX(g_last_led_count - 1, 0);
610 // Factory default magic value
611 if ( rgb_matrix_config.mode == 255 ) {
616 // Ideally we would also stop sending zeros to the LED driver PWM buffers
617 // while suspended and just do a software shutdown. This is a cheap hack for now.
618 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
619 (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
620 uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
622 // Keep track of the effect used last time,
623 // detect change in effect, so each effect can
624 // have an optional initialization.
625 static uint8_t effect_last = 255;
626 bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
627 effect_last = effect;
628 toggle_enable_last = rgb_matrix_config.enable;
630 // this gets ticked at 20 Hz.
631 // each effect can opt to do calculations
632 // and/or request PWM buffer updates.
634 case RGB_MATRIX_SOLID_COLOR:
635 rgb_matrix_solid_color();
637 case RGB_MATRIX_ALPHAS_MODS:
638 rgb_matrix_alphas_mods();
640 case RGB_MATRIX_DUAL_BEACON:
641 rgb_matrix_dual_beacon();
643 case RGB_MATRIX_GRADIENT_UP_DOWN:
644 rgb_matrix_gradient_up_down();
646 case RGB_MATRIX_RAINDROPS:
647 rgb_matrix_raindrops( initialize );
649 case RGB_MATRIX_CYCLE_ALL:
650 rgb_matrix_cycle_all();
652 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
653 rgb_matrix_cycle_left_right();
655 case RGB_MATRIX_CYCLE_UP_DOWN:
656 rgb_matrix_cycle_up_down();
658 case RGB_MATRIX_RAINBOW_BEACON:
659 rgb_matrix_rainbow_beacon();
661 case RGB_MATRIX_RAINBOW_PINWHEELS:
662 rgb_matrix_rainbow_pinwheels();
664 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
665 rgb_matrix_rainbow_moving_chevron();
667 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
668 rgb_matrix_jellybean_raindrops( initialize );
670 #ifdef RGB_MATRIX_KEYPRESSES
671 case RGB_MATRIX_SOLID_REACTIVE:
672 rgb_matrix_solid_reactive();
674 case RGB_MATRIX_SPLASH:
677 case RGB_MATRIX_MULTISPLASH:
678 rgb_matrix_multisplash();
680 case RGB_MATRIX_SOLID_SPLASH:
681 rgb_matrix_solid_splash();
683 case RGB_MATRIX_SOLID_MULTISPLASH:
684 rgb_matrix_solid_multisplash();
692 if ( ! suspend_backlight ) {
693 rgb_matrix_indicators();
698 void rgb_matrix_indicators(void) {
699 rgb_matrix_indicators_kb();
700 rgb_matrix_indicators_user();
703 __attribute__((weak))
704 void rgb_matrix_indicators_kb(void) {}
706 __attribute__((weak))
707 void rgb_matrix_indicators_user(void) {}
710 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
712 // if ( row >= MATRIX_ROWS )
714 // // Special value, 255=none, 254=all
719 // // This needs updated to something like
720 // // uint8_t led[8], led_count;
721 // // map_row_column_to_led(row,column,led,&led_count);
722 // // for(uint8_t i = 0; i < led_count; i++)
723 // map_row_column_to_led( row, column, index );
727 void rgb_matrix_init(void) {
728 rgb_matrix_setup_drivers();
730 // TODO: put the 1 second startup delay here?
732 // clear the key hits
733 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
734 g_key_hit[led] = 255;
738 if (!eeconfig_is_enabled()) {
739 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
741 eeconfig_update_rgb_matrix_default();
743 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
744 if (!rgb_matrix_config.mode) {
745 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
746 eeconfig_update_rgb_matrix_default();
747 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
749 eeconfig_debug_rgb_matrix(); // display current eeprom values
752 void rgb_matrix_setup_drivers(void) {
755 IS31_init( DRIVER_ADDR_1 );
756 IS31_init( DRIVER_ADDR_2 );
758 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
760 // This only caches it for later
761 IS31_set_led_control_register( index, enabled, enabled, enabled );
763 // This actually updates the LED drivers
764 IS31_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
767 // Deals with the messy details of incrementing an integer
768 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
769 int16_t new_value = value;
771 return MIN( MAX( new_value, min ), max );
774 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
775 int16_t new_value = value;
777 return MIN( MAX( new_value, min ), max );
780 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
782 // // 3 bytes per color
783 // return EECONFIG_RGB_MATRIX + ( led * 3 );
786 // void backlight_get_key_color( uint8_t led, HSV *hsv )
788 // void *address = backlight_get_custom_key_color_eeprom_address( led );
789 // hsv->h = eeprom_read_byte(address);
790 // hsv->s = eeprom_read_byte(address+1);
791 // hsv->v = eeprom_read_byte(address+2);
794 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
796 // uint8_t led[8], led_count;
797 // map_row_column_to_led(row,column,led,&led_count);
798 // for(uint8_t i = 0; i < led_count; i++) {
799 // if ( led[i] < DRIVER_LED_TOTAL )
801 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
802 // eeprom_update_byte(address, hsv.h);
803 // eeprom_update_byte(address+1, hsv.s);
804 // eeprom_update_byte(address+2, hsv.v);
809 void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
810 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
814 IS31_set_led_control_register( i, red, green, blue );
818 IS31_set_led_control_register( i, false, false, false );
823 uint32_t rgb_matrix_get_tick(void) {
827 void rgblight_toggle(void) {
828 rgb_matrix_config.enable ^= 1;
829 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
832 void rgblight_step(void) {
833 rgb_matrix_config.mode++;
834 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
835 rgb_matrix_config.mode = 1;
836 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
839 void rgblight_step_reverse(void) {
840 rgb_matrix_config.mode--;
841 if (rgb_matrix_config.mode < 1)
842 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
843 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
846 void rgblight_increase_hue(void) {
847 rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
848 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
851 void rgblight_decrease_hue(void) {
852 rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
853 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
856 void rgblight_increase_sat(void) {
857 rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
858 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
861 void rgblight_decrease_sat(void) {
862 rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
863 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
866 void rgblight_increase_val(void) {
867 rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
868 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
871 void rgblight_decrease_val(void) {
872 rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
873 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
876 void rgblight_increase_speed(void) {
877 rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
878 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
881 void rgblight_decrease_speed(void) {
882 rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
883 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
886 void rgblight_mode(uint8_t mode) {
887 rgb_matrix_config.mode = mode;
888 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
891 uint32_t rgblight_get_mode(void) {
892 return rgb_matrix_config.mode;