diff options
author | Ryan <fauxpark@gmail.com> | 2021-06-22 18:26:23 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-22 18:26:23 +1000 |
commit | d61e5c0027e289ccf48652afa4c442342e7ccf04 (patch) | |
tree | e8edc86d191a190691b4722f05e32bdc40affc58 /quantum/rgb_matrix/rgb_matrix.c | |
parent | c03cb4edd7ff73391a0a64f0c4f5e0755e902908 (diff) | |
download | qmk_firmware-d61e5c0027e289ccf48652afa4c442342e7ccf04.tar.gz qmk_firmware-d61e5c0027e289ccf48652afa4c442342e7ccf04.zip |
Move LED/RGB Matrix code into their own directories (#13257)
Diffstat (limited to 'quantum/rgb_matrix/rgb_matrix.c')
-rw-r--r-- | quantum/rgb_matrix/rgb_matrix.c | 640 |
1 files changed, 640 insertions, 0 deletions
diff --git a/quantum/rgb_matrix/rgb_matrix.c b/quantum/rgb_matrix/rgb_matrix.c new file mode 100644 index 000000000..789cd2860 --- /dev/null +++ b/quantum/rgb_matrix/rgb_matrix.c | |||
@@ -0,0 +1,640 @@ | |||
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 led_point_t k_rgb_matrix_center = {112, 32}; | ||
30 | #else | ||
31 | const led_point_t k_rgb_matrix_center = RGB_MATRIX_CENTER; | ||
32 | #endif | ||
33 | |||
34 | // clang-format off | ||
35 | #ifndef RGB_MATRIX_IMMEDIATE_EEPROM | ||
36 | # define rgb_eeconfig_update(v) rgb_update_eeprom |= v | ||
37 | #else | ||
38 | # define rgb_eeconfig_update(v) if (v) eeconfig_update_rgb_matrix() | ||
39 | #endif | ||
40 | // clang-format on | ||
41 | |||
42 | __attribute__((weak)) RGB rgb_matrix_hsv_to_rgb(HSV hsv) { return hsv_to_rgb(hsv); } | ||
43 | |||
44 | // Generic effect runners | ||
45 | #include "rgb_matrix_runners.inc" | ||
46 | |||
47 | // ------------------------------------------ | ||
48 | // -----Begin rgb effect includes macros----- | ||
49 | #define RGB_MATRIX_EFFECT(name) | ||
50 | #define RGB_MATRIX_CUSTOM_EFFECT_IMPLS | ||
51 | |||
52 | #include "rgb_matrix_effects.inc" | ||
53 | #ifdef RGB_MATRIX_CUSTOM_KB | ||
54 | # include "rgb_matrix_kb.inc" | ||
55 | #endif | ||
56 | #ifdef RGB_MATRIX_CUSTOM_USER | ||
57 | # include "rgb_matrix_user.inc" | ||
58 | #endif | ||
59 | |||
60 | #undef RGB_MATRIX_CUSTOM_EFFECT_IMPLS | ||
61 | #undef RGB_MATRIX_EFFECT | ||
62 | // -----End rgb effect includes macros------- | ||
63 | // ------------------------------------------ | ||
64 | |||
65 | #if defined(RGB_DISABLE_AFTER_TIMEOUT) && !defined(RGB_DISABLE_TIMEOUT) | ||
66 | # define RGB_DISABLE_TIMEOUT (RGB_DISABLE_AFTER_TIMEOUT * 1200UL) | ||
67 | #endif | ||
68 | |||
69 | #ifndef RGB_DISABLE_TIMEOUT | ||
70 | # define RGB_DISABLE_TIMEOUT 0 | ||
71 | #endif | ||
72 | |||
73 | #if !defined(RGB_MATRIX_MAXIMUM_BRIGHTNESS) || RGB_MATRIX_MAXIMUM_BRIGHTNESS > UINT8_MAX | ||
74 | # undef RGB_MATRIX_MAXIMUM_BRIGHTNESS | ||
75 | # define RGB_MATRIX_MAXIMUM_BRIGHTNESS UINT8_MAX | ||
76 | #endif | ||
77 | |||
78 | #if !defined(RGB_MATRIX_HUE_STEP) | ||
79 | # define RGB_MATRIX_HUE_STEP 8 | ||
80 | #endif | ||
81 | |||
82 | #if !defined(RGB_MATRIX_SAT_STEP) | ||
83 | # define RGB_MATRIX_SAT_STEP 16 | ||
84 | #endif | ||
85 | |||
86 | #if !defined(RGB_MATRIX_VAL_STEP) | ||
87 | # define RGB_MATRIX_VAL_STEP 16 | ||
88 | #endif | ||
89 | |||
90 | #if !defined(RGB_MATRIX_SPD_STEP) | ||
91 | # define RGB_MATRIX_SPD_STEP 16 | ||
92 | #endif | ||
93 | |||
94 | #if !defined(RGB_MATRIX_STARTUP_MODE) | ||
95 | # ifndef DISABLE_RGB_MATRIX_CYCLE_LEFT_RIGHT | ||
96 | # define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT | ||
97 | # else | ||
98 | // fallback to solid colors if RGB_MATRIX_CYCLE_LEFT_RIGHT is disabled in userspace | ||
99 | # define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_SOLID_COLOR | ||
100 | # endif | ||
101 | #endif | ||
102 | |||
103 | #if !defined(RGB_MATRIX_STARTUP_HUE) | ||
104 | # define RGB_MATRIX_STARTUP_HUE 0 | ||
105 | #endif | ||
106 | |||
107 | #if !defined(RGB_MATRIX_STARTUP_SAT) | ||
108 | # define RGB_MATRIX_STARTUP_SAT UINT8_MAX | ||
109 | #endif | ||
110 | |||
111 | #if !defined(RGB_MATRIX_STARTUP_VAL) | ||
112 | # define RGB_MATRIX_STARTUP_VAL RGB_MATRIX_MAXIMUM_BRIGHTNESS | ||
113 | #endif | ||
114 | |||
115 | #if !defined(RGB_MATRIX_STARTUP_SPD) | ||
116 | # define RGB_MATRIX_STARTUP_SPD UINT8_MAX / 2 | ||
117 | #endif | ||
118 | |||
119 | // globals | ||
120 | rgb_config_t rgb_matrix_config; // TODO: would like to prefix this with g_ for global consistancy, do this in another pr | ||
121 | uint32_t g_rgb_timer; | ||
122 | #ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS | ||
123 | uint8_t g_rgb_frame_buffer[MATRIX_ROWS][MATRIX_COLS] = {{0}}; | ||
124 | #endif // RGB_MATRIX_FRAMEBUFFER_EFFECTS | ||
125 | #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED | ||
126 | last_hit_t g_last_hit_tracker; | ||
127 | #endif // RGB_MATRIX_KEYREACTIVE_ENABLED | ||
128 | |||
129 | // internals | ||
130 | static bool suspend_state = false; | ||
131 | static bool rgb_update_eeprom = false; | ||
132 | static uint8_t rgb_last_enable = UINT8_MAX; | ||
133 | static uint8_t rgb_last_effect = UINT8_MAX; | ||
134 | static effect_params_t rgb_effect_params = {0, LED_FLAG_ALL, false}; | ||
135 | static rgb_task_states rgb_task_state = SYNCING; | ||
136 | #if RGB_DISABLE_TIMEOUT > 0 | ||
137 | static uint32_t rgb_anykey_timer; | ||
138 | #endif // RGB_DISABLE_TIMEOUT > 0 | ||
139 | |||
140 | // double buffers | ||
141 | static uint32_t rgb_timer_buffer; | ||
142 | #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED | ||
143 | static last_hit_t last_hit_buffer; | ||
144 | #endif // RGB_MATRIX_KEYREACTIVE_ENABLED | ||
145 | |||
146 | // split rgb matrix | ||
147 | #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT) | ||
148 | const uint8_t k_rgb_matrix_split[2] = RGB_MATRIX_SPLIT; | ||
149 | #endif | ||
150 | |||
151 | void eeconfig_read_rgb_matrix(void) { eeprom_read_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); } | ||
152 | |||
153 | void eeconfig_update_rgb_matrix(void) { eeprom_update_block(&rgb_matrix_config, EECONFIG_RGB_MATRIX, sizeof(rgb_matrix_config)); } | ||
154 | |||
155 | void eeconfig_update_rgb_matrix_default(void) { | ||
156 | dprintf("eeconfig_update_rgb_matrix_default\n"); | ||
157 | rgb_matrix_config.enable = 1; | ||
158 | rgb_matrix_config.mode = RGB_MATRIX_STARTUP_MODE; | ||
159 | rgb_matrix_config.hsv = (HSV){RGB_MATRIX_STARTUP_HUE, RGB_MATRIX_STARTUP_SAT, RGB_MATRIX_STARTUP_VAL}; | ||
160 | rgb_matrix_config.speed = RGB_MATRIX_STARTUP_SPD; | ||
161 | rgb_matrix_config.flags = LED_FLAG_ALL; | ||
162 | eeconfig_update_rgb_matrix(); | ||
163 | } | ||
164 | |||
165 | void eeconfig_debug_rgb_matrix(void) { | ||
166 | dprintf("rgb_matrix_config EEPROM\n"); | ||
167 | dprintf("rgb_matrix_config.enable = %d\n", rgb_matrix_config.enable); | ||
168 | dprintf("rgb_matrix_config.mode = %d\n", rgb_matrix_config.mode); | ||
169 | dprintf("rgb_matrix_config.hsv.h = %d\n", rgb_matrix_config.hsv.h); | ||
170 | dprintf("rgb_matrix_config.hsv.s = %d\n", rgb_matrix_config.hsv.s); | ||
171 | dprintf("rgb_matrix_config.hsv.v = %d\n", rgb_matrix_config.hsv.v); | ||
172 | dprintf("rgb_matrix_config.speed = %d\n", rgb_matrix_config.speed); | ||
173 | dprintf("rgb_matrix_config.flags = %d\n", rgb_matrix_config.flags); | ||
174 | } | ||
175 | |||
176 | __attribute__((weak)) uint8_t rgb_matrix_map_row_column_to_led_kb(uint8_t row, uint8_t column, uint8_t *led_i) { return 0; } | ||
177 | |||
178 | uint8_t rgb_matrix_map_row_column_to_led(uint8_t row, uint8_t column, uint8_t *led_i) { | ||
179 | uint8_t led_count = rgb_matrix_map_row_column_to_led_kb(row, column, led_i); | ||
180 | uint8_t led_index = g_led_config.matrix_co[row][column]; | ||
181 | if (led_index != NO_LED) { | ||
182 | led_i[led_count] = led_index; | ||
183 | led_count++; | ||
184 | } | ||
185 | return led_count; | ||
186 | } | ||
187 | |||
188 | void rgb_matrix_update_pwm_buffers(void) { rgb_matrix_driver.flush(); } | ||
189 | |||
190 | void rgb_matrix_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) { | ||
191 | #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT) | ||
192 | if (!is_keyboard_left() && index >= k_rgb_matrix_split[0]) | ||
193 | rgb_matrix_driver.set_color(index - k_rgb_matrix_split[0], red, green, blue); | ||
194 | else if (is_keyboard_left() && index < k_rgb_matrix_split[0]) | ||
195 | #endif | ||
196 | rgb_matrix_driver.set_color(index, red, green, blue); | ||
197 | } | ||
198 | |||
199 | void rgb_matrix_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { | ||
200 | #if defined(RGB_MATRIX_ENABLE) && defined(RGB_MATRIX_SPLIT) | ||
201 | for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) rgb_matrix_set_color(i, red, green, blue); | ||
202 | #else | ||
203 | rgb_matrix_driver.set_color_all(red, green, blue); | ||
204 | #endif | ||
205 | } | ||
206 | |||
207 | void process_rgb_matrix(uint8_t row, uint8_t col, bool pressed) { | ||
208 | #ifndef RGB_MATRIX_SPLIT | ||
209 | if (!is_keyboard_master()) return; | ||
210 | #endif | ||
211 | #if RGB_DISABLE_TIMEOUT > 0 | ||
212 | rgb_anykey_timer = 0; | ||
213 | #endif // RGB_DISABLE_TIMEOUT > 0 | ||
214 | |||
215 | #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED | ||
216 | uint8_t led[LED_HITS_TO_REMEMBER]; | ||
217 | uint8_t led_count = 0; | ||
218 | |||
219 | # if defined(RGB_MATRIX_KEYRELEASES) | ||
220 | if (!pressed) | ||
221 | # elif defined(RGB_MATRIX_KEYPRESSES) | ||
222 | if (pressed) | ||
223 | # endif // defined(RGB_MATRIX_KEYRELEASES) | ||
224 | { | ||
225 | led_count = rgb_matrix_map_row_column_to_led(row, col, led); | ||
226 | } | ||
227 | |||
228 | if (last_hit_buffer.count + led_count > LED_HITS_TO_REMEMBER) { | ||
229 | memcpy(&last_hit_buffer.x[0], &last_hit_buffer.x[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
230 | memcpy(&last_hit_buffer.y[0], &last_hit_buffer.y[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
231 | memcpy(&last_hit_buffer.tick[0], &last_hit_buffer.tick[led_count], (LED_HITS_TO_REMEMBER - led_count) * 2); // 16 bit | ||
232 | memcpy(&last_hit_buffer.index[0], &last_hit_buffer.index[led_count], LED_HITS_TO_REMEMBER - led_count); | ||
233 | last_hit_buffer.count--; | ||
234 | } | ||
235 | |||
236 | for (uint8_t i = 0; i < led_count; i++) { | ||
237 | uint8_t index = last_hit_buffer.count; | ||
238 | last_hit_buffer.x[index] = g_led_config.point[led[i]].x; | ||
239 | last_hit_buffer.y[index] = g_led_config.point[led[i]].y; | ||
240 | last_hit_buffer.index[index] = led[i]; | ||
241 | last_hit_buffer.tick[index] = 0; | ||
242 | last_hit_buffer.count++; | ||
243 | } | ||
244 | #endif // RGB_MATRIX_KEYREACTIVE_ENABLED | ||
245 | |||
246 | #if defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP) | ||
247 | if (rgb_matrix_config.mode == RGB_MATRIX_TYPING_HEATMAP) { | ||
248 | process_rgb_matrix_typing_heatmap(row, col); | ||
249 | } | ||
250 | #endif // defined(RGB_MATRIX_FRAMEBUFFER_EFFECTS) && !defined(DISABLE_RGB_MATRIX_TYPING_HEATMAP) | ||
251 | } | ||
252 | |||
253 | void rgb_matrix_test(void) { | ||
254 | // Mask out bits 4 and 5 | ||
255 | // Increase the factor to make the test animation slower (and reduce to make it faster) | ||
256 | uint8_t factor = 10; | ||
257 | switch ((g_rgb_timer & (0b11 << factor)) >> factor) { | ||
258 | case 0: { | ||
259 | rgb_matrix_set_color_all(20, 0, 0); | ||
260 | break; | ||
261 | } | ||
262 | case 1: { | ||
263 | rgb_matrix_set_color_all(0, 20, 0); | ||
264 | break; | ||
265 | } | ||
266 | case 2: { | ||
267 | rgb_matrix_set_color_all(0, 0, 20); | ||
268 | break; | ||
269 | } | ||
270 | case 3: { | ||
271 | rgb_matrix_set_color_all(20, 20, 20); | ||
272 | break; | ||
273 | } | ||
274 | } | ||
275 | } | ||
276 | |||
277 | static bool rgb_matrix_none(effect_params_t *params) { | ||
278 | if (!params->init) { | ||
279 | return false; | ||
280 | } | ||
281 | |||
282 | rgb_matrix_set_color_all(0, 0, 0); | ||
283 | return false; | ||
284 | } | ||
285 | |||
286 | static void rgb_task_timers(void) { | ||
287 | #if defined(RGB_MATRIX_KEYREACTIVE_ENABLED) || RGB_DISABLE_TIMEOUT > 0 | ||
288 | uint32_t deltaTime = sync_timer_elapsed32(rgb_timer_buffer); | ||
289 | #endif // defined(RGB_MATRIX_KEYREACTIVE_ENABLED) || RGB_DISABLE_TIMEOUT > 0 | ||
290 | rgb_timer_buffer = sync_timer_read32(); | ||
291 | |||
292 | // Update double buffer timers | ||
293 | #if RGB_DISABLE_TIMEOUT > 0 | ||
294 | if (rgb_anykey_timer < UINT32_MAX) { | ||
295 | if (UINT32_MAX - deltaTime < rgb_anykey_timer) { | ||
296 | rgb_anykey_timer = UINT32_MAX; | ||
297 | } else { | ||
298 | rgb_anykey_timer += deltaTime; | ||
299 | } | ||
300 | } | ||
301 | #endif // RGB_DISABLE_TIMEOUT > 0 | ||
302 | |||
303 | // Update double buffer last hit timers | ||
304 | #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED | ||
305 | uint8_t count = last_hit_buffer.count; | ||
306 | for (uint8_t i = 0; i < count; ++i) { | ||
307 | if (UINT16_MAX - deltaTime < last_hit_buffer.tick[i]) { | ||
308 | last_hit_buffer.count--; | ||
309 | continue; | ||
310 | } | ||
311 | last_hit_buffer.tick[i] += deltaTime; | ||
312 | } | ||
313 | #endif // RGB_MATRIX_KEYREACTIVE_ENABLED | ||
314 | } | ||
315 | |||
316 | static void rgb_task_sync(void) { | ||
317 | // next task | ||
318 | if (rgb_update_eeprom) eeconfig_update_rgb_matrix(); | ||
319 | rgb_update_eeprom = false; | ||
320 | if (sync_timer_elapsed32(g_rgb_timer) >= RGB_MATRIX_LED_FLUSH_LIMIT) rgb_task_state = STARTING; | ||
321 | } | ||
322 | |||
323 | static void rgb_task_start(void) { | ||
324 | // reset iter | ||
325 | rgb_effect_params.iter = 0; | ||
326 | |||
327 | // update double buffers | ||
328 | g_rgb_timer = rgb_timer_buffer; | ||
329 | #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED | ||
330 | g_last_hit_tracker = last_hit_buffer; | ||
331 | #endif // RGB_MATRIX_KEYREACTIVE_ENABLED | ||
332 | |||
333 | // next task | ||
334 | rgb_task_state = RENDERING; | ||
335 | } | ||
336 | |||
337 | static void rgb_task_render(uint8_t effect) { | ||
338 | bool rendering = false; | ||
339 | rgb_effect_params.init = (effect != rgb_last_effect) || (rgb_matrix_config.enable != rgb_last_enable); | ||
340 | if (rgb_effect_params.flags != rgb_matrix_config.flags) { | ||
341 | rgb_effect_params.flags = rgb_matrix_config.flags; | ||
342 | rgb_matrix_set_color_all(0, 0, 0); | ||
343 | } | ||
344 | |||
345 | // each effect can opt to do calculations | ||
346 | // and/or request PWM buffer updates. | ||
347 | switch (effect) { | ||
348 | case RGB_MATRIX_NONE: | ||
349 | rendering = rgb_matrix_none(&rgb_effect_params); | ||
350 | break; | ||
351 | |||
352 | // --------------------------------------------- | ||
353 | // -----Begin rgb effect switch case macros----- | ||
354 | #define RGB_MATRIX_EFFECT(name, ...) \ | ||
355 | case RGB_MATRIX_##name: \ | ||
356 | rendering = name(&rgb_effect_params); \ | ||
357 | break; | ||
358 | #include "rgb_matrix_effects.inc" | ||
359 | #undef RGB_MATRIX_EFFECT | ||
360 | |||
361 | #if defined(RGB_MATRIX_CUSTOM_KB) || defined(RGB_MATRIX_CUSTOM_USER) | ||
362 | # define RGB_MATRIX_EFFECT(name, ...) \ | ||
363 | case RGB_MATRIX_CUSTOM_##name: \ | ||
364 | rendering = name(&rgb_effect_params); \ | ||
365 | break; | ||
366 | # ifdef RGB_MATRIX_CUSTOM_KB | ||
367 | # include "rgb_matrix_kb.inc" | ||
368 | # endif | ||
369 | # ifdef RGB_MATRIX_CUSTOM_USER | ||
370 | # include "rgb_matrix_user.inc" | ||
371 | # endif | ||
372 | # undef RGB_MATRIX_EFFECT | ||
373 | #endif | ||
374 | // -----End rgb effect switch case macros------- | ||
375 | // --------------------------------------------- | ||
376 | |||
377 | // Factory default magic value | ||
378 | case UINT8_MAX: { | ||
379 | rgb_matrix_test(); | ||
380 | rgb_task_state = FLUSHING; | ||
381 | } | ||
382 | return; | ||
383 | } | ||
384 | |||
385 | rgb_effect_params.iter++; | ||
386 | |||
387 | // next task | ||
388 | if (!rendering) { | ||
389 | rgb_task_state = FLUSHING; | ||
390 | if (!rgb_effect_params.init && effect == RGB_MATRIX_NONE) { | ||
391 | // We only need to flush once if we are RGB_MATRIX_NONE | ||
392 | rgb_task_state = SYNCING; | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | static void rgb_task_flush(uint8_t effect) { | ||
398 | // update last trackers after the first full render so we can init over several frames | ||
399 | rgb_last_effect = effect; | ||
400 | rgb_last_enable = rgb_matrix_config.enable; | ||
401 | |||
402 | // update pwm buffers | ||
403 | rgb_matrix_update_pwm_buffers(); | ||
404 | |||
405 | // next task | ||
406 | rgb_task_state = SYNCING; | ||
407 | } | ||
408 | |||
409 | void rgb_matrix_task(void) { | ||
410 | rgb_task_timers(); | ||
411 | |||
412 | // Ideally we would also stop sending zeros to the LED driver PWM buffers | ||
413 | // while suspended and just do a software shutdown. This is a cheap hack for now. | ||
414 | bool suspend_backlight = suspend_state || | ||
415 | #if RGB_DISABLE_TIMEOUT > 0 | ||
416 | (rgb_anykey_timer > (uint32_t)RGB_DISABLE_TIMEOUT) || | ||
417 | #endif // RGB_DISABLE_TIMEOUT > 0 | ||
418 | false; | ||
419 | |||
420 | uint8_t effect = suspend_backlight || !rgb_matrix_config.enable ? 0 : rgb_matrix_config.mode; | ||
421 | |||
422 | switch (rgb_task_state) { | ||
423 | case STARTING: | ||
424 | rgb_task_start(); | ||
425 | break; | ||
426 | case RENDERING: | ||
427 | rgb_task_render(effect); | ||
428 | if (effect) { | ||
429 | rgb_matrix_indicators(); | ||
430 | rgb_matrix_indicators_advanced(&rgb_effect_params); | ||
431 | } | ||
432 | break; | ||
433 | case FLUSHING: | ||
434 | rgb_task_flush(effect); | ||
435 | break; | ||
436 | case SYNCING: | ||
437 | rgb_task_sync(); | ||
438 | break; | ||
439 | } | ||
440 | } | ||
441 | |||
442 | void rgb_matrix_indicators(void) { | ||
443 | rgb_matrix_indicators_kb(); | ||
444 | rgb_matrix_indicators_user(); | ||
445 | } | ||
446 | |||
447 | __attribute__((weak)) void rgb_matrix_indicators_kb(void) {} | ||
448 | |||
449 | __attribute__((weak)) void rgb_matrix_indicators_user(void) {} | ||
450 | |||
451 | void rgb_matrix_indicators_advanced(effect_params_t *params) { | ||
452 | /* special handling is needed for "params->iter", since it's already been incremented. | ||
453 | * Could move the invocations to rgb_task_render, but then it's missing a few checks | ||
454 | * and not sure which would be better. Otherwise, this should be called from | ||
455 | * rgb_task_render, right before the iter++ line. | ||
456 | */ | ||
457 | #if defined(RGB_MATRIX_LED_PROCESS_LIMIT) && RGB_MATRIX_LED_PROCESS_LIMIT > 0 && RGB_MATRIX_LED_PROCESS_LIMIT < DRIVER_LED_TOTAL | ||
458 | uint8_t min = RGB_MATRIX_LED_PROCESS_LIMIT * (params->iter - 1); | ||
459 | uint8_t max = min + RGB_MATRIX_LED_PROCESS_LIMIT; | ||
460 | if (max > DRIVER_LED_TOTAL) max = DRIVER_LED_TOTAL; | ||
461 | #else | ||
462 | uint8_t min = 0; | ||
463 | uint8_t max = DRIVER_LED_TOTAL; | ||
464 | #endif | ||
465 | rgb_matrix_indicators_advanced_kb(min, max); | ||
466 | rgb_matrix_indicators_advanced_user(min, max); | ||
467 | } | ||
468 | |||
469 | __attribute__((weak)) void rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {} | ||
470 | |||
471 | __attribute__((weak)) void rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {} | ||
472 | |||
473 | void rgb_matrix_init(void) { | ||
474 | rgb_matrix_driver.init(); | ||
475 | |||
476 | #ifdef RGB_MATRIX_KEYREACTIVE_ENABLED | ||
477 | g_last_hit_tracker.count = 0; | ||
478 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { | ||
479 | g_last_hit_tracker.tick[i] = UINT16_MAX; | ||
480 | } | ||
481 | |||
482 | last_hit_buffer.count = 0; | ||
483 | for (uint8_t i = 0; i < LED_HITS_TO_REMEMBER; ++i) { | ||
484 | last_hit_buffer.tick[i] = UINT16_MAX; | ||
485 | } | ||
486 | #endif // RGB_MATRIX_KEYREACTIVE_ENABLED | ||
487 | |||
488 | if (!eeconfig_is_enabled()) { | ||
489 | dprintf("rgb_matrix_init_drivers eeconfig is not enabled.\n"); | ||
490 | eeconfig_init(); | ||
491 | eeconfig_update_rgb_matrix_default(); | ||
492 | } | ||
493 | |||
494 | eeconfig_read_rgb_matrix(); | ||
495 | if (!rgb_matrix_config.mode) { | ||
496 | dprintf("rgb_matrix_init_drivers rgb_matrix_config.mode = 0. Write default values to EEPROM.\n"); | ||
497 | eeconfig_update_rgb_matrix_default(); | ||
498 | } | ||
499 | eeconfig_debug_rgb_matrix(); // display current eeprom values | ||
500 | } | ||
501 | |||
502 | void rgb_matrix_set_suspend_state(bool state) { | ||
503 | #ifdef RGB_DISABLE_WHEN_USB_SUSPENDED | ||
504 | if (state) { | ||
505 | rgb_matrix_set_color_all(0, 0, 0); // turn off all LEDs when suspending | ||
506 | } | ||
507 | suspend_state = state; | ||
508 | #endif | ||
509 | } | ||
510 | |||
511 | bool rgb_matrix_get_suspend_state(void) { return suspend_state; } | ||
512 | |||
513 | void rgb_matrix_toggle_eeprom_helper(bool write_to_eeprom) { | ||
514 | rgb_matrix_config.enable ^= 1; | ||
515 | rgb_task_state = STARTING; | ||
516 | rgb_eeconfig_update(write_to_eeprom); | ||
517 | dprintf("rgb matrix toggle [%s]: rgb_matrix_config.enable = %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.enable); | ||
518 | } | ||
519 | void rgb_matrix_toggle_noeeprom(void) { rgb_matrix_toggle_eeprom_helper(false); } | ||
520 | void rgb_matrix_toggle(void) { rgb_matrix_toggle_eeprom_helper(true); } | ||
521 | |||
522 | void rgb_matrix_enable(void) { | ||
523 | rgb_matrix_enable_noeeprom(); | ||
524 | rgb_eeconfig_update(true); | ||
525 | } | ||
526 | |||
527 | void rgb_matrix_enable_noeeprom(void) { | ||
528 | if (!rgb_matrix_config.enable) rgb_task_state = STARTING; | ||
529 | rgb_matrix_config.enable = 1; | ||
530 | } | ||
531 | |||
532 | void rgb_matrix_disable(void) { | ||
533 | rgb_matrix_disable_noeeprom(); | ||
534 | rgb_eeconfig_update(true); | ||
535 | } | ||
536 | |||
537 | void rgb_matrix_disable_noeeprom(void) { | ||
538 | if (rgb_matrix_config.enable) rgb_task_state = STARTING; | ||
539 | rgb_matrix_config.enable = 0; | ||
540 | } | ||
541 | |||
542 | uint8_t rgb_matrix_is_enabled(void) { return rgb_matrix_config.enable; } | ||
543 | |||
544 | void rgb_matrix_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { | ||
545 | if (!rgb_matrix_config.enable) { | ||
546 | return; | ||
547 | } | ||
548 | if (mode < 1) { | ||
549 | rgb_matrix_config.mode = 1; | ||
550 | } else if (mode >= RGB_MATRIX_EFFECT_MAX) { | ||
551 | rgb_matrix_config.mode = RGB_MATRIX_EFFECT_MAX - 1; | ||
552 | } else { | ||
553 | rgb_matrix_config.mode = mode; | ||
554 | } | ||
555 | rgb_task_state = STARTING; | ||
556 | rgb_eeconfig_update(write_to_eeprom); | ||
557 | dprintf("rgb matrix mode [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.mode); | ||
558 | } | ||
559 | void rgb_matrix_mode_noeeprom(uint8_t mode) { rgb_matrix_mode_eeprom_helper(mode, false); } | ||
560 | void rgb_matrix_mode(uint8_t mode) { rgb_matrix_mode_eeprom_helper(mode, true); } | ||
561 | |||
562 | uint8_t rgb_matrix_get_mode(void) { return rgb_matrix_config.mode; } | ||
563 | |||
564 | void rgb_matrix_step_helper(bool write_to_eeprom) { | ||
565 | uint8_t mode = rgb_matrix_config.mode + 1; | ||
566 | rgb_matrix_mode_eeprom_helper((mode < RGB_MATRIX_EFFECT_MAX) ? mode : 1, write_to_eeprom); | ||
567 | } | ||
568 | void rgb_matrix_step_noeeprom(void) { rgb_matrix_step_helper(false); } | ||
569 | void rgb_matrix_step(void) { rgb_matrix_step_helper(true); } | ||
570 | |||
571 | void rgb_matrix_step_reverse_helper(bool write_to_eeprom) { | ||
572 | uint8_t mode = rgb_matrix_config.mode - 1; | ||
573 | rgb_matrix_mode_eeprom_helper((mode < 1) ? RGB_MATRIX_EFFECT_MAX - 1 : mode, write_to_eeprom); | ||
574 | } | ||
575 | void rgb_matrix_step_reverse_noeeprom(void) { rgb_matrix_step_reverse_helper(false); } | ||
576 | void rgb_matrix_step_reverse(void) { rgb_matrix_step_reverse_helper(true); } | ||
577 | |||
578 | void rgb_matrix_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) { | ||
579 | if (!rgb_matrix_config.enable) { | ||
580 | return; | ||
581 | } | ||
582 | rgb_matrix_config.hsv.h = hue; | ||
583 | rgb_matrix_config.hsv.s = sat; | ||
584 | rgb_matrix_config.hsv.v = (val > RGB_MATRIX_MAXIMUM_BRIGHTNESS) ? RGB_MATRIX_MAXIMUM_BRIGHTNESS : val; | ||
585 | rgb_eeconfig_update(write_to_eeprom); | ||
586 | dprintf("rgb matrix set hsv [%s]: %u,%u,%u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v); | ||
587 | } | ||
588 | void rgb_matrix_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { rgb_matrix_sethsv_eeprom_helper(hue, sat, val, false); } | ||
589 | void rgb_matrix_sethsv(uint16_t hue, uint8_t sat, uint8_t val) { rgb_matrix_sethsv_eeprom_helper(hue, sat, val, true); } | ||
590 | |||
591 | HSV rgb_matrix_get_hsv(void) { return rgb_matrix_config.hsv; } | ||
592 | uint8_t rgb_matrix_get_hue(void) { return rgb_matrix_config.hsv.h; } | ||
593 | uint8_t rgb_matrix_get_sat(void) { return rgb_matrix_config.hsv.s; } | ||
594 | uint8_t rgb_matrix_get_val(void) { return rgb_matrix_config.hsv.v; } | ||
595 | |||
596 | void rgb_matrix_increase_hue_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h + RGB_MATRIX_HUE_STEP, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v, write_to_eeprom); } | ||
597 | void rgb_matrix_increase_hue_noeeprom(void) { rgb_matrix_increase_hue_helper(false); } | ||
598 | void rgb_matrix_increase_hue(void) { rgb_matrix_increase_hue_helper(true); } | ||
599 | |||
600 | void rgb_matrix_decrease_hue_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h - RGB_MATRIX_HUE_STEP, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v, write_to_eeprom); } | ||
601 | void rgb_matrix_decrease_hue_noeeprom(void) { rgb_matrix_decrease_hue_helper(false); } | ||
602 | void rgb_matrix_decrease_hue(void) { rgb_matrix_decrease_hue_helper(true); } | ||
603 | |||
604 | void rgb_matrix_increase_sat_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, qadd8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP), rgb_matrix_config.hsv.v, write_to_eeprom); } | ||
605 | void rgb_matrix_increase_sat_noeeprom(void) { rgb_matrix_increase_sat_helper(false); } | ||
606 | void rgb_matrix_increase_sat(void) { rgb_matrix_increase_sat_helper(true); } | ||
607 | |||
608 | void rgb_matrix_decrease_sat_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, qsub8(rgb_matrix_config.hsv.s, RGB_MATRIX_SAT_STEP), rgb_matrix_config.hsv.v, write_to_eeprom); } | ||
609 | void rgb_matrix_decrease_sat_noeeprom(void) { rgb_matrix_decrease_sat_helper(false); } | ||
610 | void rgb_matrix_decrease_sat(void) { rgb_matrix_decrease_sat_helper(true); } | ||
611 | |||
612 | void rgb_matrix_increase_val_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qadd8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom); } | ||
613 | void rgb_matrix_increase_val_noeeprom(void) { rgb_matrix_increase_val_helper(false); } | ||
614 | void rgb_matrix_increase_val(void) { rgb_matrix_increase_val_helper(true); } | ||
615 | |||
616 | void rgb_matrix_decrease_val_helper(bool write_to_eeprom) { rgb_matrix_sethsv_eeprom_helper(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, qsub8(rgb_matrix_config.hsv.v, RGB_MATRIX_VAL_STEP), write_to_eeprom); } | ||
617 | void rgb_matrix_decrease_val_noeeprom(void) { rgb_matrix_decrease_val_helper(false); } | ||
618 | void rgb_matrix_decrease_val(void) { rgb_matrix_decrease_val_helper(true); } | ||
619 | |||
620 | void rgb_matrix_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) { | ||
621 | rgb_matrix_config.speed = speed; | ||
622 | rgb_eeconfig_update(write_to_eeprom); | ||
623 | dprintf("rgb matrix set speed [%s]: %u\n", (write_to_eeprom) ? "EEPROM" : "NOEEPROM", rgb_matrix_config.speed); | ||
624 | } | ||
625 | void rgb_matrix_set_speed_noeeprom(uint8_t speed) { rgb_matrix_set_speed_eeprom_helper(speed, false); } | ||
626 | void rgb_matrix_set_speed(uint8_t speed) { rgb_matrix_set_speed_eeprom_helper(speed, true); } | ||
627 | |||
628 | uint8_t rgb_matrix_get_speed(void) { return rgb_matrix_config.speed; } | ||
629 | |||
630 | void rgb_matrix_increase_speed_helper(bool write_to_eeprom) { rgb_matrix_set_speed_eeprom_helper(qadd8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom); } | ||
631 | void rgb_matrix_increase_speed_noeeprom(void) { rgb_matrix_increase_speed_helper(false); } | ||
632 | void rgb_matrix_increase_speed(void) { rgb_matrix_increase_speed_helper(true); } | ||
633 | |||
634 | void rgb_matrix_decrease_speed_helper(bool write_to_eeprom) { rgb_matrix_set_speed_eeprom_helper(qsub8(rgb_matrix_config.speed, RGB_MATRIX_SPD_STEP), write_to_eeprom); } | ||
635 | void rgb_matrix_decrease_speed_noeeprom(void) { rgb_matrix_decrease_speed_helper(false); } | ||
636 | void rgb_matrix_decrease_speed(void) { rgb_matrix_decrease_speed_helper(true); } | ||
637 | |||
638 | led_flags_t rgb_matrix_get_flags(void) { return rgb_matrix_config.flags; } | ||
639 | |||
640 | void rgb_matrix_set_flags(led_flags_t flags) { rgb_matrix_config.flags = flags; } | ||