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