VIA Configurator Refactor (#7268)
[jackhill/qmk/firmware.git] / quantum / rgblight.c
1 /* Copyright 2016-2017 Yang Liu
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include <math.h>
17 #include <string.h>
18 #ifdef __AVR__
19 # include <avr/eeprom.h>
20 # include <avr/interrupt.h>
21 #endif
22 #ifdef STM32_EEPROM_ENABLE
23 # include "hal.h"
24 # include "eeprom.h"
25 # include "eeprom_stm32.h"
26 #endif
27 #include "wait.h"
28 #include "progmem.h"
29 #include "timer.h"
30 #include "rgblight.h"
31 #include "color.h"
32 #include "debug.h"
33 #include "led_tables.h"
34 #include "lib/lib8tion/lib8tion.h"
35 #ifdef VELOCIKEY_ENABLE
36 # include "velocikey.h"
37 #endif
38
39 #ifdef RGBLIGHT_SPLIT
40 /* for split keyboard */
41 # define RGBLIGHT_SPLIT_SET_CHANGE_MODE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_MODE
42 # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_HSVS
43 # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS rgblight_status.change_flags |= (RGBLIGHT_STATUS_CHANGE_MODE | RGBLIGHT_STATUS_CHANGE_HSVS)
44 # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER
45 # define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK
46 #else
47 # define RGBLIGHT_SPLIT_SET_CHANGE_MODE
48 # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS
49 # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
50 # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
51 # define RGBLIGHT_SPLIT_ANIMATION_TICK
52 #endif
53
54 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
55 #define _RGBM_SINGLE_DYNAMIC(sym)
56 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
57 #define _RGBM_MULTI_DYNAMIC(sym)
58 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##sym,
59 #define _RGBM_TMP_DYNAMIC(sym, msym)
60 static uint8_t static_effect_table[] = {
61 #include "rgblight_modes.h"
62 };
63
64 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
65 #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
66 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
67 #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
68 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##msym,
69 #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_##msym,
70 static uint8_t mode_base_table[] = {
71 0, // RGBLIGHT_MODE_zero
72 #include "rgblight_modes.h"
73 };
74
75 static inline int is_static_effect(uint8_t mode) { return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL; }
76
77 #ifdef RGBLIGHT_LED_MAP
78 const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
79 #endif
80
81 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
82 __attribute__((weak)) const uint8_t RGBLED_GRADIENT_RANGES[] PROGMEM = {255, 170, 127, 85, 64};
83 #endif
84
85 rgblight_config_t rgblight_config;
86 rgblight_status_t rgblight_status = {.timer_enabled = false};
87 bool is_rgblight_initialized = false;
88
89 #ifdef RGBLIGHT_USE_TIMER
90 animation_status_t animation_status = {};
91 #endif
92
93 #ifndef LED_ARRAY
94 LED_TYPE led[RGBLED_NUM];
95 # define LED_ARRAY led
96 #endif
97
98 static uint8_t clipping_start_pos = 0;
99 static uint8_t clipping_num_leds = RGBLED_NUM;
100 static uint8_t effect_start_pos = 0;
101 static uint8_t effect_end_pos = RGBLED_NUM;
102 static uint8_t effect_num_leds = RGBLED_NUM;
103
104 void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
105 clipping_start_pos = start_pos;
106 clipping_num_leds = num_leds;
107 }
108
109 void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds) {
110 if (start_pos >= RGBLED_NUM) return;
111 if (start_pos + num_leds > RGBLED_NUM) return;
112 effect_start_pos = start_pos;
113 effect_end_pos = start_pos + num_leds;
114 effect_num_leds = num_leds;
115 }
116
117 void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
118 HSV hsv = {hue, sat, val};
119 RGB rgb = hsv_to_rgb(hsv);
120 setrgb(rgb.r, rgb.g, rgb.b, led1);
121 }
122
123 void sethsv(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { sethsv_raw(hue, sat, val > RGBLIGHT_LIMIT_VAL ? RGBLIGHT_LIMIT_VAL : val, led1); }
124
125 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
126 (*led1).r = r;
127 (*led1).g = g;
128 (*led1).b = b;
129 #ifdef RGBW
130 (*led1).w = 0;
131 #endif
132 }
133
134 void rgblight_check_config(void) {
135 /* Add some out of bound checks for RGB light config */
136
137 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
138 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
139 } else if (rgblight_config.mode > RGBLIGHT_MODES) {
140 rgblight_config.mode = RGBLIGHT_MODES;
141 }
142
143 if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
144 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
145 }
146 }
147
148 uint32_t eeconfig_read_rgblight(void) {
149 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
150 return eeprom_read_dword(EECONFIG_RGBLIGHT);
151 #else
152 return 0;
153 #endif
154 }
155
156 void eeconfig_update_rgblight(uint32_t val) {
157 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
158 rgblight_check_config();
159 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
160 #endif
161 }
162
163 void eeconfig_update_rgblight_default(void) {
164 rgblight_config.enable = 1;
165 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
166 rgblight_config.hue = 0;
167 rgblight_config.sat = UINT8_MAX;
168 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
169 rgblight_config.speed = 0;
170 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
171 eeconfig_update_rgblight(rgblight_config.raw);
172 }
173
174 void eeconfig_debug_rgblight(void) {
175 dprintf("rgblight_config EEPROM:\n");
176 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
177 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
178 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
179 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
180 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
181 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
182 }
183
184 void rgblight_init(void) {
185 /* if already initialized, don't do it again.
186 If you must do it again, extern this and set to false, first.
187 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
188 if (is_rgblight_initialized) {
189 return;
190 }
191
192 dprintf("rgblight_init called.\n");
193 dprintf("rgblight_init start!\n");
194 if (!eeconfig_is_enabled()) {
195 dprintf("rgblight_init eeconfig is not enabled.\n");
196 eeconfig_init();
197 eeconfig_update_rgblight_default();
198 }
199 rgblight_config.raw = eeconfig_read_rgblight();
200 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
201 if (!rgblight_config.mode) {
202 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
203 eeconfig_update_rgblight_default();
204 rgblight_config.raw = eeconfig_read_rgblight();
205 }
206 rgblight_check_config();
207
208 eeconfig_debug_rgblight(); // display current eeprom values
209
210 #ifdef RGBLIGHT_USE_TIMER
211 rgblight_timer_init(); // setup the timer
212 #endif
213
214 if (rgblight_config.enable) {
215 rgblight_mode_noeeprom(rgblight_config.mode);
216 }
217
218 is_rgblight_initialized = true;
219 }
220
221 uint32_t rgblight_read_dword(void) { return rgblight_config.raw; }
222
223 void rgblight_update_dword(uint32_t dword) {
224 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
225 rgblight_config.raw = dword;
226 if (rgblight_config.enable)
227 rgblight_mode_noeeprom(rgblight_config.mode);
228 else {
229 #ifdef RGBLIGHT_USE_TIMER
230 rgblight_timer_disable();
231 #endif
232 rgblight_set();
233 }
234 }
235
236 void rgblight_increase(void) {
237 uint8_t mode = 0;
238 if (rgblight_config.mode < RGBLIGHT_MODES) {
239 mode = rgblight_config.mode + 1;
240 }
241 rgblight_mode(mode);
242 }
243 void rgblight_decrease(void) {
244 uint8_t mode = 0;
245 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
246 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
247 mode = rgblight_config.mode - 1;
248 }
249 rgblight_mode(mode);
250 }
251 void rgblight_step_helper(bool write_to_eeprom) {
252 uint8_t mode = 0;
253 mode = rgblight_config.mode + 1;
254 if (mode > RGBLIGHT_MODES) {
255 mode = 1;
256 }
257 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
258 }
259 void rgblight_step_noeeprom(void) { rgblight_step_helper(false); }
260 void rgblight_step(void) { rgblight_step_helper(true); }
261 void rgblight_step_reverse_helper(bool write_to_eeprom) {
262 uint8_t mode = 0;
263 mode = rgblight_config.mode - 1;
264 if (mode < 1) {
265 mode = RGBLIGHT_MODES;
266 }
267 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
268 }
269 void rgblight_step_reverse_noeeprom(void) { rgblight_step_reverse_helper(false); }
270 void rgblight_step_reverse(void) { rgblight_step_reverse_helper(true); }
271
272 uint8_t rgblight_get_mode(void) {
273 if (!rgblight_config.enable) {
274 return false;
275 }
276
277 return rgblight_config.mode;
278 }
279
280 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
281 if (!rgblight_config.enable) {
282 return;
283 }
284 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
285 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
286 } else if (mode > RGBLIGHT_MODES) {
287 rgblight_config.mode = RGBLIGHT_MODES;
288 } else {
289 rgblight_config.mode = mode;
290 }
291 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
292 if (write_to_eeprom) {
293 eeconfig_update_rgblight(rgblight_config.raw);
294 dprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
295 } else {
296 dprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
297 }
298 if (is_static_effect(rgblight_config.mode)) {
299 #ifdef RGBLIGHT_USE_TIMER
300 rgblight_timer_disable();
301 #endif
302 } else {
303 #ifdef RGBLIGHT_USE_TIMER
304 rgblight_timer_enable();
305 #endif
306 }
307 #ifdef RGBLIGHT_USE_TIMER
308 animation_status.restart = true;
309 #endif
310 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
311 }
312
313 void rgblight_mode(uint8_t mode) { rgblight_mode_eeprom_helper(mode, true); }
314
315 void rgblight_mode_noeeprom(uint8_t mode) { rgblight_mode_eeprom_helper(mode, false); }
316
317 void rgblight_toggle(void) {
318 dprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
319 if (rgblight_config.enable) {
320 rgblight_disable();
321 } else {
322 rgblight_enable();
323 }
324 }
325
326 void rgblight_toggle_noeeprom(void) {
327 dprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
328 if (rgblight_config.enable) {
329 rgblight_disable_noeeprom();
330 } else {
331 rgblight_enable_noeeprom();
332 }
333 }
334
335 void rgblight_enable(void) {
336 rgblight_config.enable = 1;
337 // No need to update EEPROM here. rgblight_mode() will do that, actually
338 // eeconfig_update_rgblight(rgblight_config.raw);
339 dprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
340 rgblight_mode(rgblight_config.mode);
341 }
342
343 void rgblight_enable_noeeprom(void) {
344 rgblight_config.enable = 1;
345 dprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
346 rgblight_mode_noeeprom(rgblight_config.mode);
347 }
348
349 void rgblight_disable(void) {
350 rgblight_config.enable = 0;
351 eeconfig_update_rgblight(rgblight_config.raw);
352 dprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
353 #ifdef RGBLIGHT_USE_TIMER
354 rgblight_timer_disable();
355 #endif
356 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
357 wait_ms(50);
358 rgblight_set();
359 }
360
361 void rgblight_disable_noeeprom(void) {
362 rgblight_config.enable = 0;
363 dprintf("rgblight disable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
364 #ifdef RGBLIGHT_USE_TIMER
365 rgblight_timer_disable();
366 #endif
367 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
368 wait_ms(50);
369 rgblight_set();
370 }
371
372 void rgblight_increase_hue_helper(bool write_to_eeprom) {
373 uint8_t hue = rgblight_config.hue + RGBLIGHT_HUE_STEP;
374 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
375 }
376 void rgblight_increase_hue_noeeprom(void) { rgblight_increase_hue_helper(false); }
377 void rgblight_increase_hue(void) { rgblight_increase_hue_helper(true); }
378 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
379 uint8_t hue = rgblight_config.hue - RGBLIGHT_HUE_STEP;
380 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
381 }
382 void rgblight_decrease_hue_noeeprom(void) { rgblight_decrease_hue_helper(false); }
383 void rgblight_decrease_hue(void) { rgblight_decrease_hue_helper(true); }
384 void rgblight_increase_sat_helper(bool write_to_eeprom) {
385 uint8_t sat = qadd8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
386 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
387 }
388 void rgblight_increase_sat_noeeprom(void) { rgblight_increase_sat_helper(false); }
389 void rgblight_increase_sat(void) { rgblight_increase_sat_helper(true); }
390 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
391 uint8_t sat = qsub8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
392 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
393 }
394 void rgblight_decrease_sat_noeeprom(void) { rgblight_decrease_sat_helper(false); }
395 void rgblight_decrease_sat(void) { rgblight_decrease_sat_helper(true); }
396 void rgblight_increase_val_helper(bool write_to_eeprom) {
397 uint8_t val = qadd8(rgblight_config.val, RGBLIGHT_VAL_STEP);
398 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
399 }
400 void rgblight_increase_val_noeeprom(void) { rgblight_increase_val_helper(false); }
401 void rgblight_increase_val(void) { rgblight_increase_val_helper(true); }
402 void rgblight_decrease_val_helper(bool write_to_eeprom) {
403 uint8_t val = qsub8(rgblight_config.val, RGBLIGHT_VAL_STEP);
404 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
405 }
406 void rgblight_decrease_val_noeeprom(void) { rgblight_decrease_val_helper(false); }
407 void rgblight_decrease_val(void) { rgblight_decrease_val_helper(true); }
408 void rgblight_increase_speed(void) {
409 if (rgblight_config.speed < 3) rgblight_config.speed++;
410 // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
411 eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
412 }
413
414 void rgblight_decrease_speed(void) {
415 if (rgblight_config.speed > 0) rgblight_config.speed--;
416 // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
417 eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
418 }
419
420 void rgblight_sethsv_noeeprom_old(uint8_t hue, uint8_t sat, uint8_t val) {
421 if (rgblight_config.enable) {
422 LED_TYPE tmp_led;
423 sethsv(hue, sat, val, &tmp_led);
424 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
425 }
426 }
427
428 void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
429 if (rgblight_config.enable) {
430 rgblight_status.base_mode = mode_base_table[rgblight_config.mode];
431 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
432 // same static color
433 LED_TYPE tmp_led;
434 sethsv(hue, sat, val, &tmp_led);
435 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
436 } else {
437 // all LEDs in same color
438 if (1 == 0) { // dummy
439 }
440 #ifdef RGBLIGHT_EFFECT_BREATHING
441 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
442 // breathing mode, ignore the change of val, use in memory value instead
443 val = rgblight_config.val;
444 }
445 #endif
446 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
447 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
448 // rainbow mood, ignore the change of hue
449 hue = rgblight_config.hue;
450 }
451 #endif
452 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
453 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
454 // rainbow swirl, ignore the change of hue
455 hue = rgblight_config.hue;
456 }
457 #endif
458 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
459 else if (rgblight_status.base_mode == RGBLIGHT_MODE_STATIC_GRADIENT) {
460 // static gradient
461 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
462 bool direction = (delta % 2) == 0;
463 # ifdef __AVR__
464 // probably due to how pgm_read_word is defined for ARM, but the ARM compiler really hates this line
465 uint8_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[delta / 2]);
466 # else
467 uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2];
468 # endif
469 for (uint8_t i = 0; i < effect_num_leds; i++) {
470 uint8_t _hue = ((uint16_t)i * (uint16_t)range) / effect_num_leds;
471 if (direction) {
472 _hue = hue + _hue;
473 } else {
474 _hue = hue - _hue;
475 }
476 dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range);
477 sethsv(_hue, sat, val, (LED_TYPE *)&led[i + effect_start_pos]);
478 }
479 rgblight_set();
480 }
481 #endif
482 }
483 #ifdef RGBLIGHT_SPLIT
484 if (rgblight_config.hue != hue || rgblight_config.sat != sat || rgblight_config.val != val) {
485 RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
486 }
487 #endif
488 rgblight_config.hue = hue;
489 rgblight_config.sat = sat;
490 rgblight_config.val = val;
491 if (write_to_eeprom) {
492 eeconfig_update_rgblight(rgblight_config.raw);
493 dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
494 } else {
495 dprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
496 }
497 }
498 }
499
500 void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, true); }
501
502 void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, false); }
503
504 uint8_t rgblight_get_hue(void) { return rgblight_config.hue; }
505
506 uint8_t rgblight_get_sat(void) { return rgblight_config.sat; }
507
508 uint8_t rgblight_get_val(void) { return rgblight_config.val; }
509
510 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
511 if (!rgblight_config.enable) {
512 return;
513 }
514
515 for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
516 led[i].r = r;
517 led[i].g = g;
518 led[i].b = b;
519 #ifdef RGBW
520 led[i].w = 0;
521 #endif
522 }
523 rgblight_set();
524 }
525
526 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
527 if (!rgblight_config.enable || index >= RGBLED_NUM) {
528 return;
529 }
530
531 led[index].r = r;
532 led[index].g = g;
533 led[index].b = b;
534 #ifdef RGBW
535 led[index].w = 0;
536 #endif
537 rgblight_set();
538 }
539
540 void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index) {
541 if (!rgblight_config.enable) {
542 return;
543 }
544
545 LED_TYPE tmp_led;
546 sethsv(hue, sat, val, &tmp_led);
547 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
548 }
549
550 #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
551
552 static uint8_t get_interval_time(const uint8_t *default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
553 return
554 # ifdef VELOCIKEY_ENABLE
555 velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
556 # endif
557 pgm_read_byte(default_interval_address);
558 }
559
560 #endif
561
562 void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
563 if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) {
564 return;
565 }
566
567 for (uint8_t i = start; i < end; i++) {
568 led[i].r = r;
569 led[i].g = g;
570 led[i].b = b;
571 #ifdef RGBW
572 led[i].w = 0;
573 #endif
574 }
575 rgblight_set();
576 wait_ms(1);
577 }
578
579 void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
580 if (!rgblight_config.enable) {
581 return;
582 }
583
584 LED_TYPE tmp_led;
585 sethsv(hue, sat, val, &tmp_led);
586 rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
587 }
588
589 #ifndef RGBLIGHT_SPLIT
590 void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) { rgblight_setrgb_range(r, g, b, 0, (uint8_t)RGBLED_NUM / 2); }
591
592 void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b) { rgblight_setrgb_range(r, g, b, (uint8_t)RGBLED_NUM / 2, (uint8_t)RGBLED_NUM); }
593
594 void rgblight_sethsv_master(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_range(hue, sat, val, 0, (uint8_t)RGBLED_NUM / 2); }
595
596 void rgblight_sethsv_slave(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_range(hue, sat, val, (uint8_t)RGBLED_NUM / 2, (uint8_t)RGBLED_NUM); }
597 #endif // ifndef RGBLIGHT_SPLIT
598
599 #ifndef RGBLIGHT_CUSTOM_DRIVER
600 void rgblight_set(void) {
601 LED_TYPE *start_led;
602 uint16_t num_leds = clipping_num_leds;
603
604 if (!rgblight_config.enable) {
605 for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
606 led[i].r = 0;
607 led[i].g = 0;
608 led[i].b = 0;
609 # ifdef RGBW
610 led[i].w = 0;
611 # endif
612 }
613 }
614
615 # ifdef RGBLIGHT_LED_MAP
616 LED_TYPE led0[RGBLED_NUM];
617 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
618 led0[i] = led[pgm_read_byte(&led_map[i])];
619 }
620 start_led = led0 + clipping_start_pos;
621 # else
622 start_led = led + clipping_start_pos;
623 # endif
624
625 #ifdef RGBW
626 for (uint8_t i = 0; i < num_leds; i++) {
627 convert_rgb_to_rgbw(&start_led[i]);
628 }
629 #endif
630 ws2812_setleds(start_led, num_leds);
631 }
632 #endif
633
634 #ifdef RGBLIGHT_SPLIT
635 /* for split keyboard master side */
636 uint8_t rgblight_get_change_flags(void) { return rgblight_status.change_flags; }
637
638 void rgblight_clear_change_flags(void) { rgblight_status.change_flags = 0; }
639
640 void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
641 syncinfo->config = rgblight_config;
642 syncinfo->status = rgblight_status;
643 }
644
645 /* for split keyboard slave side */
646 void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
647 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
648 if (syncinfo->config.enable) {
649 rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
650 rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
651 } else {
652 rgblight_disable_noeeprom();
653 }
654 }
655 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
656 rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
657 // rgblight_config.speed = config->speed; // NEED???
658 }
659 # ifdef RGBLIGHT_USE_TIMER
660 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
661 if (syncinfo->status.timer_enabled) {
662 rgblight_timer_enable();
663 } else {
664 rgblight_timer_disable();
665 }
666 }
667 # ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
668 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
669 animation_status.restart = true;
670 }
671 # endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
672 # endif /* RGBLIGHT_USE_TIMER */
673 }
674 #endif /* RGBLIGHT_SPLIT */
675
676 #ifdef RGBLIGHT_USE_TIMER
677
678 typedef void (*effect_func_t)(animation_status_t *anim);
679
680 // Animation timer -- use system timer (AVR Timer0)
681 void rgblight_timer_init(void) {
682 // OLD!!!! Animation timer -- AVR Timer3
683 // static uint8_t rgblight_timer_is_init = 0;
684 // if (rgblight_timer_is_init) {
685 // return;
686 // }
687 // rgblight_timer_is_init = 1;
688 // /* Timer 3 setup */
689 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
690 // | _BV(CS30); // Clock selelct: clk/1
691 // /* Set TOP value */
692 // uint8_t sreg = SREG;
693 // cli();
694 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
695 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
696 // SREG = sreg;
697
698 rgblight_status.timer_enabled = false;
699 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
700 }
701 void rgblight_timer_enable(void) {
702 if (!is_static_effect(rgblight_config.mode)) {
703 rgblight_status.timer_enabled = true;
704 }
705 animation_status.last_timer = timer_read();
706 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
707 dprintf("rgblight timer enabled.\n");
708 }
709 void rgblight_timer_disable(void) {
710 rgblight_status.timer_enabled = false;
711 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
712 dprintf("rgblight timer disable.\n");
713 }
714 void rgblight_timer_toggle(void) {
715 dprintf("rgblight timer toggle.\n");
716 if (rgblight_status.timer_enabled) {
717 rgblight_timer_disable();
718 } else {
719 rgblight_timer_enable();
720 }
721 }
722
723 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
724 rgblight_enable();
725 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
726 rgblight_setrgb(r, g, b);
727 }
728
729 static void rgblight_effect_dummy(animation_status_t *anim) {
730 // do nothing
731 /********
732 dprintf("rgblight_task() what happened?\n");
733 dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
734 dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
735 rgblight_config.mode, rgblight_status.base_mode,
736 rgblight_status.timer_enabled);
737 dprintf("last_timer = %d\n",anim->last_timer);
738 **/
739 }
740
741 void rgblight_task(void) {
742 if (rgblight_status.timer_enabled) {
743 effect_func_t effect_func = rgblight_effect_dummy;
744 uint16_t interval_time = 2000; // dummy interval
745 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
746 animation_status.delta = delta;
747
748 // static light mode, do nothing here
749 if (1 == 0) { // dummy
750 }
751 # ifdef RGBLIGHT_EFFECT_BREATHING
752 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
753 // breathing mode
754 interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
755 effect_func = rgblight_effect_breathing;
756 }
757 # endif
758 # ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
759 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
760 // rainbow mood mode
761 interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
762 effect_func = rgblight_effect_rainbow_mood;
763 }
764 # endif
765 # ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
766 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
767 // rainbow swirl mode
768 interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
769 effect_func = rgblight_effect_rainbow_swirl;
770 }
771 # endif
772 # ifdef RGBLIGHT_EFFECT_SNAKE
773 else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
774 // snake mode
775 interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
776 effect_func = rgblight_effect_snake;
777 }
778 # endif
779 # ifdef RGBLIGHT_EFFECT_KNIGHT
780 else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
781 // knight mode
782 interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
783 effect_func = rgblight_effect_knight;
784 }
785 # endif
786 # ifdef RGBLIGHT_EFFECT_CHRISTMAS
787 else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
788 // christmas mode
789 interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
790 effect_func = (effect_func_t)rgblight_effect_christmas;
791 }
792 # endif
793 # ifdef RGBLIGHT_EFFECT_RGB_TEST
794 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
795 // RGB test mode
796 interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
797 effect_func = (effect_func_t)rgblight_effect_rgbtest;
798 }
799 # endif
800 # ifdef RGBLIGHT_EFFECT_ALTERNATING
801 else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING) {
802 interval_time = 500;
803 effect_func = (effect_func_t)rgblight_effect_alternating;
804 }
805 # endif
806 if (animation_status.restart) {
807 animation_status.restart = false;
808 animation_status.last_timer = timer_read() - interval_time - 1;
809 animation_status.pos16 = 0; // restart signal to local each effect
810 }
811 if (timer_elapsed(animation_status.last_timer) >= interval_time) {
812 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
813 static uint16_t report_last_timer = 0;
814 static bool tick_flag = false;
815 uint16_t oldpos16;
816 if (tick_flag) {
817 tick_flag = false;
818 if (timer_elapsed(report_last_timer) >= 30000) {
819 report_last_timer = timer_read();
820 dprintf("rgblight animation tick report to slave\n");
821 RGBLIGHT_SPLIT_ANIMATION_TICK;
822 }
823 }
824 oldpos16 = animation_status.pos16;
825 # endif
826 animation_status.last_timer += interval_time;
827 effect_func(&animation_status);
828 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
829 if (animation_status.pos16 == 0 && oldpos16 != 0) {
830 tick_flag = true;
831 }
832 # endif
833 }
834 }
835 }
836
837 #endif /* RGBLIGHT_USE_TIMER */
838
839 // Effects
840 #ifdef RGBLIGHT_EFFECT_BREATHING
841
842 # ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
843 # ifndef RGBLIGHT_BREATHE_TABLE_SIZE
844 # define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
845 # endif
846 # include <rgblight_breathe_table.h>
847 # endif
848
849 __attribute__((weak)) const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
850
851 void rgblight_effect_breathing(animation_status_t *anim) {
852 float val;
853
854 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
855 # ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
856 val = pgm_read_byte(&rgblight_effect_breathe_table[anim->pos / table_scale]);
857 # else
858 val = (exp(sin((anim->pos / 255.0) * M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER / M_E) * (RGBLIGHT_EFFECT_BREATHE_MAX / (M_E - 1 / M_E));
859 # endif
860 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
861 anim->pos = (anim->pos + 1);
862 }
863 #endif
864
865 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
866 __attribute__((weak)) const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
867
868 void rgblight_effect_rainbow_mood(animation_status_t *anim) {
869 rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
870 anim->current_hue++;
871 }
872 #endif
873
874 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
875 # ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
876 # define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
877 # endif
878
879 __attribute__((weak)) const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
880
881 void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
882 uint8_t hue;
883 uint8_t i;
884
885 for (i = 0; i < effect_num_leds; i++) {
886 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue);
887 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
888 }
889 rgblight_set();
890
891 if (anim->delta % 2) {
892 anim->current_hue++;
893 } else {
894 anim->current_hue--;
895 }
896 }
897 #endif
898
899 #ifdef RGBLIGHT_EFFECT_SNAKE
900 __attribute__((weak)) const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
901
902 void rgblight_effect_snake(animation_status_t *anim) {
903 static uint8_t pos = 0;
904 uint8_t i, j;
905 int8_t k;
906 int8_t increment = 1;
907
908 if (anim->delta % 2) {
909 increment = -1;
910 }
911
912 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
913 if (anim->pos == 0) { // restart signal
914 if (increment == 1) {
915 pos = effect_num_leds - 1;
916 } else {
917 pos = 0;
918 }
919 anim->pos = 1;
920 }
921 # endif
922
923 for (i = 0; i < effect_num_leds; i++) {
924 LED_TYPE *ledp = led + i + effect_start_pos;
925 ledp->r = 0;
926 ledp->g = 0;
927 ledp->b = 0;
928 # ifdef RGBW
929 ledp->w = 0;
930 # endif
931 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
932 k = pos + j * increment;
933 if (k > RGBLED_NUM) {
934 k = k % RGBLED_NUM;
935 }
936 if (k < 0) {
937 k = k + effect_num_leds;
938 }
939 if (i == k) {
940 sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val * (RGBLIGHT_EFFECT_SNAKE_LENGTH - j) / RGBLIGHT_EFFECT_SNAKE_LENGTH), ledp);
941 }
942 }
943 }
944 rgblight_set();
945 if (increment == 1) {
946 if (pos - 1 < 0) {
947 pos = effect_num_leds - 1;
948 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
949 anim->pos = 0;
950 # endif
951 } else {
952 pos -= 1;
953 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
954 anim->pos = 1;
955 # endif
956 }
957 } else {
958 pos = (pos + 1) % effect_num_leds;
959 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
960 anim->pos = pos;
961 # endif
962 }
963 }
964 #endif
965
966 #ifdef RGBLIGHT_EFFECT_KNIGHT
967 __attribute__((weak)) const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
968
969 void rgblight_effect_knight(animation_status_t *anim) {
970 static int8_t low_bound = 0;
971 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
972 static int8_t increment = 1;
973 uint8_t i, cur;
974
975 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
976 if (anim->pos == 0) { // restart signal
977 anim->pos = 1;
978 low_bound = 0;
979 high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
980 increment = 1;
981 }
982 # endif
983 // Set all the LEDs to 0
984 for (i = effect_start_pos; i < effect_end_pos; i++) {
985 led[i].r = 0;
986 led[i].g = 0;
987 led[i].b = 0;
988 # ifdef RGBW
989 led[i].w = 0;
990 # endif
991 }
992 // Determine which LEDs should be lit up
993 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
994 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos;
995
996 if (i >= low_bound && i <= high_bound) {
997 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
998 } else {
999 led[cur].r = 0;
1000 led[cur].g = 0;
1001 led[cur].b = 0;
1002 # ifdef RGBW
1003 led[cur].w = 0;
1004 # endif
1005 }
1006 }
1007 rgblight_set();
1008
1009 // Move from low_bound to high_bound changing the direction we increment each
1010 // time a boundary is hit.
1011 low_bound += increment;
1012 high_bound += increment;
1013
1014 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
1015 increment = -increment;
1016 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1017 if (increment == 1) {
1018 anim->pos = 0;
1019 }
1020 # endif
1021 }
1022 }
1023 #endif
1024
1025 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
1026 void rgblight_effect_christmas(animation_status_t *anim) {
1027 uint8_t hue;
1028 uint8_t i;
1029
1030 anim->current_offset = (anim->current_offset + 1) % 2;
1031 for (i = 0; i < effect_num_leds; i++) {
1032 hue = 0 + ((i / RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85;
1033 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
1034 }
1035 rgblight_set();
1036 }
1037 #endif
1038
1039 #ifdef RGBLIGHT_EFFECT_RGB_TEST
1040 __attribute__((weak)) const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
1041
1042 void rgblight_effect_rgbtest(animation_status_t *anim) {
1043 static uint8_t maxval = 0;
1044 uint8_t g;
1045 uint8_t r;
1046 uint8_t b;
1047
1048 if (maxval == 0) {
1049 LED_TYPE tmp_led;
1050 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
1051 maxval = tmp_led.r;
1052 }
1053 g = r = b = 0;
1054 switch (anim->pos) {
1055 case 0:
1056 r = maxval;
1057 break;
1058 case 1:
1059 g = maxval;
1060 break;
1061 case 2:
1062 b = maxval;
1063 break;
1064 }
1065 rgblight_setrgb(r, g, b);
1066 anim->pos = (anim->pos + 1) % 3;
1067 }
1068 #endif
1069
1070 #ifdef RGBLIGHT_EFFECT_ALTERNATING
1071 void rgblight_effect_alternating(animation_status_t *anim) {
1072 for (int i = 0; i < effect_num_leds; i++) {
1073 LED_TYPE *ledp = led + i + effect_start_pos;
1074 if (i < effect_num_leds / 2 && anim->pos) {
1075 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
1076 } else if (i >= effect_num_leds / 2 && !anim->pos) {
1077 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
1078 } else {
1079 sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp);
1080 }
1081 }
1082 rgblight_set();
1083 anim->pos = (anim->pos + 1) % 2;
1084 }
1085 #endif