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 bool g_suspend_state = false;
46 // Global tick at 20 Hz
49 // Ticks since this key was last hit.
50 uint8_t g_key_hit[DRIVER_LED_TOTAL];
52 // Ticks since any key was last hit.
53 uint32_t g_any_key_hit = 0;
59 uint32_t eeconfig_read_rgb_matrix(void) {
60 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
62 void eeconfig_update_rgb_matrix(uint32_t val) {
63 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
65 void eeconfig_update_rgb_matrix_default(void) {
66 dprintf("eeconfig_update_rgb_matrix_default\n");
67 rgb_matrix_config.enable = 1;
68 rgb_matrix_config.mode = RGB_MATRIX_CYCLE_LEFT_RIGHT;
69 rgb_matrix_config.hue = 0;
70 rgb_matrix_config.sat = 255;
71 rgb_matrix_config.val = 255;
72 rgb_matrix_config.speed = 0;
73 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
75 void eeconfig_debug_rgb_matrix(void) {
76 dprintf("rgb_matrix_config eprom\n");
77 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
78 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
79 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
80 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
81 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
82 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
86 #define LED_HITS_TO_REMEMBER 8
87 uint8_t g_last_led_hit[LED_HITS_TO_REMEMBER] = {255};
88 uint8_t g_last_led_count = 0;
90 void map_row_column_to_led( uint8_t row, uint8_t column, uint8_t *led_i, uint8_t *led_count) {
94 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
95 // map_index_to_led(i, &led);
97 if (row == led.matrix_co.row && column == led.matrix_co.col) {
98 led_i[*led_count] = i;
105 void rgb_matrix_update_pwm_buffers(void) {
106 IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
107 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
110 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
111 IS31FL3731_set_color( index, red, green, blue );
114 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
115 IS31FL3731_set_color_all( red, green, blue );
119 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
120 if ( record->event.pressed ) {
121 uint8_t led[8], led_count;
122 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
124 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
125 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
127 g_last_led_hit[0] = led[0];
128 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
130 for(uint8_t i = 0; i < led_count; i++)
131 g_key_hit[led[i]] = 0;
134 #ifdef RGB_MATRIX_KEYRELEASES
135 uint8_t led[8], led_count;
136 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
137 for(uint8_t i = 0; i < led_count; i++)
138 g_key_hit[led[i]] = 255;
146 void rgb_matrix_set_suspend_state(bool state) {
147 g_suspend_state = state;
150 void rgb_matrix_test(void) {
151 // Mask out bits 4 and 5
152 // Increase the factor to make the test animation slower (and reduce to make it faster)
154 switch ( (g_tick & (0b11 << factor)) >> factor )
158 rgb_matrix_set_color_all( 20, 0, 0 );
163 rgb_matrix_set_color_all( 0, 20, 0 );
168 rgb_matrix_set_color_all( 0, 0, 20 );
173 rgb_matrix_set_color_all( 20, 20, 20 );
179 // This tests the LEDs
180 // Note that it will change the LED control registers
181 // in the LED drivers, and leave them in an invalid
182 // state for other backlight effects.
183 // ONLY USE THIS FOR TESTING LEDS!
184 void rgb_matrix_single_LED_test(void) {
185 static uint8_t color = 0; // 0,1,2 for R,G,B
186 static uint8_t row = 0;
187 static uint8_t column = 0;
189 static uint8_t tick = 0;
197 if ( column > MATRIX_COLS )
202 if ( row > MATRIX_ROWS )
212 uint8_t led[8], led_count;
213 map_row_column_to_led(row,column,led,&led_count);
214 for(uint8_t i = 0; i < led_count; i++) {
215 rgb_matrix_set_color_all( 40, 40, 40 );
216 rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
221 void rgb_matrix_all_off(void) {
222 rgb_matrix_set_color_all( 0, 0, 0 );
226 void rgb_matrix_solid_color(void) {
227 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
228 RGB rgb = hsv_to_rgb( hsv );
229 rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
232 void rgb_matrix_solid_reactive(void) {
233 // Relies on hue being 8-bit and wrapping
234 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
236 uint16_t offset2 = g_key_hit[i]<<2;
237 offset2 = (offset2<=130) ? (130-offset2) : 0;
239 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
240 RGB rgb = hsv_to_rgb( hsv );
241 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
245 // alphas = color1, mods = color2
246 void rgb_matrix_alphas_mods(void) {
248 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
249 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
252 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
254 if ( led.matrix_co.raw < 0xFF ) {
257 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
261 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
267 void rgb_matrix_gradient_up_down(void) {
268 int16_t h1 = rgb_matrix_config.hue;
269 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
270 int16_t deltaH = h2 - h1;
272 // Take the shortest path between hues
277 else if ( deltaH < -127 )
281 // Divide delta by 4, this gives the delta per row
284 int16_t s1 = rgb_matrix_config.sat;
285 int16_t s2 = rgb_matrix_config.hue;
286 int16_t deltaS = ( s2 - s1 ) / 4;
288 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
291 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
293 // map_led_to_point( i, &point );
294 point = g_rgb_leds[i].point;
295 // The y range will be 0..64, map this to 0..4
296 uint8_t y = (point.y>>4);
297 // Relies on hue being 8-bit and wrapping
298 hsv.h = rgb_matrix_config.hue + ( deltaH * y );
299 hsv.s = rgb_matrix_config.sat + ( deltaS * y );
300 rgb = hsv_to_rgb( hsv );
301 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
305 void rgb_matrix_raindrops(bool initialize) {
306 int16_t h1 = rgb_matrix_config.hue;
307 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
308 int16_t deltaH = h2 - h1;
311 // Take the shortest path between hues
316 else if ( deltaH < -127 )
321 int16_t s1 = rgb_matrix_config.sat;
322 int16_t s2 = rgb_matrix_config.sat;
323 int16_t deltaS = ( s2 - s1 ) / 4;
328 // Change one LED every tick, make sure speed is not 0
329 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
331 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
333 // If initialize, all get set to random colors
334 // If not, all but one will stay the same as before.
335 if ( initialize || i == led_to_change )
337 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
338 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
339 // Override brightness with global brightness control
340 hsv.v = rgb_matrix_config.val;
342 rgb = hsv_to_rgb( hsv );
343 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
348 void rgb_matrix_cycle_all(void) {
349 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
353 // Relies on hue being 8-bit and wrapping
354 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
356 // map_index_to_led(i, &led);
358 if (led.matrix_co.raw < 0xFF) {
359 uint16_t offset2 = g_key_hit[i]<<2;
360 offset2 = (offset2<=63) ? (63-offset2) : 0;
362 HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
363 RGB rgb = hsv_to_rgb( hsv );
364 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
369 void rgb_matrix_cycle_left_right(void) {
370 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
371 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
375 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
377 // map_index_to_led(i, &led);
379 if (led.matrix_co.raw < 0xFF) {
380 uint16_t offset2 = g_key_hit[i]<<2;
381 offset2 = (offset2<=63) ? (63-offset2) : 0;
383 // map_led_to_point( i, &point );
384 point = g_rgb_leds[i].point;
385 // Relies on hue being 8-bit and wrapping
386 hsv.h = point.x + offset + offset2;
387 rgb = hsv_to_rgb( hsv );
388 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
393 void rgb_matrix_cycle_up_down(void) {
394 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
395 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
399 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
401 // map_index_to_led(i, &led);
403 if (led.matrix_co.raw < 0xFF) {
404 uint16_t offset2 = g_key_hit[i]<<2;
405 offset2 = (offset2<=63) ? (63-offset2) : 0;
407 // map_led_to_point( i, &point );
408 point = g_rgb_leds[i].point;
409 // Relies on hue being 8-bit and wrapping
410 hsv.h = point.y + offset + offset2;
411 rgb = hsv_to_rgb( hsv );
412 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
418 void rgb_matrix_dual_beacon(void) {
419 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
422 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
424 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;
425 rgb = hsv_to_rgb( hsv );
426 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
430 void rgb_matrix_rainbow_beacon(void) {
431 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
434 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
436 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;
437 rgb = hsv_to_rgb( hsv );
438 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
442 void rgb_matrix_rainbow_pinwheels(void) {
443 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
446 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
448 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;
449 rgb = hsv_to_rgb( hsv );
450 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
454 void rgb_matrix_rainbow_moving_chevron(void) {
455 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
458 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
460 // uint8_t r = g_tick;
462 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;
463 rgb = hsv_to_rgb( hsv );
464 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
469 void rgb_matrix_jellybean_raindrops( bool initialize ) {
473 // Change one LED every tick, make sure speed is not 0
474 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
476 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
478 // If initialize, all get set to random colors
479 // If not, all but one will stay the same as before.
480 if ( initialize || i == led_to_change )
482 hsv.h = rand() & 0xFF;
483 hsv.s = rand() & 0xFF;
484 // Override brightness with global brightness control
485 hsv.v = rgb_matrix_config.val;
487 rgb = hsv_to_rgb( hsv );
488 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
493 void rgb_matrix_multisplash(void) {
494 // if (g_any_key_hit < 0xFF) {
495 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
498 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
500 uint16_t c = 0, d = 0;
502 // if (g_last_led_count) {
503 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
504 last_led = g_rgb_leds[g_last_led_hit[last_i]];
505 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
506 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
507 c += MIN(MAX(effect, 0), 255);
508 d += 255 - MIN(MAX(effect, 0), 255);
513 hsv.h = (rgb_matrix_config.hue + c) % 256;
514 hsv.v = MAX(MIN(d, 255), 0);
515 rgb = hsv_to_rgb( hsv );
516 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
519 // rgb_matrix_set_color_all( 0, 0, 0 );
524 void rgb_matrix_splash(void) {
525 g_last_led_count = MIN(g_last_led_count, 1);
526 rgb_matrix_multisplash();
530 void rgb_matrix_solid_multisplash(void) {
531 // if (g_any_key_hit < 0xFF) {
532 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
535 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
539 // if (g_last_led_count) {
540 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
541 last_led = g_rgb_leds[g_last_led_hit[last_i]];
542 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
543 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
544 d += 255 - MIN(MAX(effect, 0), 255);
549 hsv.v = MAX(MIN(d, 255), 0);
550 rgb = hsv_to_rgb( hsv );
551 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
554 // rgb_matrix_set_color_all( 0, 0, 0 );
559 void rgb_matrix_solid_splash(void) {
560 g_last_led_count = MIN(g_last_led_count, 1);
561 rgb_matrix_solid_multisplash();
565 // Needs eeprom access that we don't have setup currently
567 void rgb_matrix_custom(void) {
570 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
572 // backlight_get_key_color(i, &hsv);
573 // // Override brightness with global brightness control
574 // hsv.v = rgb_matrix_config.val;
575 // rgb = hsv_to_rgb( hsv );
576 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
580 void rgb_matrix_task(void) {
581 static uint8_t toggle_enable_last = 255;
582 if (!rgb_matrix_config.enable) {
583 rgb_matrix_all_off();
584 toggle_enable_last = rgb_matrix_config.enable;
587 // delay 1 second before driving LEDs or doing anything else
588 static uint8_t startup_tick = 0;
589 if ( startup_tick < 20 ) {
596 if ( g_any_key_hit < 0xFFFFFFFF ) {
600 for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
601 if ( g_key_hit[led] < 255 ) {
602 if (g_key_hit[led] == 254)
603 g_last_led_count = MAX(g_last_led_count - 1, 0);
608 // Factory default magic value
609 if ( rgb_matrix_config.mode == 255 ) {
614 // Ideally we would also stop sending zeros to the LED driver PWM buffers
615 // while suspended and just do a software shutdown. This is a cheap hack for now.
616 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
617 (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
618 uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
620 // Keep track of the effect used last time,
621 // detect change in effect, so each effect can
622 // have an optional initialization.
623 static uint8_t effect_last = 255;
624 bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
625 effect_last = effect;
626 toggle_enable_last = rgb_matrix_config.enable;
628 // this gets ticked at 20 Hz.
629 // each effect can opt to do calculations
630 // and/or request PWM buffer updates.
632 case RGB_MATRIX_SOLID_COLOR:
633 rgb_matrix_solid_color();
635 case RGB_MATRIX_ALPHAS_MODS:
636 rgb_matrix_alphas_mods();
638 case RGB_MATRIX_DUAL_BEACON:
639 rgb_matrix_dual_beacon();
641 case RGB_MATRIX_GRADIENT_UP_DOWN:
642 rgb_matrix_gradient_up_down();
644 case RGB_MATRIX_RAINDROPS:
645 rgb_matrix_raindrops( initialize );
647 case RGB_MATRIX_CYCLE_ALL:
648 rgb_matrix_cycle_all();
650 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
651 rgb_matrix_cycle_left_right();
653 case RGB_MATRIX_CYCLE_UP_DOWN:
654 rgb_matrix_cycle_up_down();
656 case RGB_MATRIX_RAINBOW_BEACON:
657 rgb_matrix_rainbow_beacon();
659 case RGB_MATRIX_RAINBOW_PINWHEELS:
660 rgb_matrix_rainbow_pinwheels();
662 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
663 rgb_matrix_rainbow_moving_chevron();
665 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
666 rgb_matrix_jellybean_raindrops( initialize );
668 #ifdef RGB_MATRIX_KEYPRESSES
669 case RGB_MATRIX_SOLID_REACTIVE:
670 rgb_matrix_solid_reactive();
672 case RGB_MATRIX_SPLASH:
675 case RGB_MATRIX_MULTISPLASH:
676 rgb_matrix_multisplash();
678 case RGB_MATRIX_SOLID_SPLASH:
679 rgb_matrix_solid_splash();
681 case RGB_MATRIX_SOLID_MULTISPLASH:
682 rgb_matrix_solid_multisplash();
690 if ( ! suspend_backlight ) {
691 rgb_matrix_indicators();
696 void rgb_matrix_indicators(void) {
697 rgb_matrix_indicators_kb();
698 rgb_matrix_indicators_user();
701 __attribute__((weak))
702 void rgb_matrix_indicators_kb(void) {}
704 __attribute__((weak))
705 void rgb_matrix_indicators_user(void) {}
708 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
710 // if ( row >= MATRIX_ROWS )
712 // // Special value, 255=none, 254=all
717 // // This needs updated to something like
718 // // uint8_t led[8], led_count;
719 // // map_row_column_to_led(row,column,led,&led_count);
720 // // for(uint8_t i = 0; i < led_count; i++)
721 // map_row_column_to_led( row, column, index );
725 void rgb_matrix_init_drivers(void) {
728 IS31FL3731_init( DRIVER_ADDR_1 );
729 IS31FL3731_init( DRIVER_ADDR_2 );
731 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
733 // This only caches it for later
734 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
736 // This actually updates the LED drivers
737 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
739 // TODO: put the 1 second startup delay here?
741 // clear the key hits
742 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
743 g_key_hit[led] = 255;
747 if (!eeconfig_is_enabled()) {
748 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
750 eeconfig_update_rgb_matrix_default();
752 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
753 if (!rgb_matrix_config.mode) {
754 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
755 eeconfig_update_rgb_matrix_default();
756 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
758 eeconfig_debug_rgb_matrix(); // display current eeprom values
761 // Deals with the messy details of incrementing an integer
762 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
763 int16_t new_value = value;
765 return MIN( MAX( new_value, min ), max );
768 uint8_t decrement( 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 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
776 // // 3 bytes per color
777 // return EECONFIG_RGB_MATRIX + ( led * 3 );
780 // void backlight_get_key_color( uint8_t led, HSV *hsv )
782 // void *address = backlight_get_custom_key_color_eeprom_address( led );
783 // hsv->h = eeprom_read_byte(address);
784 // hsv->s = eeprom_read_byte(address+1);
785 // hsv->v = eeprom_read_byte(address+2);
788 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
790 // uint8_t led[8], led_count;
791 // map_row_column_to_led(row,column,led,&led_count);
792 // for(uint8_t i = 0; i < led_count; i++) {
793 // if ( led[i] < DRIVER_LED_TOTAL )
795 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
796 // eeprom_update_byte(address, hsv.h);
797 // eeprom_update_byte(address+1, hsv.s);
798 // eeprom_update_byte(address+2, hsv.v);
803 void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
804 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
808 IS31FL3731_set_led_control_register( i, red, green, blue );
812 IS31FL3731_set_led_control_register( i, false, false, false );
817 uint32_t rgb_matrix_get_tick(void) {
821 void rgblight_toggle(void) {
822 rgb_matrix_config.enable ^= 1;
823 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
826 void rgblight_step(void) {
827 rgb_matrix_config.mode++;
828 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
829 rgb_matrix_config.mode = 1;
830 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
833 void rgblight_step_reverse(void) {
834 rgb_matrix_config.mode--;
835 if (rgb_matrix_config.mode < 1)
836 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
837 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
840 void rgblight_increase_hue(void) {
841 rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
842 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
845 void rgblight_decrease_hue(void) {
846 rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
847 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
850 void rgblight_increase_sat(void) {
851 rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
852 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
855 void rgblight_decrease_sat(void) {
856 rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
857 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
860 void rgblight_increase_val(void) {
861 rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 );
862 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
865 void rgblight_decrease_val(void) {
866 rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 );
867 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
870 void rgblight_increase_speed(void) {
871 rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
872 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
875 void rgblight_decrease_speed(void) {
876 rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
877 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
880 void rgblight_mode(uint8_t mode) {
881 rgb_matrix_config.mode = mode;
882 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
885 uint32_t rgblight_get_mode(void) {
886 return rgb_matrix_config.mode;