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 // ------------------------------------------
30 // -----Begin rgb effect includes macros-----
31 #define RGB_MATRIX_EFFECT(name)
32 #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS
34 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
35 #ifdef RGB_MATRIX_CUSTOM_KB
36 #include "rgb_matrix_kb.inc"
38 #ifdef RGB_MATRIX_CUSTOM_USER
39 #include "rgb_matrix_user.inc"
42 #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS
43 #undef RGB_MATRIX_EFFECT
44 // -----End rgb effect includes macros-------
45 // ------------------------------------------
47 #ifndef RGB_DISABLE_AFTER_TIMEOUT
48 #define RGB_DISABLE_AFTER_TIMEOUT 0
51 #ifndef RGB_DISABLE_WHEN_USB_SUSPENDED
52 #define RGB_DISABLE_WHEN_USB_SUSPENDED false
55 #ifndef EECONFIG_RGB_MATRIX
56 #define EECONFIG_RGB_MATRIX EECONFIG_RGBLIGHT
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
64 #if !defined(RGB_MATRIX_HUE_STEP)
65 #define RGB_MATRIX_HUE_STEP 8
68 #if !defined(RGB_MATRIX_SAT_STEP)
69 #define RGB_MATRIX_SAT_STEP 16
72 #if !defined(RGB_MATRIX_VAL_STEP)
73 #define RGB_MATRIX_VAL_STEP 16
76 #if !defined(RGB_MATRIX_SPD_STEP)
77 #define RGB_MATRIX_SPD_STEP 16
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
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
89 bool g_suspend_state = false;
91 rgb_config_t rgb_matrix_config;
93 rgb_counters_t g_rgb_counters;
94 static uint32_t rgb_counters_buffer;
96 #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
97 uint8_t rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}};
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
105 uint32_t eeconfig_read_rgb_matrix(void) {
106 return eeprom_read_dword(EECONFIG_RGB_MATRIX);
109 void eeconfig_update_rgb_matrix(uint32_t val) {
110 eeprom_update_dword(EECONFIG_RGB_MATRIX, val);
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);
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);
134 __attribute__ ((weak))
135 uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) {
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;
149 void rgb_matrix_update_pwm_buffers(void) {
150 rgb_matrix_driver.flush();
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);
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);
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;
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;
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;
176 #endif // defined(RGB_MATRIX_KEYRELEASES)
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--;
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++;
194 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
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);
200 #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP)
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)
209 switch ( (g_rgb_counters.tick & (0b11 << factor)) >> factor )
212 rgb_matrix_set_color_all( 20, 0, 0 );
216 rgb_matrix_set_color_all( 0, 20, 0 );
220 rgb_matrix_set_color_all( 0, 0, 20 );
224 rgb_matrix_set_color_all( 20, 20, 20 );
230 static bool rgb_matrix_none(effect_params_t* params) {
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);
239 return led_max < DRIVER_LED_TOTAL;
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;
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;
255 g_rgb_counters.any_key_hit += deltaTime;
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--;
267 last_hit_buffer.tick[i] += deltaTime;
269 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
272 static void rgb_task_sync(void) {
274 if (timer_elapsed32(g_rgb_counters.tick) >= RGB_MATRIX_LED_FLUSH_LIMIT)
275 rgb_task_state = STARTING;
278 static void rgb_task_start(void) {
280 rgb_effect_params.iter = 0;
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
289 rgb_task_state = RENDERING;
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);
296 // each effect can opt to do calculations
297 // and/or request PWM buffer updates.
299 case RGB_MATRIX_NONE:
300 rendering = rgb_matrix_none(&rgb_effect_params);
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); \
309 #include "rgb_matrix_animations/rgb_matrix_effects.inc"
310 #undef RGB_MATRIX_EFFECT
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); \
317 #ifdef RGB_MATRIX_CUSTOM_KB
318 #include "rgb_matrix_kb.inc"
320 #ifdef RGB_MATRIX_CUSTOM_USER
321 #include "rgb_matrix_user.inc"
323 #undef RGB_MATRIX_EFFECT
325 // -----End rgb effect switch case macros-------
326 // ---------------------------------------------
328 // Factory default magic value
331 rgb_task_state = FLUSHING;
336 rgb_effect_params.iter++;
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;
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;
353 // update pwm buffers
354 rgb_matrix_update_pwm_buffers();
357 rgb_task_state = SYNCING;
360 void rgb_matrix_task(void) {
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;
368 switch (rgb_task_state) {
373 rgb_task_render(effect);
376 rgb_task_flush(effect);
383 if (!suspend_backlight) {
384 rgb_matrix_indicators();
388 void rgb_matrix_indicators(void) {
389 rgb_matrix_indicators_kb();
390 rgb_matrix_indicators_user();
393 __attribute__((weak))
394 void rgb_matrix_indicators_kb(void) {}
396 __attribute__((weak))
397 void rgb_matrix_indicators_user(void) {}
399 void rgb_matrix_init(void) {
400 rgb_matrix_driver.init();
402 // TODO: put the 1 second startup delay here?
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;
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;
414 #endif // RGB_MATRIX_KEYREACTIVE_ENABLED
416 if (!eeconfig_is_enabled()) {
417 dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n");
419 eeconfig_update_rgb_matrix_default();
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();
429 eeconfig_debug_rgb_matrix(); // display current eeprom values
432 void rgb_matrix_set_suspend_state(bool state) {
433 g_suspend_state = state;
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);
442 void rgb_matrix_enable(void) {
443 rgb_matrix_enable_noeeprom();
444 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
447 void rgb_matrix_enable_noeeprom(void) {
448 if (!rgb_matrix_config.enable)
449 rgb_task_state = STARTING;
450 rgb_matrix_config.enable = 1;
453 void rgb_matrix_disable(void) {
454 rgb_matrix_disable_noeeprom();
455 eeconfig_update_rgb_matrix(rgb_matrix_config.raw);
458 void rgb_matrix_disable_noeeprom(void) {
459 if (rgb_matrix_config.enable)
460 rgb_task_state = STARTING;
461 rgb_matrix_config.enable = 0;
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);
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);
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);
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);
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);
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);
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);
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);
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
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
522 led_flags_t rgb_matrix_get_flags(void) {
523 return rgb_effect_params.flags;
526 void rgb_matrix_set_flags(led_flags_t flags) {
527 rgb_effect_params.flags = flags;
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);
536 void rgb_matrix_mode_noeeprom(uint8_t mode) {
537 rgb_matrix_config.mode = mode;
540 uint8_t rgb_matrix_get_mode(void) {
541 return rgb_matrix_config.mode;
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);
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;