]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgblight.c
Add rgblight mode 35 (R,G,B test mode) (#3114)
[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 <avr/eeprom.h>
18 #include <avr/interrupt.h>
19 #include <util/delay.h>
20 #include "progmem.h"
21 #include "timer.h"
22 #include "rgblight.h"
23 #include "debug.h"
24 #include "led_tables.h"
25
26 #ifndef RGBLIGHT_LIMIT_VAL
27 #define RGBLIGHT_LIMIT_VAL 255
28 #endif
29
30 #define MIN(a,b) (((a)<(b))?(a):(b))
31 #define MAX(a,b) (((a)>(b))?(a):(b))
32
33 __attribute__ ((weak))
34 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
35 __attribute__ ((weak))
36 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
37 __attribute__ ((weak))
38 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
39 __attribute__ ((weak))
40 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
41 __attribute__ ((weak))
42 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
43 __attribute__ ((weak))
44 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
45 __attribute__ ((weak))
46 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
47
48 rgblight_config_t rgblight_config;
49
50 LED_TYPE led[RGBLED_NUM];
51 uint8_t rgblight_inited = 0;
52 bool rgblight_timer_enabled = false;
53
54 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
55   uint8_t r = 0, g = 0, b = 0, base, color;
56
57   if (val > RGBLIGHT_LIMIT_VAL) {
58       val=RGBLIGHT_LIMIT_VAL; // limit the val
59   }
60
61   if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
62     r = val;
63     g = val;
64     b = val;
65   } else {
66     base = ((255 - sat) * val) >> 8;
67     color = (val - base) * (hue % 60) / 60;
68
69     switch (hue / 60) {
70       case 0:
71         r = val;
72         g = base + color;
73         b = base;
74         break;
75       case 1:
76         r = val - color;
77         g = val;
78         b = base;
79         break;
80       case 2:
81         r = base;
82         g = val;
83         b = base + color;
84         break;
85       case 3:
86         r = base;
87         g = val - color;
88         b = val;
89         break;
90       case 4:
91         r = base + color;
92         g = base;
93         b = val;
94         break;
95       case 5:
96         r = val;
97         g = base;
98         b = val - color;
99         break;
100     }
101   }
102   r = pgm_read_byte(&CIE1931_CURVE[r]);
103   g = pgm_read_byte(&CIE1931_CURVE[g]);
104   b = pgm_read_byte(&CIE1931_CURVE[b]);
105
106   setrgb(r, g, b, led1);
107 }
108
109 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
110   (*led1).r = r;
111   (*led1).g = g;
112   (*led1).b = b;
113 }
114
115
116 uint32_t eeconfig_read_rgblight(void) {
117   return eeprom_read_dword(EECONFIG_RGBLIGHT);
118 }
119 void eeconfig_update_rgblight(uint32_t val) {
120   eeprom_update_dword(EECONFIG_RGBLIGHT, val);
121 }
122 void eeconfig_update_rgblight_default(void) {
123   dprintf("eeconfig_update_rgblight_default\n");
124   rgblight_config.enable = 1;
125   rgblight_config.mode = 1;
126   rgblight_config.hue = 0;
127   rgblight_config.sat = 255;
128   rgblight_config.val = RGBLIGHT_LIMIT_VAL;
129   rgblight_config.speed = 0;
130   eeconfig_update_rgblight(rgblight_config.raw);
131 }
132 void eeconfig_debug_rgblight(void) {
133   dprintf("rgblight_config eprom\n");
134   dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
135   dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
136   dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
137   dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
138   dprintf("rgblight_config.val = %d\n", rgblight_config.val);
139   dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
140 }
141
142 void rgblight_init(void) {
143   debug_enable = 1; // Debug ON!
144   dprintf("rgblight_init called.\n");
145   rgblight_inited = 1;
146   dprintf("rgblight_init start!\n");
147   if (!eeconfig_is_enabled()) {
148     dprintf("rgblight_init eeconfig is not enabled.\n");
149     eeconfig_init();
150     eeconfig_update_rgblight_default();
151   }
152   rgblight_config.raw = eeconfig_read_rgblight();
153   if (!rgblight_config.mode) {
154     dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
155     eeconfig_update_rgblight_default();
156     rgblight_config.raw = eeconfig_read_rgblight();
157   }
158   eeconfig_debug_rgblight(); // display current eeprom values
159
160   #ifdef RGBLIGHT_ANIMATIONS
161     rgblight_timer_init(); // setup the timer
162   #endif
163
164   if (rgblight_config.enable) {
165     rgblight_mode_noeeprom(rgblight_config.mode);
166   }
167 }
168
169 void rgblight_update_dword(uint32_t dword) {
170   rgblight_config.raw = dword;
171   eeconfig_update_rgblight(rgblight_config.raw);
172   if (rgblight_config.enable)
173     rgblight_mode(rgblight_config.mode);
174   else {
175     #ifdef RGBLIGHT_ANIMATIONS
176       rgblight_timer_disable();
177     #endif
178       rgblight_set();
179   }
180 }
181
182 void rgblight_increase(void) {
183   uint8_t mode = 0;
184   if (rgblight_config.mode < RGBLIGHT_MODES) {
185     mode = rgblight_config.mode + 1;
186   }
187   rgblight_mode(mode);
188 }
189 void rgblight_decrease(void) {
190   uint8_t mode = 0;
191   // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
192   if (rgblight_config.mode > 1) {
193     mode = rgblight_config.mode - 1;
194   }
195   rgblight_mode(mode);
196 }
197 void rgblight_step(void) {
198   uint8_t mode = 0;
199   mode = rgblight_config.mode + 1;
200   if (mode > RGBLIGHT_MODES) {
201     mode = 1;
202   }
203   rgblight_mode(mode);
204 }
205 void rgblight_step_reverse(void) {
206   uint8_t mode = 0;
207   mode = rgblight_config.mode - 1;
208   if (mode < 1) {
209     mode = RGBLIGHT_MODES;
210   }
211   rgblight_mode(mode);
212 }
213
214 uint32_t rgblight_get_mode(void) {
215   if (!rgblight_config.enable) {
216     return false;
217   }
218
219   return rgblight_config.mode;
220 }
221
222 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
223   if (!rgblight_config.enable) {
224     return;
225   }
226   if (mode < 1) {
227     rgblight_config.mode = 1;
228   } else if (mode > RGBLIGHT_MODES) {
229     rgblight_config.mode = RGBLIGHT_MODES;
230   } else {
231     rgblight_config.mode = mode;
232   }
233   if (write_to_eeprom) {
234     eeconfig_update_rgblight(rgblight_config.raw);
235     xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
236   } else {
237     xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
238   }
239   if (rgblight_config.mode == 1) {
240     #ifdef RGBLIGHT_ANIMATIONS
241       rgblight_timer_disable();
242     #endif
243   } else if ((rgblight_config.mode >= 2 && rgblight_config.mode <= 24) ||
244              rgblight_config.mode == 35 ) {
245     // MODE 2-5, breathing
246     // MODE 6-8, rainbow mood
247     // MODE 9-14, rainbow swirl
248     // MODE 15-20, snake
249     // MODE 21-23, knight
250     // MODE 24, xmas
251     // MODE 35  RGB test
252
253     #ifdef RGBLIGHT_ANIMATIONS
254       rgblight_timer_enable();
255     #endif
256   } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
257     // MODE 25-34, static gradient
258
259     #ifdef RGBLIGHT_ANIMATIONS
260       rgblight_timer_disable();
261     #endif
262   }
263   rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
264 }
265
266 void rgblight_mode(uint8_t mode) {
267   rgblight_mode_eeprom_helper(mode, true);
268 }
269
270 void rgblight_mode_noeeprom(uint8_t mode) {
271   rgblight_mode_eeprom_helper(mode, false);
272 }
273
274
275 void rgblight_toggle(void) {
276   xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
277   if (rgblight_config.enable) {
278     rgblight_disable();
279   }
280   else {
281     rgblight_enable();
282   }
283 }
284
285 void rgblight_toggle_noeeprom(void) {
286   xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
287   if (rgblight_config.enable) {
288     rgblight_disable_noeeprom();
289   }
290   else {
291     rgblight_enable_noeeprom();
292   }
293 }
294
295 void rgblight_enable(void) {
296   rgblight_config.enable = 1;
297   // No need to update EEPROM here. rgblight_mode() will do that, actually
298   //eeconfig_update_rgblight(rgblight_config.raw);
299   xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
300   rgblight_mode(rgblight_config.mode);
301 }
302
303 void rgblight_enable_noeeprom(void) {
304   rgblight_config.enable = 1;
305   xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
306   rgblight_mode_noeeprom(rgblight_config.mode);
307 }
308
309 void rgblight_disable(void) {
310   rgblight_config.enable = 0;
311   eeconfig_update_rgblight(rgblight_config.raw);
312   xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
313   #ifdef RGBLIGHT_ANIMATIONS
314     rgblight_timer_disable();
315   #endif
316   _delay_ms(50);
317   rgblight_set();
318 }
319
320 void rgblight_disable_noeeprom(void) {
321   rgblight_config.enable = 0;
322   xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
323   #ifdef RGBLIGHT_ANIMATIONS
324     rgblight_timer_disable();
325   #endif
326   _delay_ms(50);
327   rgblight_set();
328 }
329
330
331 // Deals with the messy details of incrementing an integer
332 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
333     int16_t new_value = value;
334     new_value += step;
335     return MIN( MAX( new_value, min ), max );
336 }
337
338 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
339     int16_t new_value = value;
340     new_value -= step;
341     return MIN( MAX( new_value, min ), max );
342 }
343
344 void rgblight_increase_hue(void) {
345   uint16_t hue;
346   hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
347   rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
348 }
349 void rgblight_decrease_hue(void) {
350   uint16_t hue;
351   if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
352     hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
353   } else {
354     hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
355   }
356   rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
357 }
358 void rgblight_increase_sat(void) {
359   uint8_t sat;
360   if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
361     sat = 255;
362   } else {
363     sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
364   }
365   rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
366 }
367 void rgblight_decrease_sat(void) {
368   uint8_t sat;
369   if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
370     sat = 0;
371   } else {
372     sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
373   }
374   rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
375 }
376 void rgblight_increase_val(void) {
377   uint8_t val;
378   if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
379     val = RGBLIGHT_LIMIT_VAL;
380   } else {
381     val = rgblight_config.val + RGBLIGHT_VAL_STEP;
382   }
383   rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
384 }
385 void rgblight_decrease_val(void) {
386   uint8_t val;
387   if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
388     val = 0;
389   } else {
390     val = rgblight_config.val - RGBLIGHT_VAL_STEP;
391   }
392   rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
393 }
394 void rgblight_increase_speed(void) {
395     rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
396     eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
397 }
398
399 void rgblight_decrease_speed(void) {
400     rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
401     eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
402 }
403
404 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
405   if (rgblight_config.enable) {
406     LED_TYPE tmp_led;
407     sethsv(hue, sat, val, &tmp_led);
408     // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
409     rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
410   }
411 }
412
413 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
414   if (rgblight_config.enable) {
415     if (rgblight_config.mode == 1) {
416       // same static color
417       LED_TYPE tmp_led;
418       sethsv(hue, sat, val, &tmp_led);
419       rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
420     } else {
421       // all LEDs in same color
422       if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
423         // breathing mode, ignore the change of val, use in memory value instead
424         val = rgblight_config.val;
425       } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
426         // rainbow mood and rainbow swirl, ignore the change of hue
427         hue = rgblight_config.hue;
428       } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
429         // static gradient
430         uint16_t _hue;
431         int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
432         uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
433         for (uint8_t i = 0; i < RGBLED_NUM; i++) {
434           _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
435           dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
436           sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
437         }
438         rgblight_set();
439       }
440     }
441     rgblight_config.hue = hue;
442     rgblight_config.sat = sat;
443     rgblight_config.val = val;
444     if (write_to_eeprom) {
445       eeconfig_update_rgblight(rgblight_config.raw);
446       xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
447     } else {
448       xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
449     }
450   }
451 }
452
453 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
454   rgblight_sethsv_eeprom_helper(hue, sat, val, true);
455 }
456
457 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
458   rgblight_sethsv_eeprom_helper(hue, sat, val, false);
459 }
460
461 uint16_t rgblight_get_hue(void) {
462   return rgblight_config.hue;
463 }
464
465 uint8_t rgblight_get_sat(void) {
466   return rgblight_config.sat;
467 }
468
469 uint8_t rgblight_get_val(void) {
470   return rgblight_config.val;
471 }
472
473 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
474   if (!rgblight_config.enable) { return; }
475
476   for (uint8_t i = 0; i < RGBLED_NUM; i++) {
477     led[i].r = r;
478     led[i].g = g;
479     led[i].b = b;
480   }
481   rgblight_set();
482 }
483
484 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
485   if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
486
487   led[index].r = r;
488   led[index].g = g;
489   led[index].b = b;
490   rgblight_set();
491 }
492
493 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
494   if (!rgblight_config.enable) { return; }
495
496   LED_TYPE tmp_led;
497   sethsv(hue, sat, val, &tmp_led);
498   rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
499 }
500
501 #ifndef RGBLIGHT_CUSTOM_DRIVER
502 void rgblight_set(void) {
503   if (rgblight_config.enable) {
504     #ifdef RGBW
505       ws2812_setleds_rgbw(led, RGBLED_NUM);
506     #else
507       ws2812_setleds(led, RGBLED_NUM);
508     #endif
509   } else {
510     for (uint8_t i = 0; i < RGBLED_NUM; i++) {
511       led[i].r = 0;
512       led[i].g = 0;
513       led[i].b = 0;
514     }
515     #ifdef RGBW
516       ws2812_setleds_rgbw(led, RGBLED_NUM);
517     #else
518       ws2812_setleds(led, RGBLED_NUM);
519     #endif
520   }
521 }
522 #endif
523
524 #ifdef RGBLIGHT_ANIMATIONS
525
526 // Animation timer -- AVR Timer3
527 void rgblight_timer_init(void) {
528   // static uint8_t rgblight_timer_is_init = 0;
529   // if (rgblight_timer_is_init) {
530   //   return;
531   // }
532   // rgblight_timer_is_init = 1;
533   // /* Timer 3 setup */
534   // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
535   //       | _BV(CS30); // Clock selelct: clk/1
536   // /* Set TOP value */
537   // uint8_t sreg = SREG;
538   // cli();
539   // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
540   // OCR3AL = RGBLED_TIMER_TOP & 0xff;
541   // SREG = sreg;
542
543   rgblight_timer_enabled = true;
544 }
545 void rgblight_timer_enable(void) {
546   rgblight_timer_enabled = true;
547   dprintf("TIMER3 enabled.\n");
548 }
549 void rgblight_timer_disable(void) {
550   rgblight_timer_enabled = false;
551   dprintf("TIMER3 disabled.\n");
552 }
553 void rgblight_timer_toggle(void) {
554   rgblight_timer_enabled ^= rgblight_timer_enabled;
555   dprintf("TIMER3 toggled.\n");
556 }
557
558 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
559   rgblight_enable();
560   rgblight_mode(1);
561   rgblight_setrgb(r, g, b);
562 }
563
564 void rgblight_task(void) {
565   if (rgblight_timer_enabled) {
566     // mode = 1, static light, do nothing here
567     if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
568       // mode = 2 to 5, breathing mode
569       rgblight_effect_breathing(rgblight_config.mode - 2);
570     } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
571       // mode = 6 to 8, rainbow mood mod
572       rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
573     } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
574       // mode = 9 to 14, rainbow swirl mode
575       rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
576     } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
577       // mode = 15 to 20, snake mode
578       rgblight_effect_snake(rgblight_config.mode - 15);
579     } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
580       // mode = 21 to 23, knight mode
581       rgblight_effect_knight(rgblight_config.mode - 21);
582     } else if (rgblight_config.mode == 24) {
583       // mode = 24, christmas mode
584       rgblight_effect_christmas();
585     } else if (rgblight_config.mode == 35) {
586       // mode = 35, RGB test
587       rgblight_effect_rgbtest();
588     }
589   }
590 }
591
592 // Effects
593 void rgblight_effect_breathing(uint8_t interval) {
594   static uint8_t pos = 0;
595   static uint16_t last_timer = 0;
596   float val;
597
598   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
599     return;
600   }
601   last_timer = timer_read();
602
603
604   // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
605   val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
606   rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
607   pos = (pos + 1) % 256;
608 }
609 void rgblight_effect_rainbow_mood(uint8_t interval) {
610   static uint16_t current_hue = 0;
611   static uint16_t last_timer = 0;
612
613   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
614     return;
615   }
616   last_timer = timer_read();
617   rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
618   current_hue = (current_hue + 1) % 360;
619 }
620 void rgblight_effect_rainbow_swirl(uint8_t interval) {
621   static uint16_t current_hue = 0;
622   static uint16_t last_timer = 0;
623   uint16_t hue;
624   uint8_t i;
625   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
626     return;
627   }
628   last_timer = timer_read();
629   for (i = 0; i < RGBLED_NUM; i++) {
630     hue = (360 / RGBLED_NUM * i + current_hue) % 360;
631     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
632   }
633   rgblight_set();
634
635   if (interval % 2) {
636     current_hue = (current_hue + 1) % 360;
637   } else {
638     if (current_hue - 1 < 0) {
639       current_hue = 359;
640     } else {
641       current_hue = current_hue - 1;
642     }
643   }
644 }
645 void rgblight_effect_snake(uint8_t interval) {
646   static uint8_t pos = 0;
647   static uint16_t last_timer = 0;
648   uint8_t i, j;
649   int8_t k;
650   int8_t increment = 1;
651   if (interval % 2) {
652     increment = -1;
653   }
654   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
655     return;
656   }
657   last_timer = timer_read();
658   for (i = 0; i < RGBLED_NUM; i++) {
659     led[i].r = 0;
660     led[i].g = 0;
661     led[i].b = 0;
662     for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
663       k = pos + j * increment;
664       if (k < 0) {
665         k = k + RGBLED_NUM;
666       }
667       if (i == k) {
668         sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), (LED_TYPE *)&led[i]);
669       }
670     }
671   }
672   rgblight_set();
673   if (increment == 1) {
674     if (pos - 1 < 0) {
675       pos = RGBLED_NUM - 1;
676     } else {
677       pos -= 1;
678     }
679   } else {
680     pos = (pos + 1) % RGBLED_NUM;
681   }
682 }
683 void rgblight_effect_knight(uint8_t interval) {
684   static uint16_t last_timer = 0;
685   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
686     return;
687   }
688   last_timer = timer_read();
689
690   static int8_t low_bound = 0;
691   static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
692   static int8_t increment = 1;
693   uint8_t i, cur;
694
695   // Set all the LEDs to 0
696   for (i = 0; i < RGBLED_NUM; i++) {
697     led[i].r = 0;
698     led[i].g = 0;
699     led[i].b = 0;
700   }
701   // Determine which LEDs should be lit up
702   for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
703     cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
704
705     if (i >= low_bound && i <= high_bound) {
706       sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
707     } else {
708       led[cur].r = 0;
709       led[cur].g = 0;
710       led[cur].b = 0;
711     }
712   }
713   rgblight_set();
714
715   // Move from low_bound to high_bound changing the direction we increment each
716   // time a boundary is hit.
717   low_bound += increment;
718   high_bound += increment;
719
720   if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
721     increment = -increment;
722   }
723 }
724
725
726 void rgblight_effect_christmas(void) {
727   static uint16_t current_offset = 0;
728   static uint16_t last_timer = 0;
729   uint16_t hue;
730   uint8_t i;
731   if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
732     return;
733   }
734   last_timer = timer_read();
735   current_offset = (current_offset + 1) % 2;
736   for (i = 0; i < RGBLED_NUM; i++) {
737     hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
738     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
739   }
740   rgblight_set();
741 }
742
743 void rgblight_effect_rgbtest(void) {
744   static uint8_t pos = 0;
745   static uint16_t last_timer = 0;
746   static uint8_t maxval = 0;
747   uint8_t g; uint8_t r; uint8_t b;
748
749   if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
750     return;
751   }
752
753   if( maxval == 0 ) {
754       LED_TYPE tmp_led;
755       sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
756       maxval = tmp_led.r;
757   }
758   last_timer = timer_read();
759   g = r = b = 0;
760   switch( pos ) {
761     case 0: r = maxval; break;
762     case 1: g = maxval; break;
763     case 2: b = maxval; break;
764   }
765   rgblight_setrgb(r, g, b);
766   pos = (pos + 1) % 3;
767 }
768
769 #endif /* RGBLIGHT_ANIMATIONS */