1 /* Copyright 2016-2017 Yang Liu
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.
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.
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/>.
19 #include <avr/eeprom.h>
20 #include <avr/interrupt.h>
27 #include "led_tables.h"
29 #ifndef RGBLIGHT_LIMIT_VAL
30 #define RGBLIGHT_LIMIT_VAL 255
33 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
34 #define _RGBM_SINGLE_DYNAMIC(sym)
35 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
36 #define _RGBM_MULTI_DYNAMIC(sym)
37 #define _RGBM_TMP_STATIC(sym) RGBLIGHT_MODE_ ## sym,
38 #define _RGBM_TMP_DYNAMIC(sym)
39 static uint8_t static_effect_table [] = {
43 static inline int is_static_effect(uint8_t mode) {
44 return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
47 #define MIN(a,b) (((a)<(b))?(a):(b))
48 #define MAX(a,b) (((a)>(b))?(a):(b))
50 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
51 __attribute__ ((weak))
52 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
55 rgblight_config_t rgblight_config;
57 LED_TYPE led[RGBLED_NUM];
58 bool rgblight_timer_enabled = false;
60 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
61 uint8_t r = 0, g = 0, b = 0, base, color;
63 if (val > RGBLIGHT_LIMIT_VAL) {
64 val=RGBLIGHT_LIMIT_VAL; // limit the val
67 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
72 base = ((255 - sat) * val) >> 8;
73 color = (val - base) * (hue % 60) / 60;
108 r = pgm_read_byte(&CIE1931_CURVE[r]);
109 g = pgm_read_byte(&CIE1931_CURVE[g]);
110 b = pgm_read_byte(&CIE1931_CURVE[b]);
112 setrgb(r, g, b, led1);
115 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
122 uint32_t eeconfig_read_rgblight(void) {
124 return eeprom_read_dword(EECONFIG_RGBLIGHT);
129 void eeconfig_update_rgblight(uint32_t val) {
131 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
134 void eeconfig_update_rgblight_default(void) {
135 //dprintf("eeconfig_update_rgblight_default\n");
136 rgblight_config.enable = 1;
137 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
138 rgblight_config.hue = 0;
139 rgblight_config.sat = 255;
140 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
141 rgblight_config.speed = 0;
142 eeconfig_update_rgblight(rgblight_config.raw);
144 void eeconfig_debug_rgblight(void) {
145 dprintf("rgblight_config eprom\n");
146 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
147 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
148 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
149 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
150 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
151 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
154 void rgblight_init(void) {
155 debug_enable = 1; // Debug ON!
156 dprintf("rgblight_init called.\n");
157 dprintf("rgblight_init start!\n");
158 if (!eeconfig_is_enabled()) {
159 dprintf("rgblight_init eeconfig is not enabled.\n");
161 eeconfig_update_rgblight_default();
163 rgblight_config.raw = eeconfig_read_rgblight();
164 if (!rgblight_config.mode) {
165 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
166 eeconfig_update_rgblight_default();
167 rgblight_config.raw = eeconfig_read_rgblight();
169 eeconfig_debug_rgblight(); // display current eeprom values
171 #ifdef RGBLIGHT_USE_TIMER
172 rgblight_timer_init(); // setup the timer
175 if (rgblight_config.enable) {
176 rgblight_mode_noeeprom(rgblight_config.mode);
180 void rgblight_update_dword(uint32_t dword) {
181 rgblight_config.raw = dword;
182 eeconfig_update_rgblight(rgblight_config.raw);
183 if (rgblight_config.enable)
184 rgblight_mode(rgblight_config.mode);
186 #ifdef RGBLIGHT_USE_TIMER
187 rgblight_timer_disable();
193 void rgblight_increase(void) {
195 if (rgblight_config.mode < RGBLIGHT_MODES) {
196 mode = rgblight_config.mode + 1;
200 void rgblight_decrease(void) {
202 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
203 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
204 mode = rgblight_config.mode - 1;
208 void rgblight_step(void) {
210 mode = rgblight_config.mode + 1;
211 if (mode > RGBLIGHT_MODES) {
216 void rgblight_step_reverse(void) {
218 mode = rgblight_config.mode - 1;
220 mode = RGBLIGHT_MODES;
225 uint32_t rgblight_get_mode(void) {
226 if (!rgblight_config.enable) {
230 return rgblight_config.mode;
233 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
234 if (!rgblight_config.enable) {
237 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
238 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
239 } else if (mode > RGBLIGHT_MODES) {
240 rgblight_config.mode = RGBLIGHT_MODES;
242 rgblight_config.mode = mode;
244 if (write_to_eeprom) {
245 eeconfig_update_rgblight(rgblight_config.raw);
246 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
248 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
250 if( is_static_effect(rgblight_config.mode) ) {
251 #ifdef RGBLIGHT_USE_TIMER
252 rgblight_timer_disable();
255 #ifdef RGBLIGHT_USE_TIMER
256 rgblight_timer_enable();
259 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
262 void rgblight_mode(uint8_t mode) {
263 rgblight_mode_eeprom_helper(mode, true);
266 void rgblight_mode_noeeprom(uint8_t mode) {
267 rgblight_mode_eeprom_helper(mode, false);
271 void rgblight_toggle(void) {
272 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
273 if (rgblight_config.enable) {
281 void rgblight_toggle_noeeprom(void) {
282 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
283 if (rgblight_config.enable) {
284 rgblight_disable_noeeprom();
287 rgblight_enable_noeeprom();
291 void rgblight_enable(void) {
292 rgblight_config.enable = 1;
293 // No need to update EEPROM here. rgblight_mode() will do that, actually
294 //eeconfig_update_rgblight(rgblight_config.raw);
295 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
296 rgblight_mode(rgblight_config.mode);
299 void rgblight_enable_noeeprom(void) {
300 rgblight_config.enable = 1;
301 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
302 rgblight_mode_noeeprom(rgblight_config.mode);
305 void rgblight_disable(void) {
306 rgblight_config.enable = 0;
307 eeconfig_update_rgblight(rgblight_config.raw);
308 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
309 #ifdef RGBLIGHT_USE_TIMER
310 rgblight_timer_disable();
316 void rgblight_disable_noeeprom(void) {
317 rgblight_config.enable = 0;
318 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
319 #ifdef RGBLIGHT_USE_TIMER
320 rgblight_timer_disable();
327 // Deals with the messy details of incrementing an integer
328 uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
329 int16_t new_value = value;
331 return MIN( MAX( new_value, min ), max );
334 uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
335 int16_t new_value = value;
337 return MIN( MAX( new_value, min ), max );
340 void rgblight_increase_hue(void) {
342 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
343 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
345 void rgblight_decrease_hue(void) {
347 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
348 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
350 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
352 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
354 void rgblight_increase_sat(void) {
356 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
359 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
361 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
363 void rgblight_decrease_sat(void) {
365 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
368 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
370 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
372 void rgblight_increase_val(void) {
374 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
375 val = RGBLIGHT_LIMIT_VAL;
377 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
379 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
381 void rgblight_decrease_val(void) {
383 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
386 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
388 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
390 void rgblight_increase_speed(void) {
391 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
392 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
395 void rgblight_decrease_speed(void) {
396 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
397 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
400 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
401 if (rgblight_config.enable) {
403 sethsv(hue, sat, val, &tmp_led);
404 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
405 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
409 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
410 if (rgblight_config.enable) {
411 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
414 sethsv(hue, sat, val, &tmp_led);
415 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
417 // all LEDs in same color
418 if ( 1 == 0 ) { //dummy
420 #ifdef RGBLIGHT_EFFECT_BREATHING
421 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
422 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
423 // breathing mode, ignore the change of val, use in memory value instead
424 val = rgblight_config.val;
427 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
428 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
429 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
430 // rainbow mood, ignore the change of hue
431 hue = rgblight_config.hue;
434 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
435 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
436 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
437 // rainbow swirl, ignore the change of hue
438 hue = rgblight_config.hue;
441 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
442 else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
443 rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
446 int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
447 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
448 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
449 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
450 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
451 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
457 rgblight_config.hue = hue;
458 rgblight_config.sat = sat;
459 rgblight_config.val = val;
460 if (write_to_eeprom) {
461 eeconfig_update_rgblight(rgblight_config.raw);
462 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
464 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
469 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
470 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
473 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
474 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
477 uint16_t rgblight_get_hue(void) {
478 return rgblight_config.hue;
481 uint8_t rgblight_get_sat(void) {
482 return rgblight_config.sat;
485 uint8_t rgblight_get_val(void) {
486 return rgblight_config.val;
489 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
490 if (!rgblight_config.enable) { return; }
492 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
500 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
501 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
509 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
510 if (!rgblight_config.enable) { return; }
513 sethsv(hue, sat, val, &tmp_led);
514 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
517 #ifndef RGBLIGHT_CUSTOM_DRIVER
518 void rgblight_set(void) {
519 if (rgblight_config.enable) {
521 ws2812_setleds_rgbw(led, RGBLED_NUM);
523 ws2812_setleds(led, RGBLED_NUM);
526 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
532 ws2812_setleds_rgbw(led, RGBLED_NUM);
534 ws2812_setleds(led, RGBLED_NUM);
540 #ifdef RGBLIGHT_USE_TIMER
542 // Animation timer -- AVR Timer3
543 void rgblight_timer_init(void) {
544 // static uint8_t rgblight_timer_is_init = 0;
545 // if (rgblight_timer_is_init) {
548 // rgblight_timer_is_init = 1;
549 // /* Timer 3 setup */
550 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
551 // | _BV(CS30); // Clock selelct: clk/1
552 // /* Set TOP value */
553 // uint8_t sreg = SREG;
555 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
556 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
559 rgblight_timer_enabled = true;
561 void rgblight_timer_enable(void) {
562 rgblight_timer_enabled = true;
563 dprintf("TIMER3 enabled.\n");
565 void rgblight_timer_disable(void) {
566 rgblight_timer_enabled = false;
567 dprintf("TIMER3 disabled.\n");
569 void rgblight_timer_toggle(void) {
570 rgblight_timer_enabled ^= rgblight_timer_enabled;
571 dprintf("TIMER3 toggled.\n");
574 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
576 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
577 rgblight_setrgb(r, g, b);
580 void rgblight_task(void) {
581 if (rgblight_timer_enabled) {
582 // static light mode, do nothing here
583 if ( 1 == 0 ) { //dummy
585 #ifdef RGBLIGHT_EFFECT_BREATHING
586 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
587 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
589 rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
592 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
593 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
594 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
596 rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
599 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
600 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
601 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
602 // rainbow swirl mode
603 rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
606 #ifdef RGBLIGHT_EFFECT_SNAKE
607 else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
608 rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
610 rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
613 #ifdef RGBLIGHT_EFFECT_KNIGHT
614 else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
615 rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
617 rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
620 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
621 else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
623 rgblight_effect_christmas();
626 #ifdef RGBLIGHT_EFFECT_RGB_TEST
627 else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
629 rgblight_effect_rgbtest();
632 #ifdef RGBLIGHT_EFFECT_ALTERNATING
633 else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
634 rgblight_effect_alternating();
640 #endif /* RGBLIGHT_USE_TIMER */
643 #ifdef RGBLIGHT_EFFECT_BREATHING
644 __attribute__ ((weak))
645 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
647 void rgblight_effect_breathing(uint8_t interval) {
648 static uint8_t pos = 0;
649 static uint16_t last_timer = 0;
652 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
655 last_timer = timer_read();
657 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
658 val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
659 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
660 pos = (pos + 1) % 256;
664 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
665 __attribute__ ((weak))
666 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
668 void rgblight_effect_rainbow_mood(uint8_t interval) {
669 static uint16_t current_hue = 0;
670 static uint16_t last_timer = 0;
672 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
675 last_timer = timer_read();
676 rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
677 current_hue = (current_hue + 1) % 360;
681 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
682 __attribute__ ((weak))
683 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
685 void rgblight_effect_rainbow_swirl(uint8_t interval) {
686 static uint16_t current_hue = 0;
687 static uint16_t last_timer = 0;
690 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
693 last_timer = timer_read();
694 for (i = 0; i < RGBLED_NUM; i++) {
695 hue = (360 / RGBLED_NUM * i + current_hue) % 360;
696 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
701 current_hue = (current_hue + 1) % 360;
703 if (current_hue - 1 < 0) {
706 current_hue = current_hue - 1;
712 #ifdef RGBLIGHT_EFFECT_SNAKE
713 __attribute__ ((weak))
714 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
716 void rgblight_effect_snake(uint8_t interval) {
717 static uint8_t pos = 0;
718 static uint16_t last_timer = 0;
721 int8_t increment = 1;
725 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
728 last_timer = timer_read();
729 for (i = 0; i < RGBLED_NUM; i++) {
733 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
734 k = pos + j * increment;
739 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]);
744 if (increment == 1) {
746 pos = RGBLED_NUM - 1;
751 pos = (pos + 1) % RGBLED_NUM;
756 #ifdef RGBLIGHT_EFFECT_KNIGHT
757 __attribute__ ((weak))
758 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
760 void rgblight_effect_knight(uint8_t interval) {
761 static uint16_t last_timer = 0;
762 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
765 last_timer = timer_read();
767 static int8_t low_bound = 0;
768 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
769 static int8_t increment = 1;
772 // Set all the LEDs to 0
773 for (i = 0; i < RGBLED_NUM; i++) {
778 // Determine which LEDs should be lit up
779 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
780 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
782 if (i >= low_bound && i <= high_bound) {
783 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
792 // Move from low_bound to high_bound changing the direction we increment each
793 // time a boundary is hit.
794 low_bound += increment;
795 high_bound += increment;
797 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
798 increment = -increment;
803 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
804 void rgblight_effect_christmas(void) {
805 static uint16_t current_offset = 0;
806 static uint16_t last_timer = 0;
809 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
812 last_timer = timer_read();
813 current_offset = (current_offset + 1) % 2;
814 for (i = 0; i < RGBLED_NUM; i++) {
815 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
816 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
822 #ifdef RGBLIGHT_EFFECT_RGB_TEST
823 __attribute__ ((weak))
824 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
826 void rgblight_effect_rgbtest(void) {
827 static uint8_t pos = 0;
828 static uint16_t last_timer = 0;
829 static uint8_t maxval = 0;
830 uint8_t g; uint8_t r; uint8_t b;
832 if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
838 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
841 last_timer = timer_read();
844 case 0: r = maxval; break;
845 case 1: g = maxval; break;
846 case 2: b = maxval; break;
848 rgblight_setrgb(r, g, b);
853 #ifdef RGBLIGHT_EFFECT_ALTERNATING
854 void rgblight_effect_alternating(void){
855 static uint16_t last_timer = 0;
856 static uint16_t pos = 0;
857 if (timer_elapsed(last_timer) < 500) {
860 last_timer = timer_read();
862 for(int i = 0; i<RGBLED_NUM; i++){
863 if(i<RGBLED_NUM/2 && pos){
864 rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, i);
865 }else if (i>=RGBLED_NUM/2 && !pos){
866 rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, i);
868 rgblight_sethsv_at(rgblight_config.hue, rgblight_config.sat, 0, i);