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;
104 void rgb_matrix_update_pwm_buffers(void) {
105 uint8_t ret = IS31FL3731_update_pwm_buffers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
106 ret |= IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
110 rgb_matrix_setup_drivers();
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 );
122 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
123 if ( record->event.pressed ) {
124 uint8_t led[8], led_count;
125 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
127 for (uint8_t i = LED_HITS_TO_REMEMBER; i > 1; i--) {
128 g_last_led_hit[i - 1] = g_last_led_hit[i - 2];
130 g_last_led_hit[0] = led[0];
131 g_last_led_count = MIN(LED_HITS_TO_REMEMBER, g_last_led_count + 1);
133 for(uint8_t i = 0; i < led_count; i++)
134 g_key_hit[led[i]] = 0;
137 #ifdef RGB_MATRIX_KEYRELEASES
138 uint8_t led[8], led_count;
139 map_row_column_to_led(record->event.key.row, record->event.key.col, led, &led_count);
140 for(uint8_t i = 0; i < led_count; i++)
141 g_key_hit[led[i]] = 255;
149 void rgb_matrix_set_suspend_state(bool state) {
150 g_suspend_state = state;
153 void rgb_matrix_test(void) {
154 // Mask out bits 4 and 5
155 // Increase the factor to make the test animation slower (and reduce to make it faster)
157 switch ( (g_tick & (0b11 << factor)) >> factor )
161 rgb_matrix_set_color_all( 20, 0, 0 );
166 rgb_matrix_set_color_all( 0, 20, 0 );
171 rgb_matrix_set_color_all( 0, 0, 20 );
176 rgb_matrix_set_color_all( 20, 20, 20 );
182 // This tests the LEDs
183 // Note that it will change the LED control registers
184 // in the LED drivers, and leave them in an invalid
185 // state for other backlight effects.
186 // ONLY USE THIS FOR TESTING LEDS!
187 void rgb_matrix_single_LED_test(void) {
188 static uint8_t color = 0; // 0,1,2 for R,G,B
189 static uint8_t row = 0;
190 static uint8_t column = 0;
192 static uint8_t tick = 0;
200 if ( column > MATRIX_COLS )
205 if ( row > MATRIX_ROWS )
215 uint8_t led[8], led_count;
216 map_row_column_to_led(row,column,led,&led_count);
217 for(uint8_t i = 0; i < led_count; i++) {
218 rgb_matrix_set_color_all( 40, 40, 40 );
219 rgb_matrix_test_led( led[i], color==0, color==1, color==2 );
224 void rgb_matrix_all_off(void) {
225 rgb_matrix_set_color_all( 0, 0, 0 );
229 void rgb_matrix_solid_color(void) {
230 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
231 RGB rgb = hsv_to_rgb( hsv );
232 rgb_matrix_set_color_all( rgb.r, rgb.g, rgb.b );
235 void rgb_matrix_solid_reactive(void) {
236 // Relies on hue being 8-bit and wrapping
237 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
239 uint16_t offset2 = g_key_hit[i]<<2;
240 offset2 = (offset2<=130) ? (130-offset2) : 0;
242 HSV hsv = { .h = rgb_matrix_config.hue+offset2, .s = 255, .v = rgb_matrix_config.val };
243 RGB rgb = hsv_to_rgb( hsv );
244 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
248 // alphas = color1, mods = color2
249 void rgb_matrix_alphas_mods(void) {
251 RGB rgb1 = hsv_to_rgb( (HSV){ .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
252 RGB rgb2 = hsv_to_rgb( (HSV){ .h = (rgb_matrix_config.hue + 180) % 360, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val } );
255 for (int i = 0; i < DRIVER_LED_TOTAL; i++) {
257 if ( led.matrix_co.raw < 0xFF ) {
260 rgb_matrix_set_color( i, rgb2.r, rgb2.g, rgb2.b );
264 rgb_matrix_set_color( i, rgb1.r, rgb1.g, rgb1.b );
270 void rgb_matrix_gradient_up_down(void) {
271 int16_t h1 = rgb_matrix_config.hue;
272 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
273 int16_t deltaH = h2 - h1;
275 // Take the shortest path between hues
280 else if ( deltaH < -127 )
284 // Divide delta by 4, this gives the delta per row
287 int16_t s1 = rgb_matrix_config.sat;
288 int16_t s2 = rgb_matrix_config.hue;
289 int16_t deltaS = ( s2 - s1 ) / 4;
291 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
294 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
296 // map_led_to_point( i, &point );
297 point = g_rgb_leds[i].point;
298 // The y range will be 0..64, map this to 0..4
299 uint8_t y = (point.y>>4);
300 // Relies on hue being 8-bit and wrapping
301 hsv.h = rgb_matrix_config.hue + ( deltaH * y );
302 hsv.s = rgb_matrix_config.sat + ( deltaS * y );
303 rgb = hsv_to_rgb( hsv );
304 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
308 void rgb_matrix_raindrops(bool initialize) {
309 int16_t h1 = rgb_matrix_config.hue;
310 int16_t h2 = (rgb_matrix_config.hue + 180) % 360;
311 int16_t deltaH = h2 - h1;
314 // Take the shortest path between hues
319 else if ( deltaH < -127 )
324 int16_t s1 = rgb_matrix_config.sat;
325 int16_t s2 = rgb_matrix_config.sat;
326 int16_t deltaS = ( s2 - s1 ) / 4;
331 // Change one LED every tick, make sure speed is not 0
332 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
334 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
336 // If initialize, all get set to random colors
337 // If not, all but one will stay the same as before.
338 if ( initialize || i == led_to_change )
340 hsv.h = h1 + ( deltaH * ( rand() & 0x03 ) );
341 hsv.s = s1 + ( deltaS * ( rand() & 0x03 ) );
342 // Override brightness with global brightness control
343 hsv.v = rgb_matrix_config.val;
345 rgb = hsv_to_rgb( hsv );
346 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
351 void rgb_matrix_cycle_all(void) {
352 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
356 // Relies on hue being 8-bit and wrapping
357 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
359 // map_index_to_led(i, &led);
361 if (led.matrix_co.raw < 0xFF) {
362 uint16_t offset2 = g_key_hit[i]<<2;
363 offset2 = (offset2<=63) ? (63-offset2) : 0;
365 HSV hsv = { .h = offset+offset2, .s = 255, .v = rgb_matrix_config.val };
366 RGB rgb = hsv_to_rgb( hsv );
367 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
372 void rgb_matrix_cycle_left_right(void) {
373 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
374 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
378 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
380 // map_index_to_led(i, &led);
382 if (led.matrix_co.raw < 0xFF) {
383 uint16_t offset2 = g_key_hit[i]<<2;
384 offset2 = (offset2<=63) ? (63-offset2) : 0;
386 // map_led_to_point( i, &point );
387 point = g_rgb_leds[i].point;
388 // Relies on hue being 8-bit and wrapping
389 hsv.h = point.x + offset + offset2;
390 rgb = hsv_to_rgb( hsv );
391 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
396 void rgb_matrix_cycle_up_down(void) {
397 uint8_t offset = ( g_tick << rgb_matrix_config.speed ) & 0xFF;
398 HSV hsv = { .h = 0, .s = 255, .v = rgb_matrix_config.val };
402 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
404 // map_index_to_led(i, &led);
406 if (led.matrix_co.raw < 0xFF) {
407 uint16_t offset2 = g_key_hit[i]<<2;
408 offset2 = (offset2<=63) ? (63-offset2) : 0;
410 // map_led_to_point( i, &point );
411 point = g_rgb_leds[i].point;
412 // Relies on hue being 8-bit and wrapping
413 hsv.h = point.y + offset + offset2;
414 rgb = hsv_to_rgb( hsv );
415 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
421 void rgb_matrix_dual_beacon(void) {
422 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
425 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
427 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;
428 rgb = hsv_to_rgb( hsv );
429 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
433 void rgb_matrix_rainbow_beacon(void) {
434 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
437 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
439 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;
440 rgb = hsv_to_rgb( hsv );
441 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
445 void rgb_matrix_rainbow_pinwheels(void) {
446 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
449 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
451 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;
452 rgb = hsv_to_rgb( hsv );
453 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
457 void rgb_matrix_rainbow_moving_chevron(void) {
458 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
461 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
463 // uint8_t r = g_tick;
465 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;
466 rgb = hsv_to_rgb( hsv );
467 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
472 void rgb_matrix_jellybean_raindrops( bool initialize ) {
476 // Change one LED every tick, make sure speed is not 0
477 uint8_t led_to_change = ( g_tick & ( 0x0A / (rgb_matrix_config.speed == 0 ? 1 : rgb_matrix_config.speed) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL) : 255;
479 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
481 // If initialize, all get set to random colors
482 // If not, all but one will stay the same as before.
483 if ( initialize || i == led_to_change )
485 hsv.h = rand() & 0xFF;
486 hsv.s = rand() & 0xFF;
487 // Override brightness with global brightness control
488 hsv.v = rgb_matrix_config.val;
490 rgb = hsv_to_rgb( hsv );
491 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
496 void rgb_matrix_multisplash(void) {
497 // if (g_any_key_hit < 0xFF) {
498 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
501 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
503 uint16_t c = 0, d = 0;
505 // if (g_last_led_count) {
506 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
507 last_led = g_rgb_leds[g_last_led_hit[last_i]];
508 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
509 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
510 c += MIN(MAX(effect, 0), 255);
511 d += 255 - MIN(MAX(effect, 0), 255);
516 hsv.h = (rgb_matrix_config.hue + c) % 256;
517 hsv.v = MAX(MIN(d, 255), 0);
518 rgb = hsv_to_rgb( hsv );
519 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
522 // rgb_matrix_set_color_all( 0, 0, 0 );
527 void rgb_matrix_splash(void) {
528 g_last_led_count = MIN(g_last_led_count, 1);
529 rgb_matrix_multisplash();
533 void rgb_matrix_solid_multisplash(void) {
534 // if (g_any_key_hit < 0xFF) {
535 HSV hsv = { .h = rgb_matrix_config.hue, .s = rgb_matrix_config.sat, .v = rgb_matrix_config.val };
538 for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
542 // if (g_last_led_count) {
543 for (uint8_t last_i = 0; last_i < g_last_led_count; last_i++) {
544 last_led = g_rgb_leds[g_last_led_hit[last_i]];
545 uint16_t dist = (uint16_t)sqrt(pow(led.point.x - last_led.point.x, 2) + pow(led.point.y - last_led.point.y, 2));
546 uint16_t effect = (g_key_hit[g_last_led_hit[last_i]] << 2) - dist;
547 d += 255 - MIN(MAX(effect, 0), 255);
552 hsv.v = MAX(MIN(d, 255), 0);
553 rgb = hsv_to_rgb( hsv );
554 rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
557 // rgb_matrix_set_color_all( 0, 0, 0 );
562 void rgb_matrix_solid_splash(void) {
563 g_last_led_count = MIN(g_last_led_count, 1);
564 rgb_matrix_solid_multisplash();
568 // Needs eeprom access that we don't have setup currently
570 void rgb_matrix_custom(void) {
573 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
575 // backlight_get_key_color(i, &hsv);
576 // // Override brightness with global brightness control
577 // hsv.v = rgb_matrix_config.val;
578 // rgb = hsv_to_rgb( hsv );
579 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
583 void rgb_matrix_task(void) {
584 static uint8_t toggle_enable_last = 255;
585 if (!rgb_matrix_config.enable) {
586 rgb_matrix_all_off();
587 toggle_enable_last = rgb_matrix_config.enable;
590 // delay 1 second before driving LEDs or doing anything else
591 static uint8_t startup_tick = 0;
592 if ( startup_tick < 20 ) {
599 if ( g_any_key_hit < 0xFFFFFFFF ) {
603 for ( int led = 0; led < DRIVER_LED_TOTAL; led++ ) {
604 if ( g_key_hit[led] < 255 ) {
605 if (g_key_hit[led] == 254)
606 g_last_led_count = MAX(g_last_led_count - 1, 0);
611 // Factory default magic value
612 if ( rgb_matrix_config.mode == 255 ) {
617 // Ideally we would also stop sending zeros to the LED driver PWM buffers
618 // while suspended and just do a software shutdown. This is a cheap hack for now.
619 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) ||
620 (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
621 uint8_t effect = suspend_backlight ? 0 : rgb_matrix_config.mode;
623 // Keep track of the effect used last time,
624 // detect change in effect, so each effect can
625 // have an optional initialization.
626 static uint8_t effect_last = 255;
627 bool initialize = (effect != effect_last) || (rgb_matrix_config.enable != toggle_enable_last);
628 effect_last = effect;
629 toggle_enable_last = rgb_matrix_config.enable;
631 // this gets ticked at 20 Hz.
632 // each effect can opt to do calculations
633 // and/or request PWM buffer updates.
635 case RGB_MATRIX_SOLID_COLOR:
636 rgb_matrix_solid_color();
638 case RGB_MATRIX_ALPHAS_MODS:
639 rgb_matrix_alphas_mods();
641 case RGB_MATRIX_DUAL_BEACON:
642 rgb_matrix_dual_beacon();
644 case RGB_MATRIX_GRADIENT_UP_DOWN:
645 rgb_matrix_gradient_up_down();
647 case RGB_MATRIX_RAINDROPS:
648 rgb_matrix_raindrops( initialize );
650 case RGB_MATRIX_CYCLE_ALL:
651 rgb_matrix_cycle_all();
653 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
654 rgb_matrix_cycle_left_right();
656 case RGB_MATRIX_CYCLE_UP_DOWN:
657 rgb_matrix_cycle_up_down();
659 case RGB_MATRIX_RAINBOW_BEACON:
660 rgb_matrix_rainbow_beacon();
662 case RGB_MATRIX_RAINBOW_PINWHEELS:
663 rgb_matrix_rainbow_pinwheels();
665 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
666 rgb_matrix_rainbow_moving_chevron();
668 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
669 rgb_matrix_jellybean_raindrops( initialize );
671 #ifdef RGB_MATRIX_KEYPRESSES
672 case RGB_MATRIX_SOLID_REACTIVE:
673 rgb_matrix_solid_reactive();
675 case RGB_MATRIX_SPLASH:
678 case RGB_MATRIX_MULTISPLASH:
679 rgb_matrix_multisplash();
681 case RGB_MATRIX_SOLID_SPLASH:
682 rgb_matrix_solid_splash();
684 case RGB_MATRIX_SOLID_MULTISPLASH:
685 rgb_matrix_solid_multisplash();
693 if ( ! suspend_backlight ) {
694 rgb_matrix_indicators();
699 void rgb_matrix_indicators(void) {
700 rgb_matrix_indicators_kb();
701 rgb_matrix_indicators_user();
704 __attribute__((weak))
705 void rgb_matrix_indicators_kb(void) {}
707 __attribute__((weak))
708 void rgb_matrix_indicators_user(void) {}
711 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
713 // if ( row >= MATRIX_ROWS )
715 // // Special value, 255=none, 254=all
720 // // This needs updated to something like
721 // // uint8_t led[8], led_count;
722 // // map_row_column_to_led(row,column,led,&led_count);
723 // // for(uint8_t i = 0; i < led_count; i++)
724 // map_row_column_to_led( row, column, index );
728 void rgb_matrix_init(void) {
729 rgb_matrix_setup_drivers();
731 // TODO: put the 1 second startup delay here?
733 // clear the key hits
734 for ( int led=0; led<DRIVER_LED_TOTAL; led++ ) {
735 g_key_hit[led] = 255;
739 if (!eeconfig_is_enabled()) {
740 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
742 eeconfig_update_rgb_matrix_default();
744 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
745 if (!rgb_matrix_config.mode) {
746 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
747 eeconfig_update_rgb_matrix_default();
748 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
750 eeconfig_debug_rgb_matrix(); // display current eeprom values
753 void rgb_matrix_setup_drivers(void) {
756 IS31FL3731_init( DRIVER_ADDR_1 );
757 IS31FL3731_init( DRIVER_ADDR_2 );
759 for ( int index = 0; index < DRIVER_LED_TOTAL; index++ ) {
761 // This only caches it for later
762 IS31FL3731_set_led_control_register( index, enabled, enabled, enabled );
764 // This actually updates the LED drivers
765 IS31FL3731_update_led_control_registers( DRIVER_ADDR_1, DRIVER_ADDR_2 );
768 // Deals with the messy details of incrementing an integer
769 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
770 int16_t new_value = value;
772 return MIN( MAX( new_value, min ), max );
775 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
776 int16_t new_value = value;
778 return MIN( MAX( new_value, min ), max );
781 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
783 // // 3 bytes per color
784 // return EECONFIG_RGB_MATRIX + ( led * 3 );
787 // void backlight_get_key_color( uint8_t led, HSV *hsv )
789 // void *address = backlight_get_custom_key_color_eeprom_address( led );
790 // hsv->h = eeprom_read_byte(address);
791 // hsv->s = eeprom_read_byte(address+1);
792 // hsv->v = eeprom_read_byte(address+2);
795 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
797 // uint8_t led[8], led_count;
798 // map_row_column_to_led(row,column,led,&led_count);
799 // for(uint8_t i = 0; i < led_count; i++) {
800 // if ( led[i] < DRIVER_LED_TOTAL )
802 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
803 // eeprom_update_byte(address, hsv.h);
804 // eeprom_update_byte(address+1, hsv.s);
805 // eeprom_update_byte(address+2, hsv.v);
810 void rgb_matrix_test_led( uint8_t index, bool red, bool green, bool blue ) {
811 for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
815 IS31FL3731_set_led_control_register( i, red, green, blue );
819 IS31FL3731_set_led_control_register( i, false, false, false );
824 uint32_t rgb_matrix_get_tick(void) {
828 void rgblight_toggle(void) {
829 rgb_matrix_config.enable ^= 1;
830 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
833 void rgblight_step(void) {
834 rgb_matrix_config.mode++;
835 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
836 rgb_matrix_config.mode = 1;
837 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
840 void rgblight_step_reverse(void) {
841 rgb_matrix_config.mode--;
842 if (rgb_matrix_config.mode < 1)
843 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
844 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
847 void rgblight_increase_hue(void) {
848 rgb_matrix_config.hue = increment( rgb_matrix_config.hue, 8, 0, 255 );
849 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
852 void rgblight_decrease_hue(void) {
853 rgb_matrix_config.hue = decrement( rgb_matrix_config.hue, 8, 0, 255 );
854 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
857 void rgblight_increase_sat(void) {
858 rgb_matrix_config.sat = increment( rgb_matrix_config.sat, 8, 0, 255 );
859 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
862 void rgblight_decrease_sat(void) {
863 rgb_matrix_config.sat = decrement( rgb_matrix_config.sat, 8, 0, 255 );
864 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
867 void rgblight_increase_val(void) {
868 rgb_matrix_config.val = increment( rgb_matrix_config.val, 8, 0, 255 );
869 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
872 void rgblight_decrease_val(void) {
873 rgb_matrix_config.val = decrement( rgb_matrix_config.val, 8, 0, 255 );
874 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
877 void rgblight_increase_speed(void) {
878 rgb_matrix_config.speed = increment( rgb_matrix_config.speed, 1, 0, 3 );
879 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
882 void rgblight_decrease_speed(void) {
883 rgb_matrix_config.speed = decrement( rgb_matrix_config.speed, 1, 0, 3 );
884 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
887 void rgblight_mode(uint8_t mode) {
888 rgb_matrix_config.mode = mode;
889 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
892 uint32_t rgblight_get_mode(void) {
893 return rgb_matrix_config.mode;