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