]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgb_matrix.c
[Keyboard] Doro67 cleanup (#6514)
[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 bool g_suspend_state = false;
99
100 rgb_config_t rgb_matrix_config;
101
102 rgb_counters_t  g_rgb_counters;
103 static uint32_t rgb_counters_buffer;
104
105 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
106 uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
107 #endif
108
109 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
110 last_hit_t        g_last_hit_tracker;
111 static last_hit_t last_hit_buffer;
112 #endif  // RGB_MATRIX_KEYREACTIVE_ENABLED
113
114 void eeconfig_read_rgb_matrix(void) { eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
115
116 void eeconfig_update_rgb_matrix(void) { eeprom_update_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); }
117
118 void eeconfig_update_rgb_matrix_default(void) {
119     dprintf("eeconfig_update_rgb_matrix_default\n");
120     rgb_matrix_config.enable = 1;
121     rgb_matrix_config.mode   = RGB_MATRIX_STARTUP_MODE;
122     rgb_matrix_config.hsv    = (HSV){0, UINT8_MAX, RGB_MATRIX_MAXIMUM_BRIGHTNESS};
123     rgb_matrix_config.speed  = UINT8_MAX / 2;
124     eeconfig_update_rgb_matrix();
125 }
126
127 void eeconfig_debug_rgb_matrix(void) {
128     dprintf("rgb_matrix_config eprom\n");
129     dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable);
130     dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode);
131     dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h);
132     dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s);
133     dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v);
134     dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed);
135 }
136
137 __attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; }
138
139 uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) {
140     uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i);
141     uint8_t led_index = g_led_config.matrix_co[row][column];
142     if (led_index != NO_LED) {
143         led_i[led_count] = led_index;
144         led_count++;
145     }
146     return led_count;
147 }
148
149 void rgb_matrix_update_pwm_buffers(void) { rgb_matrix_driver.flush(); }
150
151 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); }
152
153 void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { rgb_matrix_driver.set_color_all(red, green, blue); }
154
155 bool process_rgb_matrix(uint16_t keycode, keyrecord_t *record) {
156 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
157     uint8_t led[LED_HITS_TO_REMEMBER];
158     uint8_t led_count = 0;
159
160 #    if defined(RGB_MATRIX_KEYRELEASES)
161     if (!record->event.pressed) {
162         led_count                  = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
163         g_rgb_counters.any_key_hit = 0;
164     }
165 #    elif defined(RGB_MATRIX_KEYPRESSES)
166     if (record->event.pressed) {
167         led_count                  = rgb_matrix_map_row_column_to_led(record->event.key.row, record->event.key.col, led);
168         g_rgb_counters.any_key_hit = 0;
169     }
170 #    endif  // defined(RGB_MATRIX_KEYRELEASES)
171
172     if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) {
173         memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count);
174         memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count);
175         memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2);  // 16 bit
176         memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count);
177         last_hit_buffer.count--;
178     }
179
180     for (uint8_t i = 0; i < led_count; i++) {
181         uint8_t index                = last_hit_buffer.count;
182         last_hit_buffer.x[index]     = g_led_config.point[led[i]].x;
183         last_hit_buffer.y[index]     = g_led_config.point[led[i]].y;
184         last_hit_buffer.index[index] = led[i];
185         last_hit_buffer.tick[index]  = 0;
186         last_hit_buffer.count++;
187     }
188 #endif  // RGB_MATRIX_KEYREACTIVE_ENABLED
189
190 #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
191     if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) {
192         process_rgb_matrix_typing_heatmap(record);
193     }
194 #endif  // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
195
196     return true;
197 }
198
199 void rgb_matrix_test(void) {
200     // Mask out bits 4 and 5
201     // Increase the factor to make the test animation slower (and reduce to make it faster)
202     uint8_t factor = 10;
203     switch ((g_rgb_counters.tick & (0b11 << factor)) >> factor) {
204         case 0: {
205             rgb_matrix_set_color_all(20, 0, 0);
206             break;
207         }
208         case 1: {
209             rgb_matrix_set_color_all(0, 20, 0);
210             break;
211         }
212         case 2: {
213             rgb_matrix_set_color_all(0, 0, 20);
214             break;
215         }
216         case 3: {
217             rgb_matrix_set_color_all(20, 20, 20);
218             break;
219         }
220     }
221 }
222
223 static bool rgb_matrix_none(effect_params_t *params) {
224     if (!params->init) {
225         return false;
226     }
227
228     RGB_MATRIX_USE_LIMITS(led_min, led_max);
229     for (uint8_t i = led_min; i < led_max; i++) {
230         rgb_matrix_set_color(i, 0, 0, 0);
231     }
232     return led_max < DRIVER_LED_TOTAL;
233 }
234
235 static uint8_t         rgb_last_enable   = UINT8_MAX;
236 static uint8_t         rgb_last_effect   = UINT8_MAX;
237 static effect_params_t rgb_effect_params = {0, 0xFF};
238 static rgb_task_states rgb_task_state    = SYNCING;
239
240 static void rgb_task_timers(void) {
241     // Update double buffer timers
242     uint16_t deltaTime  = timer_elapsed32(rgb_counters_buffer);
243     rgb_counters_buffer = timer_read32();
244     if (g_rgb_counters.any_key_hit < UINT32_MAX) {
245         if (UINT32_MAX - deltaTime < g_rgb_counters.any_key_hit) {
246             g_rgb_counters.any_key_hit = UINT32_MAX;
247         } else {
248             g_rgb_counters.any_key_hit += deltaTime;
249         }
250     }
251
252     // Update double buffer last hit timers
253 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
254     uint8_t count = last_hit_buffer.count;
255     for (uint8_t i = 0; i < count; ++i) {
256         if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) {
257             last_hit_buffer.count--;
258             continue;
259         }
260         last_hit_buffer.tick[i] += deltaTime;
261     }
262 #endif  // RGB_MATRIX_KEYREACTIVE_ENABLED
263 }
264
265 static void rgb_task_sync(void) {
266     // next task
267     if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING;
268 }
269
270 static void rgb_task_start(void) {
271     // reset iter
272     rgb_effect_params.iter = 0;
273
274     // update double buffers
275     g_rgb_counters.tick = rgb_counters_buffer;
276 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
277     g_last_hit_tracker = last_hit_buffer;
278 #endif  // RGB_MATRIX_KEYREACTIVE_ENABLED
279
280     // next task
281     rgb_task_state = RENDERING;
282 }
283
284 static void rgb_task_render(uint8_t effect) {
285     bool rendering         = false;
286     rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable);
287
288     // each effect can opt to do calculations
289     // and/or request PWM buffer updates.
290     switch (effect) {
291         case RGB_MATRIX_NONE:
292             rendering = rgb_matrix_none(&rgb_effect_params);
293             break;
294
295 // ---------------------------------------------
296 // -----Begin rgb effect switch case macros-----
297 #define RGB_MATRIX_EFFECT(name, ...)          \
298     case RGB_MATRIX_##name:                   \
299         rendering = name(&rgb_effect_params); \
300         break;
301 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
302 #undef RGB_MATRIX_EFFECT
303
304 #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER)
305 #    define RGB_MATRIX_EFFECT(name, ...)          \
306         case RGB_MATRIX_CUSTOM_##name:            \
307             rendering = name(&rgb_effect_params); \
308             break;
309 #    ifdef RGB_MATRIX_CUSTOM_KB
310 #        include "rgb_matrix_kb.inc"
311 #    endif
312 #    ifdef RGB_MATRIX_CUSTOM_USER
313 #        include "rgb_matrix_user.inc"
314 #    endif
315 #    undef RGB_MATRIX_EFFECT
316 #endif
317             // -----End rgb effect switch case macros-------
318             // ---------------------------------------------
319
320         // Factory default magic value
321         case UINT8_MAX: {
322             rgb_matrix_test();
323             rgb_task_state = FLUSHING;
324         }
325             return;
326     }
327
328     rgb_effect_params.iter++;
329
330     // next task
331     if (!rendering) {
332         rgb_task_state = FLUSHING;
333         if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) {
334             // We only need to flush once if we are RGB_MATRIX_NONE
335             rgb_task_state = SYNCING;
336         }
337     }
338 }
339
340 static void rgb_task_flush(uint8_t effect) {
341     // update last trackers after the first full render so we can init over several frames
342     rgb_last_effect = effect;
343     rgb_last_enable = rgb_matrix_config.enable;
344
345     // update pwm buffers
346     rgb_matrix_update_pwm_buffers();
347
348     // next task
349     rgb_task_state = SYNCING;
350 }
351
352 void rgb_matrix_task(void) {
353     rgb_task_timers();
354
355     // Ideally we would also stop sending zeros to the LED driver PWM buffers
356     // while suspended and just do a software shutdown. This is a cheap hack for now.
357     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));
358     uint8_t effect            = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode;
359
360     switch (rgb_task_state) {
361         case STARTING:
362             rgb_task_start();
363             break;
364         case RENDERING:
365             rgb_task_render(effect);
366             break;
367         case FLUSHING:
368             rgb_task_flush(effect);
369             break;
370         case SYNCING:
371             rgb_task_sync();
372             break;
373     }
374
375     if (!suspend_backlight) {
376         rgb_matrix_indicators();
377     }
378 }
379
380 void rgb_matrix_indicators(void) {
381     rgb_matrix_indicators_kb();
382     rgb_matrix_indicators_user();
383 }
384
385 __attribute__((weak)) void rgb_matrix_indicators_kb(void) {}
386
387 __attribute__((weak)) void rgb_matrix_indicators_user(void) {}
388
389 void rgb_matrix_init(void) {
390     rgb_matrix_driver.init();
391
392     // TODO: put the 1 second startup delay here?
393
394 #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED
395     g_last_hit_tracker.count = 0;
396     for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
397         g_last_hit_tracker.tick[i] = UINT16_MAX;
398     }
399
400     last_hit_buffer.count = 0;
401     for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) {
402         last_hit_buffer.tick[i] = UINT16_MAX;
403     }
404 #endif  // RGB_MATRIX_KEYREACTIVE_ENABLED
405
406     if (!eeconfig_is_enabled()) {
407         dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
408         eeconfig_init();
409         eeconfig_update_rgb_matrix_default();
410     }
411
412     eeconfig_read_rgb_matrix();
413     if (!rgb_matrix_config.mode) {
414         dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n");
415         eeconfig_update_rgb_matrix_default();
416     }
417     eeconfig_debug_rgb_matrix();  // display current eeprom values
418 }
419
420 void rgb_matrix_set_suspend_state(bool state) { g_suspend_state = state; }
421
422 void rgb_matrix_toggle(void) {
423     rgb_matrix_config.enable ^= 1;
424     rgb_task_state = STARTING;
425     eeconfig_update_rgb_matrix();
426 }
427
428 void rgb_matrix_enable(void) {
429     rgb_matrix_enable_noeeprom();
430     eeconfig_update_rgb_matrix();
431 }
432
433 void rgb_matrix_enable_noeeprom(void) {
434     if (!rgb_matrix_config.enable) rgb_task_state = STARTING;
435     rgb_matrix_config.enable = 1;
436 }
437
438 void rgb_matrix_disable(void) {
439     rgb_matrix_disable_noeeprom();
440     eeconfig_update_rgb_matrix();
441 }
442
443 void rgb_matrix_disable_noeeprom(void) {
444     if (rgb_matrix_config.enable) rgb_task_state = STARTING;
445     rgb_matrix_config.enable = 0;
446 }
447
448 void rgb_matrix_step(void) {
449     rgb_matrix_config.mode++;
450     if (rgb_matrix_config.mode >= RGB_MATRIX_EFFECT_MAX) rgb_matrix_config.mode = 1;
451     rgb_task_state = STARTING;
452     eeconfig_update_rgb_matrix();
453 }
454
455 void rgb_matrix_step_reverse(void) {
456     rgb_matrix_config.mode--;
457     if (rgb_matrix_config.mode < 1) rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1;
458     rgb_task_state = STARTING;
459     eeconfig_update_rgb_matrix();
460 }
461
462 void rgb_matrix_increase_hue(void) {
463     rgb_matrix_config.hsv.h += RGB_MATRIX_HUE_STEP;
464     eeconfig_update_rgb_matrix();
465 }
466
467 void rgb_matrix_decrease_hue(void) {
468     rgb_matrix_config.hsv.h -= RGB_MATRIX_HUE_STEP;
469     eeconfig_update_rgb_matrix();
470 }
471
472 void rgb_matrix_increase_sat(void) {
473     rgb_matrix_config.hsv.s = qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
474     eeconfig_update_rgb_matrix();
475 }
476
477 void rgb_matrix_decrease_sat(void) {
478     rgb_matrix_config.hsv.s = qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP);
479     eeconfig_update_rgb_matrix();
480 }
481
482 void rgb_matrix_increase_val(void) {
483     rgb_matrix_config.hsv.v = qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
484     if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS) rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
485     eeconfig_update_rgb_matrix();
486 }
487
488 void rgb_matrix_decrease_val(void) {
489     rgb_matrix_config.hsv.v = qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP);
490     eeconfig_update_rgb_matrix();
491 }
492
493 void rgb_matrix_increase_speed(void) {
494     rgb_matrix_config.speed = qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
495     eeconfig_update_rgb_matrix();
496 }
497
498 void rgb_matrix_decrease_speed(void) {
499     rgb_matrix_config.speed = qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP);
500     eeconfig_update_rgb_matrix();
501 }
502
503 led_flags_t rgb_matrix_get_flags(void) { return rgb_effect_params.flags; }
504
505 void rgb_matrix_set_flags(led_flags_t flags) { rgb_effect_params.flags = flags; }
506
507 void rgb_matrix_mode(uint8_t mode) {
508     rgb_matrix_config.mode = mode;
509     rgb_task_state         = STARTING;
510     eeconfig_update_rgb_matrix();
511 }
512
513 void rgb_matrix_mode_noeeprom(uint8_t mode) { rgb_matrix_config.mode = mode; }
514
515 uint8_t rgb_matrix_get_mode(void) { return rgb_matrix_config.mode; }
516
517 void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
518     rgb_matrix_sethsv_noeeprom(hue, sat, val);
519     eeconfig_update_rgb_matrix();
520 }
521
522 void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
523     rgb_matrix_config.hsv.h = hue;
524     rgb_matrix_config.hsv.s = sat;
525     rgb_matrix_config.hsv.v = val;
526     if (rgb_matrix_config.hsv.v > RGB_MATRIX_MAXIMUM_BRIGHTNESS) rgb_matrix_config.hsv.v = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
527 }