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