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.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
210 #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
211 if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
212 process_rgb_matrix_typing_heatmap(record);
214 #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
219 void rgb_matrix_test(void) {
220 // Mask out bits 4 and 5
221 // Increase the factor to make the test animation slower (and reduce to make it faster)
223 switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
226 rgb_matrix_set_color_all( 20, 0, 0 );
230 rgb_matrix_set_color_all( 0, 20, 0 );
234 rgb_matrix_set_color_all( 0, 0, 20 );
238 rgb_matrix_set_color_all( 20, 20, 20 );
244 static bool rgb_matrix_none(effect_params_t* params) {
249 RGB_MATRIX_USE_LIMITS(led_min, led_max);
250 for (uint8_t i = led_min; i < led_max; i++) {
251 rgb_matrix_set_color(i, 0, 0, 0);
253 return led_max < DRIVER_LED_TOTAL;
256 static uint8_t rgb_last_enable = UINT8_MAX;
257 static uint8_t rgb_last_effect = UINT8_MAX;
258 static effect_params_t rgb_effect_params = { 0, 0xFF };
259 static rgb_task_states rgb_task_state = SYNCING;
261 static void rgb_task_timers(void) {
262 // Update double buffer timers
263 uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
264 rgb_counters_buffer = timer_read32();
265 if (g_rgb_counters.any_key_hit < UINT32_MAX) {
266 if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
267 g_rgb_counters.any_key_hit = UINT32_MAX;
269 g_rgb_counters.any_key_hit += deltaTime;
273 // Update double buffer last hit timers
274 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
275 uint8_t count = last_hit_buffer.count;
276 for (uint8_t i = 0; i < count; ++i) {
277 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
278 last_hit_buffer.count--;
281 last_hit_buffer.tick[i] += deltaTime;
283 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
286 static void rgb_task_sync(void) {
288 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
289 rgb_task_state = STARTING;
292 static void rgb_task_start(void) {
294 rgb_effect_params.iter = 0;
296 // update double buffers
297 g_rgb_counters.tick = rgb_counters_buffer;
298 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
299 g_last_hit_tracker = last_hit_buffer;
300 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
303 rgb_task_state = RENDERING;
306 static void rgb_task_render(uint8_t effect) {
307 bool rendering = false;
308 rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
310 // each effect can opt to do calculations
311 // and/or request PWM buffer updates.
313 case RGB_MATRIX_NONE:
314 rendering = rgb_matrix_none(&rgb_effect_params);
317 // ---------------------------------------------
318 // -----Begin rgb effect switch case macros-----
319 #define RGB_MATRIX_EFFECT(name, ...) \
320 case RGB_MATRIX_##name: \
321 rendering = name(&rgb_effect_params); \
323 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
324 #undef RGB_MATRIX_EFFECT
326 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
327 #define RGB_MATRIX_EFFECT(name, ...) \
328 case RGB_MATRIX_CUSTOM_##name: \
329 rendering = name(&rgb_effect_params); \
331 #ifdef RGB_MATRIX_CUSTOM_KB
332 #include "rgb_matrix_kb.inc"
334 #ifdef RGB_MATRIX_CUSTOM_USER
335 #include "rgb_matrix_user.inc"
337 #undef RGB_MATRIX_EFFECT
339 // -----End rgb effect switch case macros-------
340 // ---------------------------------------------
342 // Factory default magic value
345 rgb_task_state = FLUSHING;
350 rgb_effect_params.iter++;
354 rgb_task_state = FLUSHING;
355 if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
356 // We only need to flush once if we are RGB_MATRIX_NONE
357 rgb_task_state = SYNCING;
362 static void rgb_task_flush(uint8_t effect) {
363 // update last trackers after the first full render so we can init over several frames
364 rgb_last_effect = effect;
365 rgb_last_enable = rgb_matrix_config.enable;
367 // update pwm buffers
368 rgb_matrix_update_pwm_buffers();
371 rgb_task_state = SYNCING;
374 void rgb_matrix_task(void) {
377 // Ideally we would also stop sending zeros to the LED driver PWM buffers
378 // while suspended and just do a software shutdown. This is a cheap hack for now.
379 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));
380 uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
382 switch (rgb_task_state) {
387 rgb_task_render(effect);
390 rgb_task_flush(effect);
397 if (!suspend_backlight) {
398 rgb_matrix_indicators();
402 void rgb_matrix_indicators(void) {
403 rgb_matrix_indicators_kb();
404 rgb_matrix_indicators_user();
407 __attribute__((weak))
408 void rgb_matrix_indicators_kb(void) {}
410 __attribute__((weak))
411 void rgb_matrix_indicators_user(void) {}
413 void rgb_matrix_init(void) {
414 rgb_matrix_driver.init();
416 // TODO: put the 1 second startup delay here?
418 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
419 g_last_hit_tracker.count = 0;
420 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
421 g_last_hit_tracker.tick[i] = UINT16_MAX;
424 last_hit_buffer.count = 0;
425 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
426 last_hit_buffer.tick[i] = UINT16_MAX;
428 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
430 if (!eeconfig_is_enabled()) {
431 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
433 eeconfig_update_rgb_matrix_default();
436 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
437 rgb_matrix_config.speed = UINT8_MAX / 2; //EECONFIG needs to be increased to support this
438 if (!rgb_matrix_config.mode) {
439 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
440 eeconfig_update_rgb_matrix_default();
441 rgb_matrix_config.raw = eeconfig_read_rgb_matrix();
443 eeconfig_debug_rgb_matrix(); // display current eeprom values
446 void rgb_matrix_set_suspend_state(bool state) {
447 g_suspend_state = state;
450 void rgb_matrix_toggle(void) {
451 rgb_matrix_config.enable ^= 1;
452 rgb_task_state = STARTING;
453 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
456 void rgb_matrix_enable(void) {
457 rgb_matrix_enable_noeeprom();
458 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
461 void rgb_matrix_enable_noeeprom(void) {
462 if (!rgb_matrix_config.enable)
463 rgb_task_state = STARTING;
464 rgb_matrix_config.enable = 1;
467 void rgb_matrix_disable(void) {
468 rgb_matrix_disable_noeeprom();
469 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
472 void rgb_matrix_disable_noeeprom(void) {
473 if (rgb_matrix_config.enable)
474 rgb_task_state = STARTING;
475 rgb_matrix_config.enable = 0;
478 void rgb_matrix_step(void) {
479 rgb_matrix_config.mode++;
480 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX)
481 rgb_matrix_config.mode = 1;
482 rgb_task_state = STARTING;
483 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
486 void rgb_matrix_step_reverse(void) {
487 rgb_matrix_config.mode--;
488 if (rgb_matrix_config.mode < 1)
489 rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
490 rgb_task_state = STARTING;
491 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
494 void rgb_matrix_increase_hue(void) {
495 rgb_matrix_config.hue += RGB_MATRIX_HUE_STEP;
496 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
499 void rgb_matrix_decrease_hue(void) {
500 rgb_matrix_config.hue -= RGB_MATRIX_HUE_STEP;
501 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
504 void rgb_matrix_increase_sat(void) {
505 rgb_matrix_config.sat = qadd8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
506 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
509 void rgb_matrix_decrease_sat(void) {
510 rgb_matrix_config.sat = qsub8(rgb_matrix_config.sat, RGB_MATRIX_SAT_STEP);
511 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
514 void rgb_matrix_increase_val(void) {
515 rgb_matrix_config.val = qadd8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
516 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
517 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
518 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
521 void rgb_matrix_decrease_val(void) {
522 rgb_matrix_config.val = qsub8(rgb_matrix_config.val, RGB_MATRIX_VAL_STEP);
523 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
526 void rgb_matrix_increase_speed(void) {
527 rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
528 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
531 void rgb_matrix_decrease_speed(void) {
532 rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
533 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);//EECONFIG needs to be increased to support this
536 led_flags_t rgb_matrix_get_flags(void) {
537 return rgb_effect_params.flags;
540 void rgb_matrix_set_flags(led_flags_t flags) {
541 rgb_effect_params.flags = flags;
544 void rgb_matrix_mode(uint8_t mode) {
545 rgb_matrix_config.mode = mode;
546 rgb_task_state = STARTING;
547 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
550 void rgb_matrix_mode_noeeprom(uint8_t mode) {
551 rgb_matrix_config.mode = mode;
554 uint8_t rgb_matrix_get_mode(void) {
555 return rgb_matrix_config.mode;
558 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
559 rgb_matrix_sethsv_noeeprom(hue, sat, val);
560 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
563 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
564 rgb_matrix_config.hue = hue;
565 rgb_matrix_config.sat = sat;
566 rgb_matrix_config.val = val;
567 if (rgb_matrix_config.val > RGB_MATRIX_MAXIMUM_BRIGHTNESS)
568 rgb_matrix_config.val = RGB_MATRIX_MAXIMUM_BRIGHTNESS;