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