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 rgb_config_t rgb_matrix_config;
110 rgb_counters_t g_rgb_counters;
111 static uint32_t rgb_counters_buffer;
113 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
114 last_hit_t g_last_hit_tracker;
115 static last_hit_t last_hit_buffer;
116 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
118 uint32_t eeconfig_read_rgb_matrix(void) {
119 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
122 void eeconfig_update_rgb_matrix(uint32_t val) {
123 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
126 void eeconfig_update_rgb_matrix_default(void) {
127 dprintf("eeconfig_update_rgb_matrix_default\n");
128 rgb_matrix_config.enable = 1;
129 rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
130 rgb_matrix_config.hue = 0;
131 rgb_matrix_config.sat = UINT8_MAX;
132 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
133 rgb_matrix_config.speed = UINT8_MAX / 2;
134 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
137 void eeconfig_debug_rgb_matrix(void) {
138 dprintf("rgb_matrix_config eprom\n");
139 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
140 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
141 dprintf("rgb_matrix_config.hue = %d\n", rgb_matrix_config.hue);
142 dprintf("rgb_matrix_config.sat = %d\n", rgb_matrix_config.sat);
143 dprintf("rgb_matrix_config.val = %d\n", rgb_matrix_config.val);
144 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
147 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
148 // TODO: This is kinda expensive, fix this soonish
149 uint8_t led_count = 0;
150 for (uint8_t i = 0; i < DRIVER_LED_TOTAL && led_count < LED_HITS_TO_REMEMBER; i++) {
151 matrix_co_t matrix_co = g_rgb_leds[i].matrix_co;
152 if (row == matrix_co.row && column == matrix_co.col) {
153 led_i[led_count] = i;
160 void rgb_matrix_update_pwm_buffers(void) {
161 rgb_matrix_driver.flush();
164 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
165 rgb_matrix_driver.set_color(index, red, green, blue);
168 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
169 rgb_matrix_driver.set_color_all(red, green, blue);
172 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
173 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
174 uint8_t led[LED_HITS_TO_REMEMBER];
175 uint8_t led_count = 0;
177 #if defined(RGB_MATRIX_KEYRELEASES)
178 if (!record->event.pressed) {
179 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
180 g_rgb_counters.any_key_hit = 0;
182 #elif defined(RGB_MATRIX_KEYPRESSES)
183 if (record->event.pressed) {
184 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
185 g_rgb_counters.any_key_hit = 0;
187 #endif // defined(RGB_MATRIX_KEYRELEASES)
189 if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
190 memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
191 memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
192 memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
193 memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
194 last_hit_buffer.count--;
197 for(uint8_t i = 0; i < led_count; i++) {
198 uint8_t index = last_hit_buffer.count;
199 last_hit_buffer.x[index] = g_rgb_leds[led[i]].point.x;
200 last_hit_buffer.y[index] = g_rgb_leds[led[i]].point.y;
201 last_hit_buffer.index[index] = led[i];
202 last_hit_buffer.tick[index] = 0;
203 last_hit_buffer.count++;
205 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
209 void rgb_matrix_test(void) {
210 // Mask out bits 4 and 5
211 // Increase the factor to make the test animation slower (and reduce to make it faster)
213 switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
216 rgb_matrix_set_color_all( 20, 0, 0 );
220 rgb_matrix_set_color_all( 0, 20, 0 );
224 rgb_matrix_set_color_all( 0, 0, 20 );
228 rgb_matrix_set_color_all( 20, 20, 20 );
234 static bool rgb_matrix_none(effect_params_t* params) {
239 RGB_MATRIX_USE_LIMITS(led_min, led_max);
240 for (uint8_t i = led_min; i < led_max; i++) {
241 rgb_matrix_set_color(i, 0, 0, 0);
243 return led_max < DRIVER_LED_TOTAL;
246 static uint8_t rgb_last_enable = UINT8_MAX;
247 static uint8_t rgb_last_effect = UINT8_MAX;
248 static effect_params_t rgb_effect_params = { 0, 0xFF };
249 static rgb_task_states rgb_task_state = SYNCING;
251 static void rgb_task_timers(void) {
252 // Update double buffer timers
253 uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
254 rgb_counters_buffer = timer_read32();
255 if (g_rgb_counters.any_key_hit < UINT32_MAX) {
256 if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
257 g_rgb_counters.any_key_hit = UINT32_MAX;
259 g_rgb_counters.any_key_hit += deltaTime;
263 // Update double buffer last hit timers
264 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
265 uint8_t count = last_hit_buffer.count;
266 for (uint8_t i = 0; i < count; ++i) {
267 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
268 last_hit_buffer.count--;
271 last_hit_buffer.tick[i] += deltaTime;
273 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
276 static void rgb_task_sync(void) {
278 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
279 rgb_task_state = STARTING;
282 static void rgb_task_start(void) {
284 rgb_effect_params.iter = 0;
286 // update double buffers
287 g_rgb_counters.tick = rgb_counters_buffer;
288 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
289 g_last_hit_tracker = last_hit_buffer;
290 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
293 rgb_task_state = RENDERING;
296 static void rgb_task_render(uint8_t effect) {
297 bool rendering = false;
298 rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
300 // each effect can opt to do calculations
301 // and/or request PWM buffer updates.
303 case RGB_MATRIX_NONE:
304 rendering = rgb_matrix_none(&rgb_effect_params);
307 case RGB_MATRIX_SOLID_COLOR:
308 rendering = rgb_matrix_solid_color(&rgb_effect_params); // Max 1ms Avg 0ms
310 #ifndef DISABLE_RGB_MATRIX_ALPHAS_MODS
311 case RGB_MATRIX_ALPHAS_MODS:
312 rendering = rgb_matrix_alphas_mods(&rgb_effect_params); // Max 2ms Avg 1ms
314 #endif // DISABLE_RGB_MATRIX_ALPHAS_MODS
315 #ifndef DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
316 case RGB_MATRIX_GRADIENT_UP_DOWN:
317 rendering = rgb_matrix_gradient_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
319 #endif // DISABLE_RGB_MATRIX_GRADIENT_UP_DOWN
320 #ifndef DISABLE_RGB_MATRIX_BREATHING
321 case RGB_MATRIX_BREATHING:
322 rendering = rgb_matrix_breathing(&rgb_effect_params); // Max 1ms Avg 0ms
324 #endif // DISABLE_RGB_MATRIX_BREATHING
325 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
326 case RGB_MATRIX_CYCLE_ALL:
327 rendering = rgb_matrix_cycle_all(&rgb_effect_params); // Max 4ms Avg 3ms
329 #endif // DISABLE_RGB_MATRIX_CYCLE_ALL
330 #ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
331 case RGB_MATRIX_CYCLE_LEFT_RIGHT:
332 rendering = rgb_matrix_cycle_left_right(&rgb_effect_params); // Max 4ms Avg 3ms
334 #endif // DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
335 #ifndef DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
336 case RGB_MATRIX_CYCLE_UP_DOWN:
337 rendering = rgb_matrix_cycle_up_down(&rgb_effect_params); // Max 4ms Avg 3ms
339 #endif // DISABLE_RGB_MATRIX_CYCLE_UP_DOWN
340 #ifndef DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
341 case RGB_MATRIX_RAINBOW_MOVING_CHEVRON:
342 rendering = rgb_matrix_rainbow_moving_chevron(&rgb_effect_params); // Max 4ms Avg 3ms
344 #endif // DISABLE_RGB_MATRIX_RAINBOW_MOVING_CHEVRON
345 #ifndef DISABLE_RGB_MATRIX_DUAL_BEACON
346 case RGB_MATRIX_DUAL_BEACON:
347 rendering = rgb_matrix_dual_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
349 #endif // DISABLE_RGB_MATRIX_DUAL_BEACON
350 #ifndef DISABLE_RGB_MATRIX_RAINBOW_BEACON
351 case RGB_MATRIX_RAINBOW_BEACON:
352 rendering = rgb_matrix_rainbow_beacon(&rgb_effect_params); // Max 4ms Avg 3ms
354 #endif // DISABLE_RGB_MATRIX_RAINBOW_BEACON
355 #ifndef DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
356 case RGB_MATRIX_RAINBOW_PINWHEELS:
357 rendering = rgb_matrix_rainbow_pinwheels(&rgb_effect_params); // Max 4ms Avg 3ms
359 #endif // DISABLE_RGB_MATRIX_RAINBOW_PINWHEELS
360 #ifndef DISABLE_RGB_MATRIX_RAINDROPS
361 case RGB_MATRIX_RAINDROPS:
362 rendering = rgb_matrix_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
364 #endif // DISABLE_RGB_MATRIX_RAINDROPS
365 #ifndef DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
366 case RGB_MATRIX_JELLYBEAN_RAINDROPS:
367 rendering = rgb_matrix_jellybean_raindrops(&rgb_effect_params); // Max 1ms Avg 0ms
369 #endif // DISABLE_RGB_MATRIX_JELLYBEAN_RAINDROPS
370 #ifndef DISABLE_RGB_MATRIX_DIGITAL_RAIN
371 case RGB_MATRIX_DIGITAL_RAIN:
372 rendering = rgb_matrix_digital_rain(&rgb_effect_params); // Max 9ms Avg 8ms | this is expensive, fix it
374 #endif // DISABLE_RGB_MATRIX_DIGITAL_RAIN
375 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
376 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_SIMPLE
377 case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
378 rendering = rgb_matrix_solid_reactive_simple(&rgb_effect_params);// Max 4ms Avg 3ms
381 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE
382 case RGB_MATRIX_SOLID_REACTIVE:
383 rendering = rgb_matrix_solid_reactive(&rgb_effect_params); // Max 4ms Avg 3ms
385 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE
386 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
387 case RGB_MATRIX_SOLID_REACTIVE_WIDE:
388 rendering = rgb_matrix_solid_reactive_wide(&rgb_effect_params); // Max ?? ms Avg ?? ms
390 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_WIDE
391 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
392 case RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE:
393 rendering = rgb_matrix_solid_reactive_multiwide(&rgb_effect_params); // Max ?? ms Avg ?? ms
395 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTIWIDE
396 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
397 case RGB_MATRIX_SOLID_REACTIVE_CROSS:
398 rendering = rgb_matrix_solid_reactive_cross(&rgb_effect_params); // Max ?? ms Avg ?? ms
400 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_CROSS
401 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
402 case RGB_MATRIX_SOLID_REACTIVE_MULTICROSS:
403 rendering = rgb_matrix_solid_reactive_multicross(&rgb_effect_params); // Max ?? ms Avg ?? ms
405 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTICROSS
406 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
407 case RGB_MATRIX_SOLID_REACTIVE_NEXUS:
408 rendering = rgb_matrix_solid_reactive_nexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
410 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_NEXUS
411 #ifndef DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
412 case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
413 rendering = rgb_matrix_solid_reactive_multinexus(&rgb_effect_params); // Max ?? ms Avg ?? ms
415 #endif // DISABLE_RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS
416 #ifndef DISABLE_RGB_MATRIX_SPLASH
417 case RGB_MATRIX_SPLASH:
418 rendering = rgb_matrix_splash(&rgb_effect_params); // Max 5ms Avg 3ms
420 #endif // DISABLE_RGB_MATRIX_SPLASH
421 #ifndef DISABLE_RGB_MATRIX_MULTISPLASH
422 case RGB_MATRIX_MULTISPLASH:
423 rendering = rgb_matrix_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
425 #endif // DISABLE_RGB_MATRIX_MULTISPLASH
426 #ifndef DISABLE_RGB_MATRIX_SOLID_SPLASH
427 case RGB_MATRIX_SOLID_SPLASH:
428 rendering = rgb_matrix_solid_splash(&rgb_effect_params); // Max 5ms Avg 3ms
430 #endif // DISABLE_RGB_MATRIX_SOLID_SPLASH
431 #ifndef DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
432 case RGB_MATRIX_SOLID_MULTISPLASH:
433 rendering = rgb_matrix_solid_multisplash(&rgb_effect_params); // Max 10ms Avg 5ms
435 #endif // DISABLE_RGB_MATRIX_SOLID_MULTISPLASH
436 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
438 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
439 #define RGB_MATRIX_EFFECT(name, ...) \
440 case RGB_MATRIX_CUSTOM_##name: \
441 rendering = name(&rgb_effect_params); \
443 #ifdef RGB_MATRIX_CUSTOM_KB
444 #include "rgb_matrix_kb.inc"
446 #ifdef RGB_MATRIX_CUSTOM_USER
447 #include "rgb_matrix_user.inc"
449 #undef RGB_MATRIX_EFFECT
452 // Factory default magic value
455 rgb_task_state = FLUSHING;
460 rgb_effect_params.iter++;
464 rgb_task_state = FLUSHING;
465 if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
466 // We only need to flush once if we are RGB_MATRIX_NONE
467 rgb_task_state = SYNCING;
472 static void rgb_task_flush(uint8_t effect) {
473 // update last trackers after the first full render so we can init over several frames
474 rgb_last_effect = effect;
475 rgb_last_enable = rgb_matrix_config.enable;
477 // update pwm buffers
478 rgb_matrix_update_pwm_buffers();
481 rgb_task_state = SYNCING;
484 void rgb_matrix_task(void) {
487 // Ideally we would also stop sending zeros to the LED driver PWM buffers
488 // while suspended and just do a software shutdown. This is a cheap hack for now.
489 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));
490 uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
492 switch (rgb_task_state) {
497 rgb_task_render(effect);
500 rgb_task_flush(effect);
507 if (!suspend_backlight) {
508 rgb_matrix_indicators();
512 void rgb_matrix_indicators(void) {
513 rgb_matrix_indicators_kb();
514 rgb_matrix_indicators_user();
517 __attribute__((weak))
518 void rgb_matrix_indicators_kb(void) {}
520 __attribute__((weak))
521 void rgb_matrix_indicators_user(void) {}
523 void rgb_matrix_init(void) {
524 rgb_matrix_driver.init();
526 // TODO: put the 1 second startup delay here?
528 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
529 g_last_hit_tracker.count = 0;
530 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
531 g_last_hit_tracker.tick[i] = UINT16_MAX;
534 last_hit_buffer.count = 0;
535 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
536 last_hit_buffer.tick[i] = UINT16_MAX;
538 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
540 if (!eeconfig_is_enabled()) {
541 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
543 eeconfig_update_rgb_matrix_default();
546 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
547 rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
548 if (!rgb_matrix_config.mode) {
549 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
550 eeconfig_update_rgb_matrix_default();
551 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
553 eeconfig_debug_rgb_matrix(); // display current eeprom values
556 void rgb_matrix_set_suspend_state(bool state) {
557 g_suspend_state = state;
560 void rgb_matrix_toggle(void) {
561 rgb_matrix_config.enable ^= 1;
562 rgb_task_state = STARTING;
563 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
566 void rgb_matrix_enable(void) {
567 rgb_matrix_enable_noeeprom();
568 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
571 void rgb_matrix_enable_noeeprom(void) {
572 if (!rgb_matrix_config.enable)
573 rgb_task_state = STARTING;
574 rgb_matrix_config.enable = 1;
577 void rgb_matrix_disable(void) {
578 rgb_matrix_disable_noeeprom();
579 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
582 void rgb_matrix_disable_noeeprom(void) {
583 if (rgb_matrix_config.enable)
584 rgb_task_state = STARTING;
585 rgb_matrix_config.enable = 0;
588 void rgb_matrix_step(void) {
589 rgb_matrix_config.mode++;
590 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
591 rgb_matrix_config.mode = 1;
592 rgb_task_state = STARTING;
593 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
596 void rgb_matrix_step_reverse(void) {
597 rgb_matrix_config.mode--;
598 if (rgb_matrix_config.mode < 1)
599 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
600 rgb_task_state = STARTING;
601 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
604 void rgb_matrix_increase_hue(void) {
605 rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
606 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
609 void rgb_matrix_decrease_hue(void) {
610 rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
611 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
614 void rgb_matrix_increase_sat(void) {
615 rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
616 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
619 void rgb_matrix_decrease_sat(void) {
620 rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
621 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
624 void rgb_matrix_increase_val(void) {
625 rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
626 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
627 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
628 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
631 void rgb_matrix_decrease_val(void) {
632 rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
633 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
636 void rgb_matrix_increase_speed(void) {
637 rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
638 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
641 void rgb_matrix_decrease_speed(void) {
642 rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
643 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
646 led_flags_t rgb_matrix_get_flags(void) {
647 return rgb_effect_params.flags;
650 void rgb_matrix_set_flags(led_flags_t flags) {
651 rgb_effect_params.flags = flags;
654 void rgb_matrix_mode(uint8_t mode) {
655 rgb_matrix_config.mode = mode;
656 rgb_task_state = STARTING;
657 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
660 void rgb_matrix_mode_noeeprom(uint8_t mode) {
661 rgb_matrix_config.mode = mode;
664 uint8_t rgb_matrix_get_mode(void) {
665 return rgb_matrix_config.mode;
668 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
669 rgb_matrix_sethsv_noeeprom(hue, sat, val);
670 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
673 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
674 rgb_matrix_config.hue = hue;
675 rgb_matrix_config.sat = sat;
676 rgb_matrix_config.val = val;
677 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
678 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;