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