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