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 if (eeconfig_read_rgblight() != val) {
132 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
136 void eeconfig_update_rgblight_default(void) {
137 //dprintf("eeconfig_update_rgblight_default\n");
138 rgblight_config.enable = 1;
139 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
140 rgblight_config.hue = 0;
141 rgblight_config.sat = 255;
142 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
143 rgblight_config.speed = 0;
144 eeconfig_update_rgblight(rgblight_config.raw);
146 void eeconfig_debug_rgblight(void) {
147 dprintf("rgblight_config eprom\n");
148 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
149 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
150 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
151 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
152 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
153 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
156 void rgblight_init(void) {
157 debug_enable = 1; // Debug ON!
158 dprintf("rgblight_init called.\n");
159 dprintf("rgblight_init start!\n");
160 if (!eeconfig_is_enabled()) {
161 dprintf("rgblight_init eeconfig is not enabled.\n");
163 eeconfig_update_rgblight_default();
165 rgblight_config.raw = eeconfig_read_rgblight();
166 if (!rgblight_config.mode) {
167 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
168 eeconfig_update_rgblight_default();
169 rgblight_config.raw = eeconfig_read_rgblight();
171 eeconfig_debug_rgblight(); // display current eeprom values
173 #ifdef RGBLIGHT_USE_TIMER
174 rgblight_timer_init(); // setup the timer
177 if (rgblight_config.enable) {
178 rgblight_mode_noeeprom(rgblight_config.mode);
182 void rgblight_update_dword(uint32_t dword) {
183 rgblight_config.raw = dword;
184 eeconfig_update_rgblight(rgblight_config.raw);
185 if (rgblight_config.enable)
186 rgblight_mode(rgblight_config.mode);
188 #ifdef RGBLIGHT_USE_TIMER
189 rgblight_timer_disable();
195 void rgblight_increase(void) {
197 if (rgblight_config.mode < RGBLIGHT_MODES) {
198 mode = rgblight_config.mode + 1;
202 void rgblight_decrease(void) {
204 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
205 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
206 mode = rgblight_config.mode - 1;
210 void rgblight_step_helper(bool write_to_eeprom) {
212 mode = rgblight_config.mode + 1;
213 if (mode > RGBLIGHT_MODES) {
216 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
218 void rgblight_step_noeeprom(void) {
219 rgblight_step_helper(false);
221 void rgblight_step(void) {
222 rgblight_step_helper(true);
224 void rgblight_step_reverse_helper(bool write_to_eeprom) {
226 mode = rgblight_config.mode - 1;
228 mode = RGBLIGHT_MODES;
230 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
232 void rgblight_step_reverse_noeeprom(void) {
233 rgblight_step_reverse_helper(false);
235 void rgblight_step_reverse(void) {
236 rgblight_step_reverse_helper(true);
239 uint8_t rgblight_get_mode(void) {
240 if (!rgblight_config.enable) {
244 return rgblight_config.mode;
247 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
248 if (!rgblight_config.enable) {
251 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
252 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
253 } else if (mode > RGBLIGHT_MODES) {
254 rgblight_config.mode = RGBLIGHT_MODES;
256 rgblight_config.mode = mode;
258 if (write_to_eeprom) {
259 eeconfig_update_rgblight(rgblight_config.raw);
260 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
262 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
264 if( is_static_effect(rgblight_config.mode) ) {
265 #ifdef RGBLIGHT_USE_TIMER
266 rgblight_timer_disable();
269 #ifdef RGBLIGHT_USE_TIMER
270 rgblight_timer_enable();
273 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
276 void rgblight_mode(uint8_t mode) {
277 rgblight_mode_eeprom_helper(mode, true);
280 void rgblight_mode_noeeprom(uint8_t mode) {
281 rgblight_mode_eeprom_helper(mode, false);
285 void rgblight_toggle(void) {
286 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
287 if (rgblight_config.enable) {
295 void rgblight_toggle_noeeprom(void) {
296 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
297 if (rgblight_config.enable) {
298 rgblight_disable_noeeprom();
301 rgblight_enable_noeeprom();
305 void rgblight_enable(void) {
306 rgblight_config.enable = 1;
307 // No need to update EEPROM here. rgblight_mode() will do that, actually
308 //eeconfig_update_rgblight(rgblight_config.raw);
309 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
310 rgblight_mode(rgblight_config.mode);
313 void rgblight_enable_noeeprom(void) {
314 rgblight_config.enable = 1;
315 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
316 rgblight_mode_noeeprom(rgblight_config.mode);
319 void rgblight_disable(void) {
320 rgblight_config.enable = 0;
321 eeconfig_update_rgblight(rgblight_config.raw);
322 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
323 #ifdef RGBLIGHT_USE_TIMER
324 rgblight_timer_disable();
330 void rgblight_disable_noeeprom(void) {
331 rgblight_config.enable = 0;
332 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
333 #ifdef RGBLIGHT_USE_TIMER
334 rgblight_timer_disable();
341 // Deals with the messy details of incrementing an integer
342 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
343 int16_t new_value = value;
345 return MIN( MAX( new_value, min ), max );
348 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
349 int16_t new_value = value;
351 return MIN( MAX( new_value, min ), max );
354 void rgblight_increase_hue_helper(bool write_to_eeprom) {
356 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
357 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
359 void rgblight_increase_hue_noeeprom(void) {
360 rgblight_increase_hue_helper(false);
362 void rgblight_increase_hue(void) {
363 rgblight_increase_hue_helper(true);
365 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
367 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
368 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
370 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
372 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
374 void rgblight_decrease_hue_noeeprom(void) {
375 rgblight_decrease_hue_helper(false);
377 void rgblight_decrease_hue(void) {
378 rgblight_decrease_hue_helper(true);
380 void rgblight_increase_sat_helper(bool write_to_eeprom) {
382 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
385 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
387 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
389 void rgblight_increase_sat_noeeprom(void) {
390 rgblight_increase_sat_helper(false);
392 void rgblight_increase_sat(void) {
393 rgblight_increase_sat_helper(true);
395 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
397 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
400 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
402 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
404 void rgblight_decrease_sat_noeeprom(void) {
405 rgblight_decrease_sat_helper(false);
407 void rgblight_decrease_sat(void) {
408 rgblight_decrease_sat_helper(true);
410 void rgblight_increase_val_helper(bool write_to_eeprom) {
412 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
413 val = RGBLIGHT_LIMIT_VAL;
415 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
417 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
419 void rgblight_increase_val_noeeprom(void) {
420 rgblight_increase_val_helper(false);
422 void rgblight_increase_val(void) {
423 rgblight_increase_val_helper(true);
425 void rgblight_decrease_val_helper(bool write_to_eeprom) {
427 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
430 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
432 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
434 void rgblight_decrease_val_noeeprom(void) {
435 rgblight_decrease_val_helper(false);
437 void rgblight_decrease_val(void) {
438 rgblight_decrease_val_helper(true);
440 void rgblight_increase_speed(void) {
441 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
442 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
445 void rgblight_decrease_speed(void) {
446 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
447 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
450 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
451 if (rgblight_config.enable) {
453 sethsv(hue, sat, val, &tmp_led);
454 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
455 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
459 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
460 if (rgblight_config.enable) {
461 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
464 sethsv(hue, sat, val, &tmp_led);
465 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
467 // all LEDs in same color
468 if ( 1 == 0 ) { //dummy
470 #ifdef RGBLIGHT_EFFECT_BREATHING
471 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
472 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
473 // breathing mode, ignore the change of val, use in memory value instead
474 val = rgblight_config.val;
477 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
478 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
479 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
480 // rainbow mood, ignore the change of hue
481 hue = rgblight_config.hue;
484 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
485 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
486 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
487 // rainbow swirl, ignore the change of hue
488 hue = rgblight_config.hue;
491 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
492 else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
493 rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
496 int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
497 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
498 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
499 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
500 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
501 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
507 rgblight_config.hue = hue;
508 rgblight_config.sat = sat;
509 rgblight_config.val = val;
510 if (write_to_eeprom) {
511 eeconfig_update_rgblight(rgblight_config.raw);
512 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
514 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
519 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
520 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
523 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
524 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
527 uint16_t rgblight_get_hue(void) {
528 return rgblight_config.hue;
531 uint8_t rgblight_get_sat(void) {
532 return rgblight_config.sat;
535 uint8_t rgblight_get_val(void) {
536 return rgblight_config.val;
539 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
540 if (!rgblight_config.enable) { return; }
542 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
550 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
551 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
559 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
560 if (!rgblight_config.enable) { return; }
563 sethsv(hue, sat, val, &tmp_led);
564 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
567 #ifndef RGBLIGHT_CUSTOM_DRIVER
568 void rgblight_set(void) {
569 if (rgblight_config.enable) {
571 ws2812_setleds_rgbw(led, RGBLED_NUM);
573 ws2812_setleds(led, RGBLED_NUM);
576 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
582 ws2812_setleds_rgbw(led, RGBLED_NUM);
584 ws2812_setleds(led, RGBLED_NUM);
590 #ifdef RGBLIGHT_USE_TIMER
592 // Animation timer -- AVR Timer3
593 void rgblight_timer_init(void) {
594 // static uint8_t rgblight_timer_is_init = 0;
595 // if (rgblight_timer_is_init) {
598 // rgblight_timer_is_init = 1;
599 // /* Timer 3 setup */
600 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
601 // | _BV(CS30); // Clock selelct: clk/1
602 // /* Set TOP value */
603 // uint8_t sreg = SREG;
605 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
606 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
609 rgblight_timer_enabled = true;
611 void rgblight_timer_enable(void) {
612 rgblight_timer_enabled = true;
613 dprintf("TIMER3 enabled.\n");
615 void rgblight_timer_disable(void) {
616 rgblight_timer_enabled = false;
617 dprintf("TIMER3 disabled.\n");
619 void rgblight_timer_toggle(void) {
620 rgblight_timer_enabled ^= rgblight_timer_enabled;
621 dprintf("TIMER3 toggled.\n");
624 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
626 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
627 rgblight_setrgb(r, g, b);
630 void rgblight_task(void) {
631 if (rgblight_timer_enabled) {
632 // static light mode, do nothing here
633 if ( 1 == 0 ) { //dummy
635 #ifdef RGBLIGHT_EFFECT_BREATHING
636 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
637 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
639 rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
642 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
643 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
644 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
646 rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
649 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
650 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
651 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
652 // rainbow swirl mode
653 rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
656 #ifdef RGBLIGHT_EFFECT_SNAKE
657 else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
658 rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
660 rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
663 #ifdef RGBLIGHT_EFFECT_KNIGHT
664 else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
665 rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
667 rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
670 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
671 else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
673 rgblight_effect_christmas();
676 #ifdef RGBLIGHT_EFFECT_RGB_TEST
677 else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
679 rgblight_effect_rgbtest();
682 #ifdef RGBLIGHT_EFFECT_ALTERNATING
683 else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
684 rgblight_effect_alternating();
690 #endif /* RGBLIGHT_USE_TIMER */
693 #ifdef RGBLIGHT_EFFECT_BREATHING
694 __attribute__ ((weak))
695 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
697 void rgblight_effect_breathing(uint8_t interval) {
698 static uint8_t pos = 0;
699 static uint16_t last_timer = 0;
702 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
705 last_timer = timer_read();
707 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
708 val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
709 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
710 pos = (pos + 1) % 256;
714 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
715 __attribute__ ((weak))
716 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
718 void rgblight_effect_rainbow_mood(uint8_t interval) {
719 static uint16_t current_hue = 0;
720 static uint16_t last_timer = 0;
722 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
725 last_timer = timer_read();
726 rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
727 current_hue = (current_hue + 1) % 360;
731 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
732 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
733 #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
736 __attribute__ ((weak))
737 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
739 void rgblight_effect_rainbow_swirl(uint8_t interval) {
740 static uint16_t current_hue = 0;
741 static uint16_t last_timer = 0;
744 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
747 last_timer = timer_read();
748 for (i = 0; i < RGBLED_NUM; i++) {
749 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
750 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
755 current_hue = (current_hue + 1) % 360;
757 if (current_hue - 1 < 0) {
760 current_hue = current_hue - 1;
766 #ifdef RGBLIGHT_EFFECT_SNAKE
767 __attribute__ ((weak))
768 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
770 void rgblight_effect_snake(uint8_t interval) {
771 static uint8_t pos = 0;
772 static uint16_t last_timer = 0;
775 int8_t increment = 1;
779 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
782 last_timer = timer_read();
783 for (i = 0; i < RGBLED_NUM; i++) {
787 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
788 k = pos + j * increment;
793 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]);
798 if (increment == 1) {
800 pos = RGBLED_NUM - 1;
805 pos = (pos + 1) % RGBLED_NUM;
810 #ifdef RGBLIGHT_EFFECT_KNIGHT
811 __attribute__ ((weak))
812 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
814 void rgblight_effect_knight(uint8_t interval) {
815 static uint16_t last_timer = 0;
816 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
819 last_timer = timer_read();
821 static int8_t low_bound = 0;
822 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
823 static int8_t increment = 1;
826 // Set all the LEDs to 0
827 for (i = 0; i < RGBLED_NUM; i++) {
832 // Determine which LEDs should be lit up
833 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
834 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
836 if (i >= low_bound && i <= high_bound) {
837 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
846 // Move from low_bound to high_bound changing the direction we increment each
847 // time a boundary is hit.
848 low_bound += increment;
849 high_bound += increment;
851 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
852 increment = -increment;
857 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
858 void rgblight_effect_christmas(void) {
859 static uint16_t current_offset = 0;
860 static uint16_t last_timer = 0;
863 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
866 last_timer = timer_read();
867 current_offset = (current_offset + 1) % 2;
868 for (i = 0; i < RGBLED_NUM; i++) {
869 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
870 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
876 #ifdef RGBLIGHT_EFFECT_RGB_TEST
877 __attribute__ ((weak))
878 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
880 void rgblight_effect_rgbtest(void) {
881 static uint8_t pos = 0;
882 static uint16_t last_timer = 0;
883 static uint8_t maxval = 0;
884 uint8_t g; uint8_t r; uint8_t b;
886 if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
892 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
895 last_timer = timer_read();
898 case 0: r = maxval; break;
899 case 1: g = maxval; break;
900 case 2: b = maxval; break;
902 rgblight_setrgb(r, g, b);
907 #ifdef RGBLIGHT_EFFECT_ALTERNATING
908 void rgblight_effect_alternating(void){
909 static uint16_t last_timer = 0;
910 static uint16_t pos = 0;
911 if (timer_elapsed(last_timer) < 500) {
914 last_timer = timer_read();
916 for(int i = 0; i<RGBLED_NUM; i++){
917 if(i<RGBLED_NUM/2 && pos){
918 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
919 }else if (i>=RGBLED_NUM/2 && !pos){
920 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
922 sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);