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