Relocate grave keycode processing (#8082)
[jackhill/qmk/firmware.git] / quantum / rgb_matrix.c
1 /* Copyright 2017 Jason Williams
2 * Copyright 2017 Jack Humbert
3 * Copyright 2018 Yiancar
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19 #include "rgb_matrix.h"
20 #include "progmem.h"
21 #include "config.h"
22 #include "eeprom.h"
23 #include <string.h>
24 #include <math.h>
25
26 #include "lib/lib8tion/lib8tion.h"
27
28 #ifndef RGB_MATRIX_CENTER
29 const point_t k_rgb_matrix_center = {112, 32};
30 #else
31 const point_t k_rgb_matrix_center = RGB_MATRIX_CENTER;
32 #endif
33
34 // Generic effect runners
35 #include "rgb_matrix_runners/effect_runner_dx_dy_dist.h"
36 #include "rgb_matrix_runners/effect_runner_dx_dy.h"
37 #include "rgb_matrix_runners/effect_runner_i.h"
38 #include "rgb_matrix_runners/effect_runner_sin_cos_i.h"
39 #include "rgb_matrix_runners/effect_runner_reactive.h"
40 #include "rgb_matrix_runners/effect_runner_reactive_splash.h"
41
42 // ------------------------------------------
43 // -----Begin rgb effect includes macros-----
44 #define RGB_MATRIX_EFFECT(name)
45 #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
46
47 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
48 #ifdef RGB_MATRIX_CUSTOM_KB
49 # include "rgb_matrix_kb.inc"
50 #endif
51 #ifdef RGB_MATRIX_CUSTOM_USER
52 # include "rgb_matrix_user.inc"
53 #endif
54
55 #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
56 #undef RGB_MATRIX_EFFECT
57 // -----End rgb effect includes macros-------
58 // ------------------------------------------
59
60 #ifndef RGB_DISABLE_AFTER_TIMEOUT
61 # define RGB_DISABLE_AFTER_TIMEOUT 0
62 #endif
63
64 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
65 # define RGB_DISABLE_WHEN_USB_SUSPENDED false
66 #endif
67
68 #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX
69 # undef RGB_MATRIX_MAXIMUM_BRIGHTNESS
70 # define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX
71 #endif
72
73 #if !defined(RGB_MATRIX_HUE_STEP)
74 # define RGB_MATRIX_HUE_STEP 8
75 #endif
76
77 #if !defined(RGB_MATRIX_SAT_STEP)
78 # define RGB_MATRIX_SAT_STEP 16
79 #endif
80
81 #if !defined(RGB_MATRIX_VAL_STEP)
82 # define RGB_MATRIX_VAL_STEP 16
83 #endif
84
85 #if !defined(RGB_MATRIX_SPD_STEP)
86 # define RGB_MATRIX_SPD_STEP 16
87 #endif
88
89 #if !defined(RGB_MATRIX_STARTUP_MODE)
90 # ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT
91 # define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT
92 # else
93 // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace
94 # define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR
95 # endif
96 #endif
97
98 #if !defined(RGB_MATRIX_STARTUP_HUE)
99 # define RGB_MATRIX_STARTUP_HUE 0
100 #endif
101
102 #if !defined(RGB_MATRIX_STARTUP_SAT)
103 # define RGB_MATRIX_STARTUP_SAT UINT8_MAX
104 #endif
105
106 #if !defined(RGB_MATRIX_STARTUP_VAL)
107 # define RGB_MATRIX_STARTUP_VAL RGB_MATRIX_MAXIMUM_BRIGHTNESS
108 #endif
109
110 #if !defined(RGB_MATRIX_STARTUP_SPD)
111 # define RGB_MATRIX_STARTUP_SPD UINT8_MAX / 2
112 #endif
113
114
115 bool g_suspend_state = false;
116
117 rgb_config_t rgb_matrix_config;
118
119 rgb_counters_t g_rgb_counters;
120 static uint32_t rgb_counters_buffer;
121
122 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
123 uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
124 #endif
125
126 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
127 last_hit_t g_last_hit_tracker;
128 static last_hit_t last_hit_buffer;
129 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
130
131 void eeconfig_read_rgb_matrix(void) { eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
132
133 void eeconfig_update_rgb_matrix(void) { eeprom_update_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
134
135 void eeconfig_update_rgb_matrix_default(void) {
136 dprintf("eeconfig_update_rgb_matrix_default\n");
137 rgb_matrix_config.enable = 1;
138 rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE;
139 rgb_matrix_config.hsv = (HSV){RGB_MATRIX_STARTUP_HUE, RGB_MATRIX_STARTUP_SAT, RGB_MATRIX_STARTUP_VAL};
140 rgb_matrix_config.speed = RGB_MATRIX_STARTUP_SPD;
141 eeconfig_update_rgb_matrix();
142 }
143
144 void eeconfig_debug_rgb_matrix(void) {
145 dprintf("rgb_matrix_config eprom\n");
146 dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
147 dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
148 dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h);
149 dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
150 dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
151 dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
152 }
153
154 __attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
155
156 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
157 uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
158 uint8_t led_index = g_led_config.matrix_co[row][column];
159 if (led_index != NO_LED) {
160 led_i[led_count] = led_index;
161 led_count++;
162 }
163 return led_count;
164 }
165
166 void rgb_matrix_update_pwm_buffers(void) { rgb_matrix_driver.flush(); }
167
168 void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color(index, red, green, blue); }
169
170 void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color_all(red, green, blue); }
171
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;
176
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;
181 }
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;
186 }
187 # endif // defined(RGB_MATRIX_KEYRELEASES)
188
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--;
195 }
196
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_led_config.point[led[i]].x;
200 last_hit_buffer.y[index] = g_led_config.point[led[i]].y;
201 last_hit_buffer.index[index] = led[i];
202 last_hit_buffer.tick[index] = 0;
203 last_hit_buffer.count++;
204 }
205 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
206
207 #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
208 if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
209 process_rgb_matrix_typing_heatmap(record);
210 }
211 #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
212
213 return true;
214 }
215
216 void rgb_matrix_test(void) {
217 // Mask out bits 4 and 5
218 // Increase the factor to make the test animation slower (and reduce to make it faster)
219 uint8_t factor = 10;
220 switch ((g_rgb_counters.tick & (0b11 << factor)) >> factor) {
221 case 0: {
222 rgb_matrix_set_color_all(20, 0, 0);
223 break;
224 }
225 case 1: {
226 rgb_matrix_set_color_all(0, 20, 0);
227 break;
228 }
229 case 2: {
230 rgb_matrix_set_color_all(0, 0, 20);
231 break;
232 }
233 case 3: {
234 rgb_matrix_set_color_all(20, 20, 20);
235 break;
236 }
237 }
238 }
239
240 static bool rgb_matrix_none(effect_params_t *params) {
241 if (!params->init) {
242 return false;
243 }
244
245 RGB_MATRIX_USE_LIMITS(led_min, led_max);
246 for (uint8_t i = led_min; i < led_max; i++) {
247 rgb_matrix_set_color(i, 0, 0, 0);
248 }
249 return led_max < DRIVER_LED_TOTAL;
250 }
251
252 static uint8_t rgb_last_enable = UINT8_MAX;
253 static uint8_t rgb_last_effect = UINT8_MAX;
254 static effect_params_t rgb_effect_params = {0, 0xFF};
255 static rgb_task_states rgb_task_state = SYNCING;
256
257 static void rgb_task_timers(void) {
258 // Update double buffer timers
259 uint16_t deltaTime = timer_elapsed32(rgb_counters_buffer);
260 rgb_counters_buffer = timer_read32();
261 if (g_rgb_counters.any_key_hit < UINT32_MAX) {
262 if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
263 g_rgb_counters.any_key_hit = UINT32_MAX;
264 } else {
265 g_rgb_counters.any_key_hit += deltaTime;
266 }
267 }
268
269 // Update double buffer last hit timers
270 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
271 uint8_t count = last_hit_buffer.count;
272 for (uint8_t i = 0; i < count; ++i) {
273 if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
274 last_hit_buffer.count--;
275 continue;
276 }
277 last_hit_buffer.tick[i] += deltaTime;
278 }
279 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
280 }
281
282 static void rgb_task_sync(void) {
283 // next task
284 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
285 }
286
287 static void rgb_task_start(void) {
288 // reset iter
289 rgb_effect_params.iter = 0;
290
291 // update double buffers
292 g_rgb_counters.tick = rgb_counters_buffer;
293 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
294 g_last_hit_tracker = last_hit_buffer;
295 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
296
297 // next task
298 rgb_task_state = RENDERING;
299 }
300
301 static void rgb_task_render(uint8_t effect) {
302 bool rendering = false;
303 rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
304
305 // each effect can opt to do calculations
306 // and/or request PWM buffer updates.
307 switch (effect) {
308 case RGB_MATRIX_NONE:
309 rendering = rgb_matrix_none(&rgb_effect_params);
310 break;
311
312 // ---------------------------------------------
313 // -----Begin rgb effect switch case macros-----
314 #define RGB_MATRIX_EFFECT(name, ...) \
315 case RGB_MATRIX_##name: \
316 rendering = name(&rgb_effect_params); \
317 break;
318 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
319 #undef RGB_MATRIX_EFFECT
320
321 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
322 # define RGB_MATRIX_EFFECT(name, ...) \
323 case RGB_MATRIX_CUSTOM_##name: \
324 rendering = name(&rgb_effect_params); \
325 break;
326 # ifdef RGB_MATRIX_CUSTOM_KB
327 # include "rgb_matrix_kb.inc"
328 # endif
329 # ifdef RGB_MATRIX_CUSTOM_USER
330 # include "rgb_matrix_user.inc"
331 # endif
332 # undef RGB_MATRIX_EFFECT
333 #endif
334 // -----End rgb effect switch case macros-------
335 // ---------------------------------------------
336
337 // Factory default magic value
338 case UINT8_MAX: {
339 rgb_matrix_test();
340 rgb_task_state = FLUSHING;
341 }
342 return;
343 }
344
345 rgb_effect_params.iter++;
346
347 // next task
348 if (!rendering) {
349 rgb_task_state = FLUSHING;
350 if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
351 // We only need to flush once if we are RGB_MATRIX_NONE
352 rgb_task_state = SYNCING;
353 }
354 }
355 }
356
357 static void rgb_task_flush(uint8_t effect) {
358 // update last trackers after the first full render so we can init over several frames
359 rgb_last_effect = effect;
360 rgb_last_enable = rgb_matrix_config.enable;
361
362 // update pwm buffers
363 rgb_matrix_update_pwm_buffers();
364
365 // next task
366 rgb_task_state = SYNCING;
367 }
368
369 void rgb_matrix_task(void) {
370 rgb_task_timers();
371
372 // Ideally we would also stop sending zeros to the LED driver PWM buffers
373 // while suspended and just do a software shutdown. This is a cheap hack for now.
374 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));
375 uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
376
377 switch (rgb_task_state) {
378 case STARTING:
379 rgb_task_start();
380 break;
381 case RENDERING:
382 rgb_task_render(effect);
383 break;
384 case FLUSHING:
385 rgb_task_flush(effect);
386 break;
387 case SYNCING:
388 rgb_task_sync();
389 break;
390 }
391
392 if (!suspend_backlight) {
393 rgb_matrix_indicators();
394 }
395 }
396
397 void rgb_matrix_indicators(void) {
398 rgb_matrix_indicators_kb();
399 rgb_matrix_indicators_user();
400 }
401
402 __attribute__((weak)) void rgb_matrix_indicators_kb(void) {}
403
404 __attribute__((weak)) void rgb_matrix_indicators_user(void) {}
405
406 void rgb_matrix_init(void) {
407 rgb_matrix_driver.init();
408
409 // TODO: put the 1 second startup delay here?
410
411 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
412 g_last_hit_tracker.count = 0;
413 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
414 g_last_hit_tracker.tick[i] = UINT16_MAX;
415 }
416
417 last_hit_buffer.count = 0;
418 for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
419 last_hit_buffer.tick[i] = UINT16_MAX;
420 }
421 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
422
423 if (!eeconfig_is_enabled()) {
424 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
425 eeconfig_init();
426 eeconfig_update_rgb_matrix_default();
427 }
428
429 eeconfig_read_rgb_matrix();
430 if (!rgb_matrix_config.mode) {
431 dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
432 eeconfig_update_rgb_matrix_default();
433 }
434 eeconfig_debug_rgb_matrix(); // display current eeprom values
435 }
436
437 void rgb_matrix_set_suspend_state(bool state) {
438 if (RGB_DISABLE_WHEN_USB_SUSPENDED && state) {
439 rgb_matrix_set_color_all(0, 0, 0); // turn off all LEDs when suspending
440 }
441 g_suspend_state = state;
442 }
443
444 void rgb_matrix_toggle(void) {
445 rgb_matrix_config.enable ^= 1;
446 rgb_task_state = STARTING;
447 eeconfig_update_rgb_matrix();
448 }
449
450 void rgb_matrix_enable(void) {
451 rgb_matrix_enable_noeeprom();
452 eeconfig_update_rgb_matrix();
453 }
454
455 void rgb_matrix_enable_noeeprom(void) {
456 if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
457 rgb_matrix_config.enable = 1;
458 }
459
460 void rgb_matrix_disable(void) {
461 rgb_matrix_disable_noeeprom();
462 eeconfig_update_rgb_matrix();
463 }
464
465 void rgb_matrix_disable_noeeprom(void) {
466 if (rgb_matrix_config.enable) rgb_task_state = STARTING;
467 rgb_matrix_config.enable = 0;
468 }
469
470 void rgb_matrix_step(void) {
471 rgb_matrix_config.mode++;
472 if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) rgb_matrix_config.mode = 1;
473 rgb_task_state = STARTING;
474 eeconfig_update_rgb_matrix();
475 }
476
477 void rgb_matrix_step_reverse(void) {
478 rgb_matrix_config.mode--;
479 if (rgb_matrix_config.mode < 1) rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
480 rgb_task_state = STARTING;
481 eeconfig_update_rgb_matrix();
482 }
483
484 void rgb_matrix_increase_hue(void) {
485 rgb_matrix_config.hsv.h += RGB_MATRIX_HUE_STEP;
486 eeconfig_update_rgb_matrix();
487 }
488
489 void rgb_matrix_decrease_hue(void) {
490 rgb_matrix_config.hsv.h -= RGB_MATRIX_HUE_STEP;
491 eeconfig_update_rgb_matrix();
492 }
493
494 void rgb_matrix_increase_sat(void) {
495 rgb_matrix_config.hsv.s = qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
496 eeconfig_update_rgb_matrix();
497 }
498
499 void rgb_matrix_decrease_sat(void) {
500 rgb_matrix_config.hsv.s = qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
501 eeconfig_update_rgb_matrix();
502 }
503
504 void rgb_matrix_increase_val(void) {
505 rgb_matrix_config.hsv.v = qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
506 if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS) rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
507 eeconfig_update_rgb_matrix();
508 }
509
510 void rgb_matrix_decrease_val(void) {
511 rgb_matrix_config.hsv.v = qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
512 eeconfig_update_rgb_matrix();
513 }
514
515 void rgb_matrix_increase_speed(void) {
516 rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
517 eeconfig_update_rgb_matrix();
518 }
519
520 void rgb_matrix_decrease_speed(void) {
521 rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
522 eeconfig_update_rgb_matrix();
523 }
524
525 led_flags_t rgb_matrix_get_flags(void) { return rgb_effect_params.flags; }
526
527 void rgb_matrix_set_flags(led_flags_t flags) { rgb_effect_params.flags = flags; }
528
529 void rgb_matrix_mode(uint8_t mode) {
530 rgb_matrix_config.mode = mode;
531 rgb_task_state = STARTING;
532 eeconfig_update_rgb_matrix();
533 }
534
535 void rgb_matrix_mode_noeeprom(uint8_t mode) { rgb_matrix_config.mode = mode; }
536
537 uint8_t rgb_matrix_get_mode(void) { return rgb_matrix_config.mode; }
538
539 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
540 rgb_matrix_sethsv_noeeprom(hue, sat, val);
541 eeconfig_update_rgb_matrix();
542 }
543
544 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
545 rgb_matrix_config.hsv.h = hue;
546 rgb_matrix_config.hsv.s = sat;
547 rgb_matrix_config.hsv.v = val;
548 if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS) rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
549 }