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"
27 #include "lib/lib8tion/lib8tion.h"
29 #include "rgb_matrix_animations/solid_color_anim.h"
30 #include "rgb_matrix_animations/alpha_mods_anim.h"
31 #include "rgb_matrix_animations/dual_beacon_anim.h"
32 #include "rgb_matrix_animations/gradient_up_down_anim.h"
33 #include "rgb_matrix_animations/raindrops_anim.h"
34 #include "rgb_matrix_animations/cycle_all_anim.h"
35 #include "rgb_matrix_animations/cycle_left_right_anim.h"
36 #include "rgb_matrix_animations/cycle_up_down_anim.h"
37 #include "rgb_matrix_animations/rainbow_beacon_anim.h"
38 #include "rgb_matrix_animations/rainbow_pinwheels_anim.h"
39 #include "rgb_matrix_animations/rainbow_moving_chevron_anim.h"
40 #include "rgb_matrix_animations/jellybean_raindrops_anim.h"
41 #include "rgb_matrix_animations/digital_rain_anim.h"
42 #include "rgb_matrix_animations/solid_reactive_simple_anim.h"
43 #include "rgb_matrix_animations/solid_reactive_anim.h"
44 #include "rgb_matrix_animations/solid_reactive_wide.h"
45 #include "rgb_matrix_animations/solid_reactive_cross.h"
46 #include "rgb_matrix_animations/solid_reactive_nexus.h"
47 #include "rgb_matrix_animations/splash_anim.h"
48 #include "rgb_matrix_animations/solid_splash_anim.h"
49 #include "rgb_matrix_animations/breathing_anim.h"
51 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
52 #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
53 #define RGB_MATRIX_EFFECT(name, ...)
54 #ifdef RGB_MATRIX_CUSTOM_KB
55 #include "rgb_matrix_kb.inc"
57 #ifdef RGB_MATRIX_CUSTOM_USER
58 #include "rgb_matrix_user.inc"
60 #undef RGB_MATRIX_EFFECT
61 #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
64 #ifndef RGB_DISABLE_AFTER_TIMEOUT
65 #define RGB_DISABLE_AFTER_TIMEOUT 0
68 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
69 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
72 #ifndef EECONFIG_RGB_MATRIX
73 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
76 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
77 #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
78 #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
81 #if !defined(RGB_MATRIX_HUE_STEP)
82 #define RGB_MATRIX_HUE_STEP 8
85 #if !defined(RGB_MATRIX_SAT_STEP)
86 #define RGB_MATRIX_SAT_STEP 16
89 #if !defined(RGB_MATRIX_VAL_STEP)
90 #define RGB_MATRIX_VAL_STEP 16
93 #if !defined(RGB_MATRIX_SPD_STEP)
94 #define RGB_MATRIX_SPD_STEP 16
97 #if !defined(RGB_MATRIX_STARTUP_MODE)
98 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
99 #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
101 // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
102 #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
106 bool g_suspend_state = false;
108 extern led_config_t g_led_config;
109 rgb_config_t rgb_matrix_config;
111 rgb_counters_t g_rgb_counters;
112 static uint32_t rgb_counters_buffer;
114 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
115 last_hit_t g_last_hit_tracker;
116 static last_hit_t last_hit_buffer;
117 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
119 uint32_t eeconfig_read_rgb_matrix(void) {
120 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
123 void eeconfig_update_rgb_matrix(uint32_t val) {
124 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
127 void eeconfig_update_rgb_matrix_default(void) {
128 dprintf("eeconfig_update_rgb_matrix_default\n");
129 rgb_matrix_config.enable = 1;
130 rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
131 rgb_matrix_config.hue = 0;
132 rgb_matrix_config.sat = UINT8_MAX;
133 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
134 rgb_matrix_config.speed = UINT8_MAX / 2;
135 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
138 void eeconfig_debug_rgb_matrix(void) {
139 dprintf("rgb_matrix_config eprom\n");
140 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
141 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
142 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
143 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
144 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
145 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
148 __attribute__ ((weak))
149 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
153 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
154 uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
155 uint8_t led_index = g_led_config.matrix_co[row][column];
156 if (led_index != NO_LED) {
157 led_i[led_count] = led_index;
163 void rgb_matrix_update_pwm_buffers(void) {
164 rgb_matrix_driver.flush();
167 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
168 rgb_matrix_driver.set_color(index, red, green, blue);
171 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
172 rgb_matrix_driver.set_color_all(red, green, blue);
175 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
176 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
177 uint8_t led[LED_HITS_TO_REMEMBER];
178 uint8_t led_count = 0;
180 #if defined(RGB_MATRIX_KEYRELEASES)
181 if (!record->event.pressed) {
182 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
183 g_rgb_counters.any_key_hit = 0;
185 #elif defined(RGB_MATRIX_KEYPRESSES)
186 if (record->event.pressed) {
187 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
188 g_rgb_counters.any_key_hit = 0;
190 #endif // defined(RGB_MATRIX_KEYRELEASES)
192 if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
193 memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
194 memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
195 memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
196 memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
197 last_hit_buffer.count--;
200 for(uint8_t i = 0; i < led_count; i++) {
201 uint8_t index = last_hit_buffer.count;
202 last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
203 last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
204 last_hit_buffer.index[index] = led[i];
205 last_hit_buffer.tick[index] = 0;
206 last_hit_buffer.count++;
208 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
212 void rgb_matrix_test(void) {
213 // Mask out bits 4 and 5
214 // Increase the factor to make the test animation slower (and reduce to make it faster)
216 switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
219 rgb_matrix_set_color_all( 20, 0, 0 );
223 rgb_matrix_set_color_all( 0, 20, 0 );
227 rgb_matrix_set_color_all( 0, 0, 20 );
231 rgb_matrix_set_color_all( 20, 20, 20 );
237 static bool rgb_matrix_none(effect_params_t* params) {
242 RGB_MATRIX_USE_LIMITS(led_min, led_max);
243 for (uint8_t i = led_min; i < led_max; i++) {
244 rgb_matrix_set_color(i, 0, 0, 0);
246 return led_max < DRIVER_LED_TOTAL;
249 static uint8_t rgb_last_enable = UINT8_MAX;
250 static uint8_t rgb_last_effect = UINT8_MAX;
251 static effect_params_t rgb_effect_params = { 0, 0xFF };
252 static rgb_task_states rgb_task_state = SYNCING;
254 static void rgb_task_timers(void) {
255 // Update double buffer timers
256 uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
257 rgb_counters_buffer = timer_read32();
258 if (g_rgb_counters.any_key_hit < UINT32_MAX) {
259 if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
260 g_rgb_counters.any_key_hit = UINT32_MAX;
262 g_rgb_counters.any_key_hit += deltaTime;
266 // Update double buffer last hit timers
267 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
268 uint8_t count = last_hit_buffer.count;
269 for (uint8_t i = 0; i < count; ++i) {
270 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
271 last_hit_buffer.count--;
274 last_hit_buffer.tick[i] += deltaTime;
276 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
279 static void rgb_task_sync(void) {
281 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
282 rgb_task_state = STARTING;
285 static void rgb_task_start(void) {
287 rgb_effect_params.iter = 0;
289 // update double buffers
290 g_rgb_counters.tick = rgb_counters_buffer;
291 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
292 g_last_hit_tracker = last_hit_buffer;
293 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
296 rgb_task_state = RENDERING;
299 static void rgb_task_render(uint8_t effect) {
300 bool rendering = false;
301 rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
303 // each effect can opt to do calculations
304 // and/or request PWM buffer updates.
306 case RGB_MATRIX_NONE:
307 rendering = rgb_matrix_none(&rgb_effect_params);
310 case RGB_MATRIX_SOLID_COLOR:
311 rendering = rgb_matrix_solid_color(&rgb_effect_params); // Max 1ms Avg 0ms
313 #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS
314 case RGB_MATRIX_ALPHAS_MODS:
315 rendering = rgb_matrix_alphas_mods(&rgb_effect_params); // Max 2ms Avg 1ms
317 #endif // DISABLE_RGB_MATRIX_ALPHAS_MODS
318 #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
319 case RGB_MATRIX_GRADIENT_UP_DOWN:
320 rendering = rgb_matrix_gradient_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
322 #endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
323 #ifndef DISABLE_RGB_MATRIX_BREATHING
324 case RGB_MATRIX_BREATHING:
325 rendering = rgb_matrix_breathing(&rgb_effect_params); // Max 1ms Avg 0ms
327 #endif // DISABLE_RGB_MATRIX_BREATHING
328 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
329 case RGB_MATRIX_CYCLE_ALL:
330 rendering = rgb_matrix_cycle_all(&rgb_effect_params); // Max 4ms Avg 3ms
332 #endif // DISABLE_RGB_MATRIX_CYCLE_ALL
333 #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
334 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
335 rendering = rgb_matrix_cycle_left_right(&rgb_effect_params); // Max 4ms Avg 3ms
337 #endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
338 #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
339 case RGB_MATRIX_CYCLE_UP_DOWN:
340 rendering = rgb_matrix_cycle_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
342 #endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
343 #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
344 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
345 rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms
347 #endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
348 #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON
349 case RGB_MATRIX_DUAL_BEACON:
350 rendering = rgb_matrix_dual_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
352 #endif // DISABLE_RGB_MATRIX_DUAL_BEACON
353 #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON
354 case RGB_MATRIX_RAINBOW_BEACON:
355 rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
357 #endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON
358 #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
359 case RGB_MATRIX_RAINBOW_PINWHEELS:
360 rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params); // Max 4ms Avg 3ms
362 #endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
363 #ifndef DISABLE_RGB_MATRIX_RAINDROPS
364 case RGB_MATRIX_RAINDROPS:
365 rendering = rgb_matrix_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
367 #endif // DISABLE_RGB_MATRIX_RAINDROPS
368 #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
369 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
370 rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
372 #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
373 #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
374 case RGB_MATRIX_DIGITAL_RAIN:
375 rendering = rgb_matrix_digital_rain(&rgb_effect_params); // Max 9ms Avg 8ms | this is expensive, fix it
377 #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
378 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
379 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE
380 case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
381 rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms
384 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE
385 case RGB_MATRIX_SOLID_REACTIVE:
386 rendering = rgb_matrix_solid_reactive(&rgb_effect_params); // Max 4ms Avg 3ms
388 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE
389 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
390 case RGB_MATRIX_SOLID_REACTIVE_WIDE:
391 rendering = rgb_matrix_solid_reactive_wide(&rgb_effect_params); // Max ?? ms Avg ?? ms
393 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
394 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
395 case RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE:
396 rendering = rgb_matrix_solid_reactive_multiwide(&rgb_effect_params); // Max ?? ms Avg ?? ms
398 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
399 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
400 case RGB_MATRIX_SOLID_REACTIVE_CROSS:
401 rendering = rgb_matrix_solid_reactive_cross(&rgb_effect_params); // Max ?? ms Avg ?? ms
403 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
404 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
405 case RGB_MATRIX_SOLID_REACTIVE_MULTICROSS:
406 rendering = rgb_matrix_solid_reactive_multicross(&rgb_effect_params); // Max ?? ms Avg ?? ms
408 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
409 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
410 case RGB_MATRIX_SOLID_REACTIVE_NEXUS:
411 rendering = rgb_matrix_solid_reactive_nexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
413 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
414 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
415 case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
416 rendering = rgb_matrix_solid_reactive_multinexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
418 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
419 #ifndef DISABLE_RGB_MATRIX_SPLASH
420 case RGB_MATRIX_SPLASH:
421 rendering = rgb_matrix_splash(&rgb_effect_params); // Max 5ms Avg 3ms
423 #endif // DISABLE_RGB_MATRIX_SPLASH
424 #ifndef DISABLE_RGB_MATRIX_MULTISPLASH
425 case RGB_MATRIX_MULTISPLASH:
426 rendering = rgb_matrix_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
428 #endif // DISABLE_RGB_MATRIX_MULTISPLASH
429 #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH
430 case RGB_MATRIX_SOLID_SPLASH:
431 rendering = rgb_matrix_solid_splash(&rgb_effect_params); // Max 5ms Avg 3ms
433 #endif // DISABLE_RGB_MATRIX_SOLID_SPLASH
434 #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
435 case RGB_MATRIX_SOLID_MULTISPLASH:
436 rendering = rgb_matrix_solid_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
438 #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
439 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
441 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
442 #define RGB_MATRIX_EFFECT(name, ...) \
443 case RGB_MATRIX_CUSTOM_##name: \
444 rendering = name(&rgb_effect_params); \
446 #ifdef RGB_MATRIX_CUSTOM_KB
447 #include "rgb_matrix_kb.inc"
449 #ifdef RGB_MATRIX_CUSTOM_USER
450 #include "rgb_matrix_user.inc"
452 #undef RGB_MATRIX_EFFECT
455 // Factory default magic value
458 rgb_task_state = FLUSHING;
463 rgb_effect_params.iter++;
467 rgb_task_state = FLUSHING;
468 if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
469 // We only need to flush once if we are RGB_MATRIX_NONE
470 rgb_task_state = SYNCING;
475 static void rgb_task_flush(uint8_t effect) {
476 // update last trackers after the first full render so we can init over several frames
477 rgb_last_effect = effect;
478 rgb_last_enable = rgb_matrix_config.enable;
480 // update pwm buffers
481 rgb_matrix_update_pwm_buffers();
484 rgb_task_state = SYNCING;
487 void rgb_matrix_task(void) {
490 // Ideally we would also stop sending zeros to the LED driver PWM buffers
491 // while suspended and just do a software shutdown. This is a cheap hack for now.
492 bool suspend_backlight = ((g_suspend_state && RGB_DISABLE_WHEN_USB_SUSPENDED) || (RGB_DISABLE_AFTER_TIMEOUT > 0 && g_rgb_counters.any_key_hit > RGB_DISABLE_AFTER_TIMEOUT * 60 * 20));
493 uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
495 switch (rgb_task_state) {
500 rgb_task_render(effect);
503 rgb_task_flush(effect);
510 if (!suspend_backlight) {
511 rgb_matrix_indicators();
515 void rgb_matrix_indicators(void) {
516 rgb_matrix_indicators_kb();
517 rgb_matrix_indicators_user();
520 __attribute__((weak))
521 void rgb_matrix_indicators_kb(void) {}
523 __attribute__((weak))
524 void rgb_matrix_indicators_user(void) {}
526 void rgb_matrix_init(void) {
527 rgb_matrix_driver.init();
529 // TODO: put the 1 second startup delay here?
531 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
532 g_last_hit_tracker.count = 0;
533 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
534 g_last_hit_tracker.tick[i] = UINT16_MAX;
537 last_hit_buffer.count = 0;
538 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
539 last_hit_buffer.tick[i] = UINT16_MAX;
541 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
543 if (!eeconfig_is_enabled()) {
544 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
546 eeconfig_update_rgb_matrix_default();
549 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
550 rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
551 if (!rgb_matrix_config.mode) {
552 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
553 eeconfig_update_rgb_matrix_default();
554 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
556 eeconfig_debug_rgb_matrix(); // display current eeprom values
559 void rgb_matrix_set_suspend_state(bool state) {
560 g_suspend_state = state;
563 void rgb_matrix_toggle(void) {
564 rgb_matrix_config.enable ^= 1;
565 rgb_task_state = STARTING;
566 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
569 void rgb_matrix_enable(void) {
570 rgb_matrix_enable_noeeprom();
571 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
574 void rgb_matrix_enable_noeeprom(void) {
575 if (!rgb_matrix_config.enable)
576 rgb_task_state = STARTING;
577 rgb_matrix_config.enable = 1;
580 void rgb_matrix_disable(void) {
581 rgb_matrix_disable_noeeprom();
582 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
585 void rgb_matrix_disable_noeeprom(void) {
586 if (rgb_matrix_config.enable)
587 rgb_task_state = STARTING;
588 rgb_matrix_config.enable = 0;
591 void rgb_matrix_step(void) {
592 rgb_matrix_config.mode++;
593 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
594 rgb_matrix_config.mode = 1;
595 rgb_task_state = STARTING;
596 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
599 void rgb_matrix_step_reverse(void) {
600 rgb_matrix_config.mode--;
601 if (rgb_matrix_config.mode < 1)
602 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
603 rgb_task_state = STARTING;
604 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
607 void rgb_matrix_increase_hue(void) {
608 rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
609 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
612 void rgb_matrix_decrease_hue(void) {
613 rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
614 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
617 void rgb_matrix_increase_sat(void) {
618 rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
619 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
622 void rgb_matrix_decrease_sat(void) {
623 rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
624 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
627 void rgb_matrix_increase_val(void) {
628 rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
629 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
630 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
631 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
634 void rgb_matrix_decrease_val(void) {
635 rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
636 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
639 void rgb_matrix_increase_speed(void) {
640 rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
641 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
644 void rgb_matrix_decrease_speed(void) {
645 rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
646 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
649 led_flags_t rgb_matrix_get_flags(void) {
650 return rgb_effect_params.flags;
653 void rgb_matrix_set_flags(led_flags_t flags) {
654 rgb_effect_params.flags = flags;
657 void rgb_matrix_mode(uint8_t mode) {
658 rgb_matrix_config.mode = mode;
659 rgb_task_state = STARTING;
660 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
663 void rgb_matrix_mode_noeeprom(uint8_t mode) {
664 rgb_matrix_config.mode = mode;
667 uint8_t rgb_matrix_get_mode(void) {
668 return rgb_matrix_config.mode;
671 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
672 rgb_matrix_sethsv_noeeprom(hue, sat, val);
673 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
676 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
677 rgb_matrix_config.hue = hue;
678 rgb_matrix_config.sat = sat;
679 rgb_matrix_config.val = val;
680 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
681 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;