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