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