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