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