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