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