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 #ifndef RGB_MATRIX_CENTER
30 const point_t k_rgb_matrix_center = { 112, 32 };
32 const point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
35 // Generic effect runners
36 #include "rgb_matrix_runners/effect_runner_dx_dy_dist.h"
37 #include "rgb_matrix_runners/effect_runner_dx_dy.h"
38 #include "rgb_matrix_runners/effect_runner_i.h"
39 #include "rgb_matrix_runners/effect_runner_sin_cos_i.h"
40 #include "rgb_matrix_runners/effect_runner_reactive.h"
41 #include "rgb_matrix_runners/effect_runner_reactive_splash.h"
43 // ------------------------------------------
44 // -----Begin rgb effect includes macros-----
45 #define RGB_MATRIX_EFFECT(name)
46 #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
48 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
49 #ifdef RGB_MATRIX_CUSTOM_KB
50 #include "rgb_matrix_kb.inc"
52 #ifdef RGB_MATRIX_CUSTOM_USER
53 #include "rgb_matrix_user.inc"
56 #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
57 #undef RGB_MATRIX_EFFECT
58 // -----End rgb effect includes macros-------
59 // ------------------------------------------
61 #ifndef RGB_DISABLE_AFTER_TIMEOUT
62 #define RGB_DISABLE_AFTER_TIMEOUT 0
65 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
66 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
69 #ifndef EECONFIG_RGB_MATRIX
70 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
73 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
74 #undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
75 #define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
78 #if !defined(RGB_MATRIX_HUE_STEP)
79 #define RGB_MATRIX_HUE_STEP 8
82 #if !defined(RGB_MATRIX_SAT_STEP)
83 #define RGB_MATRIX_SAT_STEP 16
86 #if !defined(RGB_MATRIX_VAL_STEP)
87 #define RGB_MATRIX_VAL_STEP 16
90 #if !defined(RGB_MATRIX_SPD_STEP)
91 #define RGB_MATRIX_SPD_STEP 16
94 #if !defined(RGB_MATRIX_STARTUP_MODE)
95 #ifndef DISABLE_RGB_MATRIX_CYCLE_ALL
96 #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
98 // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
99 #define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
103 bool g_suspend_state = false;
105 rgb_config_t rgb_matrix_config;
107 rgb_counters_t g_rgb_counters;
108 static uint32_t rgb_counters_buffer;
110 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
111 uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
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.hsv = (HSV){ 0, UINT8_MAX, RGB_MATRIX_MAXIMUM_BRIGHTNESS };
132 rgb_matrix_config.speed = UINT8_MAX / 2;
133 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
136 void eeconfig_debug_rgb_matrix(void) {
137 dprintf("rgb_matrix_config eprom\n");
138 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
139 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
140 dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h);
141 dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
142 dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
143 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
146 __attribute__ ((weak))
147 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
151 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
152 uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
153 uint8_t led_index = g_led_config.matrix_co[row][column];
154 if (led_index != NO_LED) {
155 led_i[led_count] = led_index;
161 void rgb_matrix_update_pwm_buffers(void) {
162 rgb_matrix_driver.flush();
165 void rgb_matrix_set_color( int index, uint8_t red, uint8_t green, uint8_t blue ) {
166 rgb_matrix_driver.set_color(index, red, green, blue);
169 void rgb_matrix_set_color_all( uint8_t red, uint8_t green, uint8_t blue ) {
170 rgb_matrix_driver.set_color_all(red, green, blue);
173 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
174 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
175 uint8_t led[LED_HITS_TO_REMEMBER];
176 uint8_t led_count = 0;
178 #if defined(RGB_MATRIX_KEYRELEASES)
179 if (!record->event.pressed) {
180 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
181 g_rgb_counters.any_key_hit = 0;
183 #elif defined(RGB_MATRIX_KEYPRESSES)
184 if (record->event.pressed) {
185 led_count = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
186 g_rgb_counters.any_key_hit = 0;
188 #endif // defined(RGB_MATRIX_KEYRELEASES)
190 if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
191 memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
192 memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
193 memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit
194 memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
195 last_hit_buffer.count--;
198 for(uint8_t i = 0; i < led_count; i++) {
199 uint8_t index = last_hit_buffer.count;
200 last_hit_buffer.x[index] = g_led_config.point[led[i]].x;
201 last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
202 last_hit_buffer.index[index] = led[i];
203 last_hit_buffer.tick[index] = 0;
204 last_hit_buffer.count++;
206 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
208 #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
209 if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
210 process_rgb_matrix_typing_heatmap(record);
212 #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
217 void rgb_matrix_test(void) {
218 // Mask out bits 4 and 5
219 // Increase the factor to make the test animation slower (and reduce to make it faster)
221 switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
224 rgb_matrix_set_color_all( 20, 0, 0 );
228 rgb_matrix_set_color_all( 0, 20, 0 );
232 rgb_matrix_set_color_all( 0, 0, 20 );
236 rgb_matrix_set_color_all( 20, 20, 20 );
242 static bool rgb_matrix_none(effect_params_t* params) {
247 RGB_MATRIX_USE_LIMITS(led_min, led_max);
248 for (uint8_t i = led_min; i < led_max; i++) {
249 rgb_matrix_set_color(i, 0, 0, 0);
251 return led_max < DRIVER_LED_TOTAL;
254 static uint8_t rgb_last_enable = UINT8_MAX;
255 static uint8_t rgb_last_effect = UINT8_MAX;
256 static effect_params_t rgb_effect_params = { 0, 0xFF };
257 static rgb_task_states rgb_task_state = SYNCING;
259 static void rgb_task_timers(void) {
260 // Update double buffer timers
261 uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
262 rgb_counters_buffer = timer_read32();
263 if (g_rgb_counters.any_key_hit < UINT32_MAX) {
264 if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
265 g_rgb_counters.any_key_hit = UINT32_MAX;
267 g_rgb_counters.any_key_hit += deltaTime;
271 // Update double buffer last hit timers
272 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
273 uint8_t count = last_hit_buffer.count;
274 for (uint8_t i = 0; i < count; ++i) {
275 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
276 last_hit_buffer.count--;
279 last_hit_buffer.tick[i] += deltaTime;
281 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
284 static void rgb_task_sync(void) {
286 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
287 rgb_task_state = STARTING;
290 static void rgb_task_start(void) {
292 rgb_effect_params.iter = 0;
294 // update double buffers
295 g_rgb_counters.tick = rgb_counters_buffer;
296 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
297 g_last_hit_tracker = last_hit_buffer;
298 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
301 rgb_task_state = RENDERING;
304 static void rgb_task_render(uint8_t effect) {
305 bool rendering = false;
306 rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
308 // each effect can opt to do calculations
309 // and/or request PWM buffer updates.
311 case RGB_MATRIX_NONE:
312 rendering = rgb_matrix_none(&rgb_effect_params);
315 // ---------------------------------------------
316 // -----Begin rgb effect switch case macros-----
317 #define RGB_MATRIX_EFFECT(name, ...) \
318 case RGB_MATRIX_##name: \
319 rendering = name(&rgb_effect_params); \
321 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
322 #undef RGB_MATRIX_EFFECT
324 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
325 #define RGB_MATRIX_EFFECT(name, ...) \
326 case RGB_MATRIX_CUSTOM_##name: \
327 rendering = name(&rgb_effect_params); \
329 #ifdef RGB_MATRIX_CUSTOM_KB
330 #include "rgb_matrix_kb.inc"
332 #ifdef RGB_MATRIX_CUSTOM_USER
333 #include "rgb_matrix_user.inc"
335 #undef RGB_MATRIX_EFFECT
337 // -----End rgb effect switch case macros-------
338 // ---------------------------------------------
340 // Factory default magic value
343 rgb_task_state = FLUSHING;
348 rgb_effect_params.iter++;
352 rgb_task_state = FLUSHING;
353 if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
354 // We only need to flush once if we are RGB_MATRIX_NONE
355 rgb_task_state = SYNCING;
360 static void rgb_task_flush(uint8_t effect) {
361 // update last trackers after the first full render so we can init over several frames
362 rgb_last_effect = effect;
363 rgb_last_enable = rgb_matrix_config.enable;
365 // update pwm buffers
366 rgb_matrix_update_pwm_buffers();
369 rgb_task_state = SYNCING;
372 void rgb_matrix_task(void) {
375 // Ideally we would also stop sending zeros to the LED driver PWM buffers
376 // while suspended and just do a software shutdown. This is a cheap hack for now.
377 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));
378 uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
380 switch (rgb_task_state) {
385 rgb_task_render(effect);
388 rgb_task_flush(effect);
395 if (!suspend_backlight) {
396 rgb_matrix_indicators();
400 void rgb_matrix_indicators(void) {
401 rgb_matrix_indicators_kb();
402 rgb_matrix_indicators_user();
405 __attribute__((weak))
406 void rgb_matrix_indicators_kb(void) {}
408 __attribute__((weak))
409 void rgb_matrix_indicators_user(void) {}
411 void rgb_matrix_init(void) {
412 rgb_matrix_driver.init();
414 // TODO: put the 1 second startup delay here?
416 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
417 g_last_hit_tracker.count = 0;
418 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
419 g_last_hit_tracker.tick[i] = UINT16_MAX;
422 last_hit_buffer.count = 0;
423 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
424 last_hit_buffer.tick[i] = UINT16_MAX;
426 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
428 if (!eeconfig_is_enabled()) {
429 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
431 eeconfig_update_rgb_matrix_default();
434 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
435 rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
436 if (!rgb_matrix_config.mode) {
437 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
438 eeconfig_update_rgb_matrix_default();
439 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
441 eeconfig_debug_rgb_matrix(); // display current eeprom values
444 void rgb_matrix_set_suspend_state(bool state) {
445 g_suspend_state = state;
448 void rgb_matrix_toggle(void) {
449 rgb_matrix_config.enable ^= 1;
450 rgb_task_state = STARTING;
451 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
454 void rgb_matrix_enable(void) {
455 rgb_matrix_enable_noeeprom();
456 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
459 void rgb_matrix_enable_noeeprom(void) {
460 if (!rgb_matrix_config.enable)
461 rgb_task_state = STARTING;
462 rgb_matrix_config.enable = 1;
465 void rgb_matrix_disable(void) {
466 rgb_matrix_disable_noeeprom();
467 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
470 void rgb_matrix_disable_noeeprom(void) {
471 if (rgb_matrix_config.enable)
472 rgb_task_state = STARTING;
473 rgb_matrix_config.enable = 0;
476 void rgb_matrix_step(void) {
477 rgb_matrix_config.mode++;
478 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
479 rgb_matrix_config.mode = 1;
480 rgb_task_state = STARTING;
481 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
484 void rgb_matrix_step_reverse(void) {
485 rgb_matrix_config.mode--;
486 if (rgb_matrix_config.mode < 1)
487 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
488 rgb_task_state = STARTING;
489 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
492 void rgb_matrix_increase_hue(void) {
493 rgb_matrix_config.hsv.h += RGB_MATRIX_HUE_STEP;
494 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
497 void rgb_matrix_decrease_hue(void) {
498 rgb_matrix_config.hsv.h -= RGB_MATRIX_HUE_STEP;
499 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
502 void rgb_matrix_increase_sat(void) {
503 rgb_matrix_config.hsv.s = qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
504 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
507 void rgb_matrix_decrease_sat(void) {
508 rgb_matrix_config.hsv.s = qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
509 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
512 void rgb_matrix_increase_val(void) {
513 rgb_matrix_config.hsv.v = qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
514 if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
515 rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
516 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
519 void rgb_matrix_decrease_val(void) {
520 rgb_matrix_config.hsv.v = qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
521 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
524 void rgb_matrix_increase_speed(void) {
525 rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
526 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
529 void rgb_matrix_decrease_speed(void) {
530 rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
531 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
534 led_flags_t rgb_matrix_get_flags(void) {
535 return rgb_effect_params.flags;
538 void rgb_matrix_set_flags(led_flags_t flags) {
539 rgb_effect_params.flags = flags;
542 void rgb_matrix_mode(uint8_t mode) {
543 rgb_matrix_config.mode = mode;
544 rgb_task_state = STARTING;
545 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
548 void rgb_matrix_mode_noeeprom(uint8_t mode) {
549 rgb_matrix_config.mode = mode;
552 uint8_t rgb_matrix_get_mode(void) {
553 return rgb_matrix_config.mode;
556 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
557 rgb_matrix_sethsv_noeeprom(hue, sat, val);
558 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
561 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
562 rgb_matrix_config.hsv.h = hue;
563 rgb_matrix_config.hsv.s = sat;
564 rgb_matrix_config.hsv.v = val;
565 if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
566 rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;