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