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;
109 void rgb_matrix_update_pwm_buffers(void) {
110 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
111 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
114 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
115 IS31FL3731_set_color( index, red, green, blue );
118 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
119 IS31FL3731_set_color_all( red, green, blue );
123 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
124 if ( record->event.pressed ) {
125 uint8_t led[8], led_count;
126 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
128 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
129 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
131 g_last_led_hit[0] = led[0];
132 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
134 for(uint8_t i = 0; i < led_count; i++)
135 g_key_hit[led[i]] = 0;
138 #ifdef RGB_MATRIX_KEYRELEASES
139 uint8_t led[8], led_count;
140 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
141 for(uint8_t i = 0; i < led_count; i++)
142 g_key_hit[led[i]] = 255;
150 void rgb_matrix_set_suspend_state(bool state) {
151 g_suspend_state = state;
154 void rgb_matrix_test(void) {
155 // Mask out bits 4 and 5
156 // Increase the factor to make the test animation slower (and reduce to make it faster)
158 switch ( (g_tick & (0b11 << factor)) >> factor )
162 rgb_matrix_set_color_all( 20, 0, 0 );
167 rgb_matrix_set_color_all( 0, 20, 0 );
172 rgb_matrix_set_color_all( 0, 0, 20 );
177 rgb_matrix_set_color_all( 20, 20, 20 );
183 // This tests the LEDs
184 // Note that it will change the LED control registers
185 // in the LED drivers, and leave them in an invalid
186 // state for other backlight effects.
187 // ONLY USE THIS FOR TESTING LEDS!
188 void rgb_matrix_single_LED_test(void) {
189 static uint8_t color = 0; // 0,1,2 for R,G,B
190 static uint8_t row = 0;
191 static uint8_t column = 0;
193 static uint8_t tick = 0;
201 if ( column > MATRIX_COLS )
206 if ( row > MATRIX_ROWS )
216 uint8_t led[8], led_count;
217 map_row_column_to_led(row,column,led,&led_count);
218 for(uint8_t i = 0; i < led_count; i++) {
219 rgb_matrix_set_color_all( 40, 40, 40 );
220 rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
225 void rgb_matrix_all_off(void) {
226 rgb_matrix_set_color_all( 0, 0, 0 );
230 void rgb_matrix_solid_color(void) {
231 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
232 RGB rgb = hsv_to_rgb( hsv );
233 rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
236 void rgb_matrix_solid_reactive(void) {
237 // Relies on hue being 8-bit and wrapping
238 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
240 uint16_t offset2 = g_key_hit[i]<<2;
241 offset2 = (offset2<=130) ? (130-offset2) : 0;
243 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
244 RGB rgb = hsv_to_rgb( hsv );
245 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
249 // alphas = color1, mods = color2
250 void rgb_matrix_alphas_mods(void) {
252 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
253 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
256 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
258 if ( led.matrix_co.raw < 0xFF ) {
261 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
265 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
271 void rgb_matrix_gradient_up_down(void) {
272 int16_t h1 = rgb_matrix_config.hue;
273 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
274 int16_t deltaH = h2 - h1;
276 // Take the shortest path between hues
281 else if ( deltaH < -127 )
285 // Divide delta by 4, this gives the delta per row
288 int16_t s1 = rgb_matrix_config.sat;
289 int16_t s2 = rgb_matrix_config.hue;
290 int16_t deltaS = ( s2 - s1 ) / 4;
292 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
295 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
297 // map_led_to_point( i, &point );
298 point = g_rgb_leds[i].point;
299 // The y range will be 0..64, map this to 0..4
300 uint8_t y = (point.y>>4);
301 // Relies on hue being 8-bit and wrapping
302 hsv.h = rgb_matrix_config.hue + ( deltaH * y );
303 hsv.s = rgb_matrix_config.sat + ( deltaS * y );
304 rgb = hsv_to_rgb( hsv );
305 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
309 void rgb_matrix_raindrops(bool initialize) {
310 int16_t h1 = rgb_matrix_config.hue;
311 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
312 int16_t deltaH = h2 - h1;
315 // Take the shortest path between hues
320 else if ( deltaH < -127 )
325 int16_t s1 = rgb_matrix_config.sat;
326 int16_t s2 = rgb_matrix_config.sat;
327 int16_t deltaS = ( s2 - s1 ) / 4;
332 // Change one LED every tick, make sure speed is not 0
333 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
335 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
337 // If initialize, all get set to random colors
338 // If not, all but one will stay the same as before.
339 if ( initialize || i == led_to_change )
341 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
342 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
343 // Override brightness with global brightness control
344 hsv.v = rgb_matrix_config.val;
346 rgb = hsv_to_rgb( hsv );
347 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
352 void rgb_matrix_cycle_all(void) {
353 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
357 // Relies on hue being 8-bit and wrapping
358 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
360 // map_index_to_led(i, &led);
362 if (led.matrix_co.raw < 0xFF) {
363 uint16_t offset2 = g_key_hit[i]<<2;
364 offset2 = (offset2<=63) ? (63-offset2) : 0;
366 HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
367 RGB rgb = hsv_to_rgb( hsv );
368 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
373 void rgb_matrix_cycle_left_right(void) {
374 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
375 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
379 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
381 // map_index_to_led(i, &led);
383 if (led.matrix_co.raw < 0xFF) {
384 uint16_t offset2 = g_key_hit[i]<<2;
385 offset2 = (offset2<=63) ? (63-offset2) : 0;
387 // map_led_to_point( i, &point );
388 point = g_rgb_leds[i].point;
389 // Relies on hue being 8-bit and wrapping
390 hsv.h = point.x + offset + offset2;
391 rgb = hsv_to_rgb( hsv );
392 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
397 void rgb_matrix_cycle_up_down(void) {
398 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
399 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
403 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
405 // map_index_to_led(i, &led);
407 if (led.matrix_co.raw < 0xFF) {
408 uint16_t offset2 = g_key_hit[i]<<2;
409 offset2 = (offset2<=63) ? (63-offset2) : 0;
411 // map_led_to_point( i, &point );
412 point = g_rgb_leds[i].point;
413 // Relies on hue being 8-bit and wrapping
414 hsv.h = point.y + offset + offset2;
415 rgb = hsv_to_rgb( hsv );
416 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
422 void rgb_matrix_dual_beacon(void) {
423 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
426 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
428 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;
429 rgb = hsv_to_rgb( hsv );
430 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
434 void rgb_matrix_rainbow_beacon(void) {
435 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
438 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
440 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;
441 rgb = hsv_to_rgb( hsv );
442 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
446 void rgb_matrix_rainbow_pinwheels(void) {
447 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
450 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
452 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;
453 rgb = hsv_to_rgb( hsv );
454 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
458 void rgb_matrix_rainbow_moving_chevron(void) {
459 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
462 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
464 // uint8_t r = g_tick;
466 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;
467 rgb = hsv_to_rgb( hsv );
468 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
473 void rgb_matrix_jellybean_raindrops( bool initialize ) {
477 // Change one LED every tick, make sure speed is not 0
478 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
480 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
482 // If initialize, all get set to random colors
483 // If not, all but one will stay the same as before.
484 if ( initialize || i == led_to_change )
486 hsv.h = rand() & 0xFF;
487 hsv.s = rand() & 0xFF;
488 // Override brightness with global brightness control
489 hsv.v = rgb_matrix_config.val;
491 rgb = hsv_to_rgb( hsv );
492 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
497 void rgb_matrix_multisplash(void) {
498 // if (g_any_key_hit < 0xFF) {
499 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
502 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
504 uint16_t c = 0, d = 0;
506 // if (g_last_led_count) {
507 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
508 last_led = g_rgb_leds[g_last_led_hit[last_i]];
509 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
510 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
511 c += MIN(MAX(effect, 0), 255);
512 d += 255 - MIN(MAX(effect, 0), 255);
517 hsv.h = (rgb_matrix_config.hue + c) % 256;
518 hsv.v = MAX(MIN(d, 255), 0);
519 rgb = hsv_to_rgb( hsv );
520 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
523 // rgb_matrix_set_color_all( 0, 0, 0 );
528 void rgb_matrix_splash(void) {
529 g_last_led_count = MIN(g_last_led_count, 1);
530 rgb_matrix_multisplash();
534 void rgb_matrix_solid_multisplash(void) {
535 // if (g_any_key_hit < 0xFF) {
536 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
539 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
543 // if (g_last_led_count) {
544 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
545 last_led = g_rgb_leds[g_last_led_hit[last_i]];
546 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
547 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
548 d += 255 - MIN(MAX(effect, 0), 255);
553 hsv.v = MAX(MIN(d, 255), 0);
554 rgb = hsv_to_rgb( hsv );
555 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
558 // rgb_matrix_set_color_all( 0, 0, 0 );
563 void rgb_matrix_solid_splash(void) {
564 g_last_led_count = MIN(g_last_led_count, 1);
565 rgb_matrix_solid_multisplash();
569 // Needs eeprom access that we don't have setup currently
571 void rgb_matrix_custom(void) {
574 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
576 // backlight_get_key_color(i, &hsv);
577 // // Override brightness with global brightness control
578 // hsv.v = rgb_matrix_config.val;
579 // rgb = hsv_to_rgb( hsv );
580 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
584 void rgb_matrix_task(void) {
585 static uint8_t toggle_enable_last = 255;
586 if (!rgb_matrix_config.enable) {
587 rgb_matrix_all_off();
588 toggle_enable_last = rgb_matrix_config.enable;
591 // delay 1 second before driving LEDs or doing anything else
592 static uint8_t startup_tick = 0;
593 if ( startup_tick < 20 ) {
600 if ( g_any_key_hit < 0xFFFFFFFF ) {
604 for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
605 if ( g_key_hit[led] < 255 ) {
606 if (g_key_hit[led] == 254)
607 g_last_led_count = MAX(g_last_led_count - 1, 0);
612 // Factory default magic value
613 if ( rgb_matrix_config.mode == 255 ) {
618 // Ideally we would also stop sending zeros to the LED driver PWM buffers
619 // while suspended and just do a software shutdown. This is a cheap hack for now.
620 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
621 (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
622 uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
624 // Keep track of the effect used last time,
625 // detect change in effect, so each effect can
626 // have an optional initialization.
627 static uint8_t effect_last = 255;
628 bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
629 effect_last = effect;
630 toggle_enable_last = rgb_matrix_config.enable;
632 // this gets ticked at 20 Hz.
633 // each effect can opt to do calculations
634 // and/or request PWM buffer updates.
636 case RGB_MATRIX_SOLID_COLOR:
637 rgb_matrix_solid_color();
639 case RGB_MATRIX_ALPHAS_MODS:
640 rgb_matrix_alphas_mods();
642 case RGB_MATRIX_DUAL_BEACON:
643 rgb_matrix_dual_beacon();
645 case RGB_MATRIX_GRADIENT_UP_DOWN:
646 rgb_matrix_gradient_up_down();
648 case RGB_MATRIX_RAINDROPS:
649 rgb_matrix_raindrops( initialize );
651 case RGB_MATRIX_CYCLE_ALL:
652 rgb_matrix_cycle_all();
654 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
655 rgb_matrix_cycle_left_right();
657 case RGB_MATRIX_CYCLE_UP_DOWN:
658 rgb_matrix_cycle_up_down();
660 case RGB_MATRIX_RAINBOW_BEACON:
661 rgb_matrix_rainbow_beacon();
663 case RGB_MATRIX_RAINBOW_PINWHEELS:
664 rgb_matrix_rainbow_pinwheels();
666 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
667 rgb_matrix_rainbow_moving_chevron();
669 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
670 rgb_matrix_jellybean_raindrops( initialize );
672 #ifdef RGB_MATRIX_KEYPRESSES
673 case RGB_MATRIX_SOLID_REACTIVE:
674 rgb_matrix_solid_reactive();
676 case RGB_MATRIX_SPLASH:
679 case RGB_MATRIX_MULTISPLASH:
680 rgb_matrix_multisplash();
682 case RGB_MATRIX_SOLID_SPLASH:
683 rgb_matrix_solid_splash();
685 case RGB_MATRIX_SOLID_MULTISPLASH:
686 rgb_matrix_solid_multisplash();
694 if ( ! suspend_backlight ) {
695 rgb_matrix_indicators();
700 void rgb_matrix_indicators(void) {
701 rgb_matrix_indicators_kb();
702 rgb_matrix_indicators_user();
705 __attribute__((weak))
706 void rgb_matrix_indicators_kb(void) {}
708 __attribute__((weak))
709 void rgb_matrix_indicators_user(void) {}
712 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
714 // if ( row >= MATRIX_ROWS )
716 // // Special value, 255=none, 254=all
721 // // This needs updated to something like
722 // // uint8_t led[8], led_count;
723 // // map_row_column_to_led(row,column,led,&led_count);
724 // // for(uint8_t i = 0; i < led_count; i++)
725 // map_row_column_to_led( row, column, index );
729 void rgb_matrix_init_drivers(void) {
732 IS31FL3731_init( DRIVER_ADDR_1 );
733 IS31FL3731_init( DRIVER_ADDR_2 );
735 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
737 // This only caches it for later
738 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
740 // This actually updates the LED drivers
741 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
743 // TODO: put the 1 second startup delay here?
745 // clear the key hits
746 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
747 g_key_hit[led] = 255;
751 if (!eeconfig_is_enabled()) {
752 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
754 eeconfig_update_rgb_matrix_default();
756 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
757 if (!rgb_matrix_config.mode) {
758 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
759 eeconfig_update_rgb_matrix_default();
760 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
762 eeconfig_debug_rgb_matrix(); // display current eeprom values
765 // Deals with the messy details of incrementing an integer
766 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
767 int16_t new_value = value;
769 return MIN( MAX( new_value, min ), max );
772 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
773 int16_t new_value = value;
775 return MIN( MAX( new_value, min ), max );
778 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
780 // // 3 bytes per color
781 // return EECONFIG_RGB_MATRIX + ( led * 3 );
784 // void backlight_get_key_color( uint8_t led, HSV *hsv )
786 // void *address = backlight_get_custom_key_color_eeprom_address( led );
787 // hsv->h = eeprom_read_byte(address);
788 // hsv->s = eeprom_read_byte(address+1);
789 // hsv->v = eeprom_read_byte(address+2);
792 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
794 // uint8_t led[8], led_count;
795 // map_row_column_to_led(row,column,led,&led_count);
796 // for(uint8_t i = 0; i < led_count; i++) {
797 // if ( led[i] < DRIVER_LED_TOTAL )
799 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
800 // eeprom_update_byte(address, hsv.h);
801 // eeprom_update_byte(address+1, hsv.s);
802 // eeprom_update_byte(address+2, hsv.v);
807 void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
808 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
812 IS31FL3731_set_led_control_register( i, red, green, blue );
816 IS31FL3731_set_led_control_register( i, false, false, false );
821 uint32_t rgb_matrix_get_tick(void) {
825 void rgblight_toggle(void) {
826 rgb_matrix_config.enable ^= 1;
827 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
830 void rgblight_step(void) {
831 rgb_matrix_config.mode++;
832 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
833 rgb_matrix_config.mode = 1;
834 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
837 void rgblight_step_reverse(void) {
838 rgb_matrix_config.mode--;
839 if (rgb_matrix_config.mode < 1)
840 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
841 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
844 void rgblight_increase_hue(void) {
845 rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
846 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
849 void rgblight_decrease_hue(void) {
850 rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
851 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
854 void rgblight_increase_sat(void) {
855 rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
856 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
859 void rgblight_decrease_sat(void) {
860 rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
861 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
864 void rgblight_increase_val(void) {
865 rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
866 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
869 void rgblight_decrease_val(void) {
870 rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS );
871 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
874 void rgblight_increase_speed(void) {
875 rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
876 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
879 void rgblight_decrease_speed(void) {
880 rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
881 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
884 void rgblight_mode(uint8_t mode) {
885 rgb_matrix_config.mode = mode;
886 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
889 uint32_t rgblight_get_mode(void) {
890 return rgb_matrix_config.mode;