1 /* Copyright 2017 Jason Williams
2 * Copyright 2017 Jack Humbert
3 * Copyright 2018 Yiancar
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "rgb_matrix.h"
26 rgb_config_t rgb_matrix_config
;
29 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
33 #define MIN(a,b) ((a) < (b)? (a): (b))
36 #ifndef RGB_DISABLE_AFTER_TIMEOUT
37 #define RGB_DISABLE_AFTER_TIMEOUT 0
40 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
41 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
44 #ifndef EECONFIG_RGB_MATRIX
45 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
48 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > 255
49 #define RGB_MATRIX_MAXIMUM_BRIGHTNESS 255
52 bool g_suspend_state
= false;
54 // Global tick at 20 Hz
57 // Ticks since this key was last hit.
58 uint8_t g_key_hit
[DRIVER_LED_TOTAL
];
60 // Ticks since any key was last hit.
61 uint32_t g_any_key_hit
= 0;
67 uint32_t eeconfig_read_rgb_matrix(void) {
68 return eeprom_read_dword(EECONFIG_RGB_MATRIX
);
70 void eeconfig_update_rgb_matrix(uint32_t val
) {
71 eeprom_update_dword(EECONFIG_RGB_MATRIX
, val
);
73 void eeconfig_update_rgb_matrix_default(void) {
74 dprintf("eeconfig_update_rgb_matrix_default\n");
75 rgb_matrix_config
.enable
= 1;
76 rgb_matrix_config
.mode
= RGB_MATRIX_CYCLE_LEFT_RIGHT
;
77 rgb_matrix_config
.hue
= 0;
78 rgb_matrix_config
.sat
= 255;
79 rgb_matrix_config
.val
= RGB_MATRIX_MAXIMUM_BRIGHTNESS
;
80 rgb_matrix_config
.speed
= 0;
81 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
83 void eeconfig_debug_rgb_matrix(void) {
84 dprintf("rgb_matrix_config eprom\n");
85 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config
.enable
);
86 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config
.mode
);
87 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config
.hue
);
88 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config
.sat
);
89 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config
.val
);
90 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config
.speed
);
94 #define LED_HITS_TO_REMEMBER 8
95 uint8_t g_last_led_hit
[LED_HITS_TO_REMEMBER
] = {255};
96 uint8_t g_last_led_count
= 0;
98 void map_row_column_to_led( uint8_t row
, uint8_t column
, uint8_t *led_i
, uint8_t *led_count
) {
102 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
103 // map_index_to_led(i, &led);
105 if (row
== led
.matrix_co
.row
&& column
== led
.matrix_co
.col
) {
106 led_i
[*led_count
] = i
;
112 void rgb_matrix_update_pwm_buffers(void) {
113 rgb_matrix_driver
.flush();
116 void rgb_matrix_set_color( int index
, uint8_t red
, uint8_t green
, uint8_t blue
) {
117 rgb_matrix_driver
.set_color(index
, red
, green
, blue
);
120 void rgb_matrix_set_color_all( uint8_t red
, uint8_t green
, uint8_t blue
) {
121 rgb_matrix_driver
.set_color_all(red
, green
, blue
);
124 bool process_rgb_matrix(uint16_t keycode
, keyrecord_t
*record
) {
125 if ( record
->event
.pressed
) {
126 uint8_t led
[8], led_count
;
127 map_row_column_to_led(record
->event
.key
.row
, record
->event
.key
.col
, led
, &led_count
);
129 for (uint8_t i
= LED_HITS_TO_REMEMBER
; i
> 1; i
--) {
130 g_last_led_hit
[i
- 1] = g_last_led_hit
[i
- 2];
132 g_last_led_hit
[0] = led
[0];
133 g_last_led_count
= MIN(LED_HITS_TO_REMEMBER
, g_last_led_count
+ 1);
135 for(uint8_t i
= 0; i
< led_count
; i
++)
136 g_key_hit
[led
[i
]] = 0;
139 #ifdef RGB_MATRIX_KEYRELEASES
140 uint8_t led
[8], led_count
;
141 map_row_column_to_led(record
->event
.key
.row
, record
->event
.key
.col
, led
, &led_count
);
142 for(uint8_t i
= 0; i
< led_count
; i
++)
143 g_key_hit
[led
[i
]] = 255;
151 void rgb_matrix_set_suspend_state(bool state
) {
152 g_suspend_state
= state
;
155 void rgb_matrix_test(void) {
156 // Mask out bits 4 and 5
157 // Increase the factor to make the test animation slower (and reduce to make it faster)
159 switch ( (g_tick
& (0b11 << factor
)) >> factor
)
163 rgb_matrix_set_color_all( 20, 0, 0 );
168 rgb_matrix_set_color_all( 0, 20, 0 );
173 rgb_matrix_set_color_all( 0, 0, 20 );
178 rgb_matrix_set_color_all( 20, 20, 20 );
185 void rgb_matrix_all_off(void) {
186 rgb_matrix_set_color_all( 0, 0, 0 );
190 void rgb_matrix_solid_color(void) {
191 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
192 RGB rgb
= hsv_to_rgb( hsv
);
193 rgb_matrix_set_color_all( rgb
.r
, rgb
.g
, rgb
.b
);
196 void rgb_matrix_solid_reactive(void) {
197 // Relies on hue being 8-bit and wrapping
198 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
200 uint16_t offset2
= g_key_hit
[i
]<<2;
201 offset2
= (offset2
<=130) ? (130-offset2
) : 0;
203 HSV hsv
= { .h
= rgb_matrix_config
.hue
+offset2
, .s
= 255, .v
= rgb_matrix_config
.val
};
204 RGB rgb
= hsv_to_rgb( hsv
);
205 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
209 // alphas = color1, mods = color2
210 void rgb_matrix_alphas_mods(void) {
212 RGB rgb1
= hsv_to_rgb( (HSV
){ .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
} );
213 RGB rgb2
= hsv_to_rgb( (HSV
){ .h
= (rgb_matrix_config
.hue
+ 180) % 360, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
} );
216 for (int i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
218 if ( led
.matrix_co
.raw
< 0xFF ) {
221 rgb_matrix_set_color( i
, rgb2
.r
, rgb2
.g
, rgb2
.b
);
225 rgb_matrix_set_color( i
, rgb1
.r
, rgb1
.g
, rgb1
.b
);
231 void rgb_matrix_gradient_up_down(void) {
232 int16_t h1
= rgb_matrix_config
.hue
;
233 int16_t h2
= (rgb_matrix_config
.hue
+ 180) % 360;
234 int16_t deltaH
= h2
- h1
;
236 // Take the shortest path between hues
241 else if ( deltaH
< -127 )
245 // Divide delta by 4, this gives the delta per row
248 int16_t s1
= rgb_matrix_config
.sat
;
249 int16_t s2
= rgb_matrix_config
.hue
;
250 int16_t deltaS
= ( s2
- s1
) / 4;
252 HSV hsv
= { .h
= 0, .s
= 255, .v
= rgb_matrix_config
.val
};
255 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
257 // map_led_to_point( i, &point );
258 point
= g_rgb_leds
[i
].point
;
259 // The y range will be 0..64, map this to 0..4
260 uint8_t y
= (point
.y
>>4);
261 // Relies on hue being 8-bit and wrapping
262 hsv
.h
= rgb_matrix_config
.hue
+ ( deltaH
* y
);
263 hsv
.s
= rgb_matrix_config
.sat
+ ( deltaS
* y
);
264 rgb
= hsv_to_rgb( hsv
);
265 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
269 void rgb_matrix_raindrops(bool initialize
) {
270 int16_t h1
= rgb_matrix_config
.hue
;
271 int16_t h2
= (rgb_matrix_config
.hue
+ 180) % 360;
272 int16_t deltaH
= h2
- h1
;
275 // Take the shortest path between hues
280 else if ( deltaH
< -127 )
285 int16_t s1
= rgb_matrix_config
.sat
;
286 int16_t s2
= rgb_matrix_config
.sat
;
287 int16_t deltaS
= ( s2
- s1
) / 4;
292 // Change one LED every tick, make sure speed is not 0
293 uint8_t led_to_change
= ( g_tick
& ( 0x0A / (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL
) : 255;
295 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
297 // If initialize, all get set to random colors
298 // If not, all but one will stay the same as before.
299 if ( initialize
|| i
== led_to_change
)
301 hsv
.h
= h1
+ ( deltaH
* ( rand() & 0x03 ) );
302 hsv
.s
= s1
+ ( deltaS
* ( rand() & 0x03 ) );
303 // Override brightness with global brightness control
304 hsv
.v
= rgb_matrix_config
.val
;
306 rgb
= hsv_to_rgb( hsv
);
307 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
312 void rgb_matrix_cycle_all(void) {
313 uint8_t offset
= ( g_tick
<< rgb_matrix_config
.speed
) & 0xFF;
317 // Relies on hue being 8-bit and wrapping
318 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
320 // map_index_to_led(i, &led);
322 if (led
.matrix_co
.raw
< 0xFF) {
323 uint16_t offset2
= g_key_hit
[i
]<<2;
324 offset2
= (offset2
<=63) ? (63-offset2
) : 0;
326 HSV hsv
= { .h
= offset
+offset2
, .s
= 255, .v
= rgb_matrix_config
.val
};
327 RGB rgb
= hsv_to_rgb( hsv
);
328 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
333 void rgb_matrix_cycle_left_right(void) {
334 uint8_t offset
= ( g_tick
<< rgb_matrix_config
.speed
) & 0xFF;
335 HSV hsv
= { .h
= 0, .s
= 255, .v
= rgb_matrix_config
.val
};
339 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
341 // map_index_to_led(i, &led);
343 if (led
.matrix_co
.raw
< 0xFF) {
344 uint16_t offset2
= g_key_hit
[i
]<<2;
345 offset2
= (offset2
<=63) ? (63-offset2
) : 0;
347 // map_led_to_point( i, &point );
348 point
= g_rgb_leds
[i
].point
;
349 // Relies on hue being 8-bit and wrapping
350 hsv
.h
= point
.x
+ offset
+ offset2
;
351 rgb
= hsv_to_rgb( hsv
);
352 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
357 void rgb_matrix_cycle_up_down(void) {
358 uint8_t offset
= ( g_tick
<< rgb_matrix_config
.speed
) & 0xFF;
359 HSV hsv
= { .h
= 0, .s
= 255, .v
= rgb_matrix_config
.val
};
363 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
365 // map_index_to_led(i, &led);
367 if (led
.matrix_co
.raw
< 0xFF) {
368 uint16_t offset2
= g_key_hit
[i
]<<2;
369 offset2
= (offset2
<=63) ? (63-offset2
) : 0;
371 // map_led_to_point( i, &point );
372 point
= g_rgb_leds
[i
].point
;
373 // Relies on hue being 8-bit and wrapping
374 hsv
.h
= point
.y
+ offset
+ offset2
;
375 rgb
= hsv_to_rgb( hsv
);
376 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
382 void rgb_matrix_dual_beacon(void) {
383 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
386 double cos_value
= cos(g_tick
* PI
/ 128) / 32;
387 double sin_value
= sin(g_tick
* PI
/ 128) / 112;
388 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
389 point
= g_rgb_leds
[i
].point
;
390 hsv
.h
= ((point
.y
- 32.0)* cos_value
+ (point
.x
- 112.0) * sin_value
) * (180) + rgb_matrix_config
.hue
;
391 rgb
= hsv_to_rgb( hsv
);
392 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
396 void rgb_matrix_rainbow_beacon(void) {
397 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
400 double cos_value
= cos(g_tick
* PI
/ 128);
401 double sin_value
= sin(g_tick
* PI
/ 128);
402 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
403 point
= g_rgb_leds
[i
].point
;
404 hsv
.h
= (1.5 * (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
)) * (point
.y
- 32.0)* cos_value
+ (1.5 * (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
)) * (point
.x
- 112.0) * sin_value
+ rgb_matrix_config
.hue
;
405 rgb
= hsv_to_rgb( hsv
);
406 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
410 void rgb_matrix_rainbow_pinwheels(void) {
411 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
414 double cos_value
= cos(g_tick
* PI
/ 128);
415 double sin_value
= sin(g_tick
* PI
/ 128);
416 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
417 point
= g_rgb_leds
[i
].point
;
418 hsv
.h
= (2 * (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
)) * (point
.y
- 32.0)* cos_value
+ (2 * (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
)) * (66 - abs(point
.x
- 112.0)) * sin_value
+ rgb_matrix_config
.hue
;
419 rgb
= hsv_to_rgb( hsv
);
420 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
424 void rgb_matrix_rainbow_moving_chevron(void) {
425 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
429 double cos_value
= cos(r
* PI
/ 128);
430 double sin_value
= sin(r
* PI
/ 128);
431 double multiplier
= (g_tick
/ 256.0 * 224);
432 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
433 point
= g_rgb_leds
[i
].point
;
434 hsv
.h
= (1.5 * (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
)) * abs(point
.y
- 32.0)* sin_value
+ (1.5 * (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
)) * (point
.x
- multiplier
) * cos_value
+ rgb_matrix_config
.hue
;
435 rgb
= hsv_to_rgb( hsv
);
436 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
441 void rgb_matrix_jellybean_raindrops( bool initialize
) {
445 // Change one LED every tick, make sure speed is not 0
446 uint8_t led_to_change
= ( g_tick
& ( 0x0A / (rgb_matrix_config
.speed
== 0 ? 1 : rgb_matrix_config
.speed
) ) ) == 0 ? rand() % (DRIVER_LED_TOTAL
) : 255;
448 for ( int i
=0; i
<DRIVER_LED_TOTAL
; i
++ )
450 // If initialize, all get set to random colors
451 // If not, all but one will stay the same as before.
452 if ( initialize
|| i
== led_to_change
)
454 hsv
.h
= rand() & 0xFF;
455 hsv
.s
= rand() & 0xFF;
456 // Override brightness with global brightness control
457 hsv
.v
= rgb_matrix_config
.val
;
459 rgb
= hsv_to_rgb( hsv
);
460 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
465 void rgb_matrix_digital_rain( const bool initialize
) {
466 // algorithm ported from https://github.com/tremby/Kaleidoscope-LEDEffect-DigitalRain
467 const uint8_t drop_ticks
= 28;
468 const uint8_t new_drop_probability
= 24;
469 const uint8_t pure_green_intensity
= 0xd0;
470 const uint8_t max_brightness_boost
= 0xc0;
471 const uint8_t max_intensity
= 0xff;
473 static uint8_t map
[MATRIX_COLS
][MATRIX_ROWS
] = {{0}};
474 static uint8_t drop
= 0;
477 rgb_matrix_set_color_all(0, 0, 0);
478 memset(map
, 0, sizeof map
);
481 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
482 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
483 if (row
== 0 && drop
== 0 && rand() < RAND_MAX
/ new_drop_probability
) {
484 // top row, pixels have just fallen and we're
485 // making a new rain drop in this column
486 map
[col
][row
] = max_intensity
;
488 else if (map
[col
][row
] > 0 && map
[col
][row
] < max_intensity
) {
489 // neither fully bright nor dark, decay it
492 // set the pixel colour
493 uint8_t led
, led_count
;
494 map_row_column_to_led(row
, col
, &led
, &led_count
);
496 if (map
[col
][row
] > pure_green_intensity
) {
497 const uint8_t boost
= (uint8_t) ((uint16_t) max_brightness_boost
498 * (map
[col
][row
] - pure_green_intensity
) / (max_intensity
- pure_green_intensity
));
499 rgb_matrix_set_color(led
, boost
, max_intensity
, boost
);
502 const uint8_t green
= (uint8_t) ((uint16_t) max_intensity
* map
[col
][row
] / pure_green_intensity
);
503 rgb_matrix_set_color(led
, 0, green
, 0);
507 if (++drop
> drop_ticks
) {
510 for (uint8_t row
= MATRIX_ROWS
- 1; row
> 0; row
--) {
511 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
512 // if ths is on the bottom row and bright allow decay
513 if (row
== MATRIX_ROWS
- 1 && map
[col
][row
] == max_intensity
) {
516 // check if the pixel above is bright
517 if (map
[col
][row
- 1] == max_intensity
) {
518 // allow old bright pixel to decay
520 // make this pixel bright
521 map
[col
][row
] = max_intensity
;
528 void rgb_matrix_multisplash(void) {
529 // if (g_any_key_hit < 0xFF) {
530 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
533 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
535 uint16_t c
= 0, d
= 0;
537 // if (g_last_led_count) {
538 for (uint8_t last_i
= 0; last_i
< g_last_led_count
; last_i
++) {
539 last_led
= g_rgb_leds
[g_last_led_hit
[last_i
]];
540 uint16_t dist
= (uint16_t)sqrt(pow(led
.point
.x
- last_led
.point
.x
, 2) + pow(led
.point
.y
- last_led
.point
.y
, 2));
541 uint16_t effect
= (g_key_hit
[g_last_led_hit
[last_i
]] << 2) - dist
;
542 c
+= MIN(MAX(effect
, 0), 255);
543 d
+= 255 - MIN(MAX(effect
, 0), 255);
548 hsv
.h
= (rgb_matrix_config
.hue
+ c
) % 256;
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_splash(void) {
560 g_last_led_count
= MIN(g_last_led_count
, 1);
561 rgb_matrix_multisplash();
565 void rgb_matrix_solid_multisplash(void) {
566 // if (g_any_key_hit < 0xFF) {
567 HSV hsv
= { .h
= rgb_matrix_config
.hue
, .s
= rgb_matrix_config
.sat
, .v
= rgb_matrix_config
.val
};
570 for (uint8_t i
= 0; i
< DRIVER_LED_TOTAL
; i
++) {
574 // if (g_last_led_count) {
575 for (uint8_t last_i
= 0; last_i
< g_last_led_count
; last_i
++) {
576 last_led
= g_rgb_leds
[g_last_led_hit
[last_i
]];
577 uint16_t dist
= (uint16_t)sqrt(pow(led
.point
.x
- last_led
.point
.x
, 2) + pow(led
.point
.y
- last_led
.point
.y
, 2));
578 uint16_t effect
= (g_key_hit
[g_last_led_hit
[last_i
]] << 2) - dist
;
579 d
+= 255 - MIN(MAX(effect
, 0), 255);
584 hsv
.v
= MAX(MIN(d
, 255), 0);
585 rgb
= hsv_to_rgb( hsv
);
586 rgb_matrix_set_color( i
, rgb
.r
, rgb
.g
, rgb
.b
);
589 // rgb_matrix_set_color_all( 0, 0, 0 );
594 void rgb_matrix_solid_splash(void) {
595 g_last_led_count
= MIN(g_last_led_count
, 1);
596 rgb_matrix_solid_multisplash();
600 // Needs eeprom access that we don't have setup currently
602 void rgb_matrix_custom(void) {
605 // for ( int i=0; i<DRIVER_LED_TOTAL; i++ )
607 // backlight_get_key_color(i, &hsv);
608 // // Override brightness with global brightness control
609 // hsv.v = rgb_matrix_config.val;
610 // rgb = hsv_to_rgb( hsv );
611 // rgb_matrix_set_color( i, rgb.r, rgb.g, rgb.b );
615 void rgb_matrix_task(void) {
616 static uint8_t toggle_enable_last
= 255;
617 if (!rgb_matrix_config
.enable
) {
618 rgb_matrix_all_off();
619 toggle_enable_last
= rgb_matrix_config
.enable
;
622 // delay 1 second before driving LEDs or doing anything else
623 static uint8_t startup_tick
= 0;
624 if ( startup_tick
< 20 ) {
631 if ( g_any_key_hit
< 0xFFFFFFFF ) {
635 for ( int led
= 0; led
< DRIVER_LED_TOTAL
; led
++ ) {
636 if ( g_key_hit
[led
] < 255 ) {
637 if (g_key_hit
[led
] == 254)
638 g_last_led_count
= MAX(g_last_led_count
- 1, 0);
643 // Factory default magic value
644 if ( rgb_matrix_config
.mode
== 255 ) {
649 // Ideally we would also stop sending zeros to the LED driver PWM buffers
650 // while suspended and just do a software shutdown. This is a cheap hack for now.
651 bool suspend_backlight
= ((g_suspend_state
&& RGB_DISABLE_WHEN_USB_SUSPENDED
) ||
652 (RGB_DISABLE_AFTER_TIMEOUT
> 0 && g_any_key_hit
> RGB_DISABLE_AFTER_TIMEOUT
* 60 * 20));
653 uint8_t effect
= suspend_backlight
? 0 : rgb_matrix_config
.mode
;
655 // Keep track of the effect used last time,
656 // detect change in effect, so each effect can
657 // have an optional initialization.
658 static uint8_t effect_last
= 255;
659 bool initialize
= (effect
!= effect_last
) || (rgb_matrix_config
.enable
!= toggle_enable_last
);
660 effect_last
= effect
;
661 toggle_enable_last
= rgb_matrix_config
.enable
;
663 // this gets ticked at 20 Hz.
664 // each effect can opt to do calculations
665 // and/or request PWM buffer updates.
667 case RGB_MATRIX_SOLID_COLOR
:
668 rgb_matrix_solid_color();
670 case RGB_MATRIX_ALPHAS_MODS
:
671 rgb_matrix_alphas_mods();
673 case RGB_MATRIX_DUAL_BEACON
:
674 rgb_matrix_dual_beacon();
676 case RGB_MATRIX_GRADIENT_UP_DOWN
:
677 rgb_matrix_gradient_up_down();
679 case RGB_MATRIX_RAINDROPS
:
680 rgb_matrix_raindrops( initialize
);
682 case RGB_MATRIX_CYCLE_ALL
:
683 rgb_matrix_cycle_all();
685 case RGB_MATRIX_CYCLE_LEFT_RIGHT
:
686 rgb_matrix_cycle_left_right();
688 case RGB_MATRIX_CYCLE_UP_DOWN
:
689 rgb_matrix_cycle_up_down();
691 case RGB_MATRIX_RAINBOW_BEACON
:
692 rgb_matrix_rainbow_beacon();
694 case RGB_MATRIX_RAINBOW_PINWHEELS
:
695 rgb_matrix_rainbow_pinwheels();
697 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON
:
698 rgb_matrix_rainbow_moving_chevron();
700 case RGB_MATRIX_JELLYBEAN_RAINDROPS
:
701 rgb_matrix_jellybean_raindrops( initialize
);
703 case RGB_MATRIX_DIGITAL_RAIN
:
704 rgb_matrix_digital_rain( initialize
);
706 #ifdef RGB_MATRIX_KEYPRESSES
707 case RGB_MATRIX_SOLID_REACTIVE
:
708 rgb_matrix_solid_reactive();
710 case RGB_MATRIX_SPLASH
:
713 case RGB_MATRIX_MULTISPLASH
:
714 rgb_matrix_multisplash();
716 case RGB_MATRIX_SOLID_SPLASH
:
717 rgb_matrix_solid_splash();
719 case RGB_MATRIX_SOLID_MULTISPLASH
:
720 rgb_matrix_solid_multisplash();
728 if ( ! suspend_backlight
) {
729 rgb_matrix_indicators();
734 void rgb_matrix_indicators(void) {
735 rgb_matrix_indicators_kb();
736 rgb_matrix_indicators_user();
739 __attribute__((weak
))
740 void rgb_matrix_indicators_kb(void) {}
742 __attribute__((weak
))
743 void rgb_matrix_indicators_user(void) {}
746 // void rgb_matrix_set_indicator_index( uint8_t *index, uint8_t row, uint8_t column )
748 // if ( row >= MATRIX_ROWS )
750 // // Special value, 255=none, 254=all
755 // // This needs updated to something like
756 // // uint8_t led[8], led_count;
757 // // map_row_column_to_led(row,column,led,&led_count);
758 // // for(uint8_t i = 0; i < led_count; i++)
759 // map_row_column_to_led( row, column, index );
763 void rgb_matrix_init(void) {
764 rgb_matrix_driver
.init();
766 // TODO: put the 1 second startup delay here?
768 // clear the key hits
769 for ( int led
=0; led
<DRIVER_LED_TOTAL
; led
++ ) {
770 g_key_hit
[led
] = 255;
774 if (!eeconfig_is_enabled()) {
775 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
777 eeconfig_update_rgb_matrix_default();
779 rgb_matrix_config
.raw
= eeconfig_read_rgb_matrix();
780 if (!rgb_matrix_config
.mode
) {
781 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
782 eeconfig_update_rgb_matrix_default();
783 rgb_matrix_config
.raw
= eeconfig_read_rgb_matrix();
785 eeconfig_debug_rgb_matrix(); // display current eeprom values
788 // Deals with the messy details of incrementing an integer
789 uint8_t increment( uint8_t value
, uint8_t step
, uint8_t min
, uint8_t max
) {
790 int16_t new_value
= value
;
792 return MIN( MAX( new_value
, min
), max
);
795 uint8_t decrement( uint8_t value
, uint8_t step
, uint8_t min
, uint8_t max
) {
796 int16_t new_value
= value
;
798 return MIN( MAX( new_value
, min
), max
);
801 // void *backlight_get_custom_key_color_eeprom_address( uint8_t led )
803 // // 3 bytes per color
804 // return EECONFIG_RGB_MATRIX + ( led * 3 );
807 // void backlight_get_key_color( uint8_t led, HSV *hsv )
809 // void *address = backlight_get_custom_key_color_eeprom_address( led );
810 // hsv->h = eeprom_read_byte(address);
811 // hsv->s = eeprom_read_byte(address+1);
812 // hsv->v = eeprom_read_byte(address+2);
815 // void backlight_set_key_color( uint8_t row, uint8_t column, HSV hsv )
817 // uint8_t led[8], led_count;
818 // map_row_column_to_led(row,column,led,&led_count);
819 // for(uint8_t i = 0; i < led_count; i++) {
820 // if ( led[i] < DRIVER_LED_TOTAL )
822 // void *address = backlight_get_custom_key_color_eeprom_address(led[i]);
823 // eeprom_update_byte(address, hsv.h);
824 // eeprom_update_byte(address+1, hsv.s);
825 // eeprom_update_byte(address+2, hsv.v);
830 uint32_t rgb_matrix_get_tick(void) {
834 void rgblight_toggle(void) {
835 rgb_matrix_config
.enable
^= 1;
836 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
839 void rgblight_step(void) {
840 rgb_matrix_config
.mode
++;
841 if (rgb_matrix_config
.mode
>= RGB_MATRIX_EFFECT_MAX
)
842 rgb_matrix_config
.mode
= 1;
843 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
846 void rgblight_step_reverse(void) {
847 rgb_matrix_config
.mode
--;
848 if (rgb_matrix_config
.mode
< 1)
849 rgb_matrix_config
.mode
= RGB_MATRIX_EFFECT_MAX
- 1;
850 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
853 void rgblight_increase_hue(void) {
854 rgb_matrix_config
.hue
= increment( rgb_matrix_config
.hue
, 8, 0, 255 );
855 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
858 void rgblight_decrease_hue(void) {
859 rgb_matrix_config
.hue
= decrement( rgb_matrix_config
.hue
, 8, 0, 255 );
860 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
863 void rgblight_increase_sat(void) {
864 rgb_matrix_config
.sat
= increment( rgb_matrix_config
.sat
, 8, 0, 255 );
865 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
868 void rgblight_decrease_sat(void) {
869 rgb_matrix_config
.sat
= decrement( rgb_matrix_config
.sat
, 8, 0, 255 );
870 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
873 void rgblight_increase_val(void) {
874 rgb_matrix_config
.val
= increment( rgb_matrix_config
.val
, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS
);
875 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
878 void rgblight_decrease_val(void) {
879 rgb_matrix_config
.val
= decrement( rgb_matrix_config
.val
, 8, 0, RGB_MATRIX_MAXIMUM_BRIGHTNESS
);
880 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
883 void rgblight_increase_speed(void) {
884 rgb_matrix_config
.speed
= increment( rgb_matrix_config
.speed
, 1, 0, 3 );
885 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);//EECONFIG needs to be increased to support this
888 void rgblight_decrease_speed(void) {
889 rgb_matrix_config
.speed
= decrement( rgb_matrix_config
.speed
, 1, 0, 3 );
890 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);//EECONFIG needs to be increased to support this
893 void rgblight_mode(uint8_t mode
) {
894 rgb_matrix_config
.mode
= mode
;
895 eeconfig_update_rgb_matrix(rgb_matrix_config
.raw
);
898 uint32_t rgblight_get_mode(void) {
899 return rgb_matrix_config
.mode
;