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