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