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