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>
22 #ifdef STM32_EEPROM_ENABLE
25 #include "eeprom_stm32.h"
32 #include "led_tables.h"
34 #ifndef RGBLIGHT_LIMIT_VAL
35 #define RGBLIGHT_LIMIT_VAL 255
38 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
39 #define _RGBM_SINGLE_DYNAMIC(sym)
40 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
41 #define _RGBM_MULTI_DYNAMIC(sym)
42 #define _RGBM_TMP_STATIC(sym) RGBLIGHT_MODE_ ## sym,
43 #define _RGBM_TMP_DYNAMIC(sym)
44 static uint8_t static_effect_table [] = {
48 static inline int is_static_effect(uint8_t mode) {
49 return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
52 #define MIN(a,b) (((a)<(b))?(a):(b))
53 #define MAX(a,b) (((a)>(b))?(a):(b))
55 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
56 __attribute__ ((weak))
57 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
60 rgblight_config_t rgblight_config;
62 LED_TYPE led[RGBLED_NUM];
63 bool rgblight_timer_enabled = false;
65 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
66 uint8_t r = 0, g = 0, b = 0, base, color;
68 if (val > RGBLIGHT_LIMIT_VAL) {
69 val=RGBLIGHT_LIMIT_VAL; // limit the val
72 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
77 base = ((255 - sat) * val) >> 8;
78 color = (val - base) * (hue % 60) / 60;
113 r = pgm_read_byte(&CIE1931_CURVE[r]);
114 g = pgm_read_byte(&CIE1931_CURVE[g]);
115 b = pgm_read_byte(&CIE1931_CURVE[b]);
117 setrgb(r, g, b, led1);
120 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
127 uint32_t eeconfig_read_rgblight(void) {
128 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
129 return eeprom_read_dword(EECONFIG_RGBLIGHT);
134 void eeconfig_update_rgblight(uint32_t val) {
135 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
136 if (eeconfig_read_rgblight() != val) {
137 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
141 void eeconfig_update_rgblight_default(void) {
142 //dprintf("eeconfig_update_rgblight_default\n");
143 rgblight_config.enable = 1;
144 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
145 rgblight_config.hue = 0;
146 rgblight_config.sat = 255;
147 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
148 rgblight_config.speed = 0;
149 eeconfig_update_rgblight(rgblight_config.raw);
151 void eeconfig_debug_rgblight(void) {
152 dprintf("rgblight_config eprom\n");
153 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
154 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
155 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
156 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
157 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
158 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
161 void rgblight_init(void) {
162 debug_enable = 1; // Debug ON!
163 dprintf("rgblight_init called.\n");
164 dprintf("rgblight_init start!\n");
165 if (!eeconfig_is_enabled()) {
166 dprintf("rgblight_init eeconfig is not enabled.\n");
168 eeconfig_update_rgblight_default();
170 rgblight_config.raw = eeconfig_read_rgblight();
171 if (!rgblight_config.mode) {
172 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
173 eeconfig_update_rgblight_default();
174 rgblight_config.raw = eeconfig_read_rgblight();
176 eeconfig_debug_rgblight(); // display current eeprom values
178 #ifdef RGBLIGHT_USE_TIMER
179 rgblight_timer_init(); // setup the timer
182 if (rgblight_config.enable) {
183 rgblight_mode_noeeprom(rgblight_config.mode);
187 void rgblight_update_dword(uint32_t dword) {
188 rgblight_config.raw = dword;
189 eeconfig_update_rgblight(rgblight_config.raw);
190 if (rgblight_config.enable)
191 rgblight_mode(rgblight_config.mode);
193 #ifdef RGBLIGHT_USE_TIMER
194 rgblight_timer_disable();
200 void rgblight_increase(void) {
202 if (rgblight_config.mode < RGBLIGHT_MODES) {
203 mode = rgblight_config.mode + 1;
207 void rgblight_decrease(void) {
209 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
210 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
211 mode = rgblight_config.mode - 1;
215 void rgblight_step_helper(bool write_to_eeprom) {
217 mode = rgblight_config.mode + 1;
218 if (mode > RGBLIGHT_MODES) {
221 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
223 void rgblight_step_noeeprom(void) {
224 rgblight_step_helper(false);
226 void rgblight_step(void) {
227 rgblight_step_helper(true);
229 void rgblight_step_reverse_helper(bool write_to_eeprom) {
231 mode = rgblight_config.mode - 1;
233 mode = RGBLIGHT_MODES;
235 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
237 void rgblight_step_reverse_noeeprom(void) {
238 rgblight_step_reverse_helper(false);
240 void rgblight_step_reverse(void) {
241 rgblight_step_reverse_helper(true);
244 uint8_t rgblight_get_mode(void) {
245 if (!rgblight_config.enable) {
249 return rgblight_config.mode;
252 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
253 if (!rgblight_config.enable) {
256 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
257 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
258 } else if (mode > RGBLIGHT_MODES) {
259 rgblight_config.mode = RGBLIGHT_MODES;
261 rgblight_config.mode = mode;
263 if (write_to_eeprom) {
264 eeconfig_update_rgblight(rgblight_config.raw);
265 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
267 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
269 if( is_static_effect(rgblight_config.mode) ) {
270 #ifdef RGBLIGHT_USE_TIMER
271 rgblight_timer_disable();
274 #ifdef RGBLIGHT_USE_TIMER
275 rgblight_timer_enable();
278 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
281 void rgblight_mode(uint8_t mode) {
282 rgblight_mode_eeprom_helper(mode, true);
285 void rgblight_mode_noeeprom(uint8_t mode) {
286 rgblight_mode_eeprom_helper(mode, false);
290 void rgblight_toggle(void) {
291 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
292 if (rgblight_config.enable) {
300 void rgblight_toggle_noeeprom(void) {
301 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
302 if (rgblight_config.enable) {
303 rgblight_disable_noeeprom();
306 rgblight_enable_noeeprom();
310 void rgblight_enable(void) {
311 rgblight_config.enable = 1;
312 // No need to update EEPROM here. rgblight_mode() will do that, actually
313 //eeconfig_update_rgblight(rgblight_config.raw);
314 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
315 rgblight_mode(rgblight_config.mode);
318 void rgblight_enable_noeeprom(void) {
319 rgblight_config.enable = 1;
320 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
321 rgblight_mode_noeeprom(rgblight_config.mode);
324 void rgblight_disable(void) {
325 rgblight_config.enable = 0;
326 eeconfig_update_rgblight(rgblight_config.raw);
327 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
328 #ifdef RGBLIGHT_USE_TIMER
329 rgblight_timer_disable();
335 void rgblight_disable_noeeprom(void) {
336 rgblight_config.enable = 0;
337 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
338 #ifdef RGBLIGHT_USE_TIMER
339 rgblight_timer_disable();
346 // Deals with the messy details of incrementing an integer
347 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
348 int16_t new_value = value;
350 return MIN( MAX( new_value, min ), max );
353 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
354 int16_t new_value = value;
356 return MIN( MAX( new_value, min ), max );
359 void rgblight_increase_hue_helper(bool write_to_eeprom) {
361 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
362 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
364 void rgblight_increase_hue_noeeprom(void) {
365 rgblight_increase_hue_helper(false);
367 void rgblight_increase_hue(void) {
368 rgblight_increase_hue_helper(true);
370 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
372 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
373 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
375 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
377 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
379 void rgblight_decrease_hue_noeeprom(void) {
380 rgblight_decrease_hue_helper(false);
382 void rgblight_decrease_hue(void) {
383 rgblight_decrease_hue_helper(true);
385 void rgblight_increase_sat_helper(bool write_to_eeprom) {
387 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
390 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
392 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
394 void rgblight_increase_sat_noeeprom(void) {
395 rgblight_increase_sat_helper(false);
397 void rgblight_increase_sat(void) {
398 rgblight_increase_sat_helper(true);
400 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
402 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
405 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
407 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
409 void rgblight_decrease_sat_noeeprom(void) {
410 rgblight_decrease_sat_helper(false);
412 void rgblight_decrease_sat(void) {
413 rgblight_decrease_sat_helper(true);
415 void rgblight_increase_val_helper(bool write_to_eeprom) {
417 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
418 val = RGBLIGHT_LIMIT_VAL;
420 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
422 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
424 void rgblight_increase_val_noeeprom(void) {
425 rgblight_increase_val_helper(false);
427 void rgblight_increase_val(void) {
428 rgblight_increase_val_helper(true);
430 void rgblight_decrease_val_helper(bool write_to_eeprom) {
432 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
435 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
437 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
439 void rgblight_decrease_val_noeeprom(void) {
440 rgblight_decrease_val_helper(false);
442 void rgblight_decrease_val(void) {
443 rgblight_decrease_val_helper(true);
445 void rgblight_increase_speed(void) {
446 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
447 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
450 void rgblight_decrease_speed(void) {
451 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
452 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
455 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
456 if (rgblight_config.enable) {
458 sethsv(hue, sat, val, &tmp_led);
459 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
460 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
464 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
465 if (rgblight_config.enable) {
466 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
469 sethsv(hue, sat, val, &tmp_led);
470 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
472 // all LEDs in same color
473 if ( 1 == 0 ) { //dummy
475 #ifdef RGBLIGHT_EFFECT_BREATHING
476 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
477 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
478 // breathing mode, ignore the change of val, use in memory value instead
479 val = rgblight_config.val;
482 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
483 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
484 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
485 // rainbow mood, ignore the change of hue
486 hue = rgblight_config.hue;
489 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
490 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
491 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
492 // rainbow swirl, ignore the change of hue
493 hue = rgblight_config.hue;
496 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
497 else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
498 rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
501 int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
502 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
503 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
504 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
505 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
506 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
512 rgblight_config.hue = hue;
513 rgblight_config.sat = sat;
514 rgblight_config.val = val;
515 if (write_to_eeprom) {
516 eeconfig_update_rgblight(rgblight_config.raw);
517 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
519 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
524 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
525 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
528 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
529 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
532 uint16_t rgblight_get_hue(void) {
533 return rgblight_config.hue;
536 uint8_t rgblight_get_sat(void) {
537 return rgblight_config.sat;
540 uint8_t rgblight_get_val(void) {
541 return rgblight_config.val;
544 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
545 if (!rgblight_config.enable) { return; }
547 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
555 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
556 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
564 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
565 if (!rgblight_config.enable) { return; }
568 sethsv(hue, sat, val, &tmp_led);
569 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
572 #ifndef RGBLIGHT_CUSTOM_DRIVER
573 void rgblight_set(void) {
574 if (rgblight_config.enable) {
576 ws2812_setleds_rgbw(led, RGBLED_NUM);
578 ws2812_setleds(led, RGBLED_NUM);
581 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
587 ws2812_setleds_rgbw(led, RGBLED_NUM);
589 ws2812_setleds(led, RGBLED_NUM);
595 #ifdef RGBLIGHT_USE_TIMER
597 // Animation timer -- AVR Timer3
598 void rgblight_timer_init(void) {
599 // static uint8_t rgblight_timer_is_init = 0;
600 // if (rgblight_timer_is_init) {
603 // rgblight_timer_is_init = 1;
604 // /* Timer 3 setup */
605 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
606 // | _BV(CS30); // Clock selelct: clk/1
607 // /* Set TOP value */
608 // uint8_t sreg = SREG;
610 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
611 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
614 rgblight_timer_enabled = true;
616 void rgblight_timer_enable(void) {
617 rgblight_timer_enabled = true;
618 dprintf("TIMER3 enabled.\n");
620 void rgblight_timer_disable(void) {
621 rgblight_timer_enabled = false;
622 dprintf("TIMER3 disabled.\n");
624 void rgblight_timer_toggle(void) {
625 rgblight_timer_enabled ^= rgblight_timer_enabled;
626 dprintf("TIMER3 toggled.\n");
629 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
631 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
632 rgblight_setrgb(r, g, b);
635 void rgblight_task(void) {
636 if (rgblight_timer_enabled) {
637 // static light mode, do nothing here
638 if ( 1 == 0 ) { //dummy
640 #ifdef RGBLIGHT_EFFECT_BREATHING
641 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
642 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
644 rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
647 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
648 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
649 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
651 rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
654 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
655 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
656 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
657 // rainbow swirl mode
658 rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
661 #ifdef RGBLIGHT_EFFECT_SNAKE
662 else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
663 rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
665 rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
668 #ifdef RGBLIGHT_EFFECT_KNIGHT
669 else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
670 rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
672 rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
675 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
676 else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
678 rgblight_effect_christmas();
681 #ifdef RGBLIGHT_EFFECT_RGB_TEST
682 else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
684 rgblight_effect_rgbtest();
687 #ifdef RGBLIGHT_EFFECT_ALTERNATING
688 else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
689 rgblight_effect_alternating();
695 #endif /* RGBLIGHT_USE_TIMER */
698 #ifdef RGBLIGHT_EFFECT_BREATHING
699 __attribute__ ((weak))
700 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
702 void rgblight_effect_breathing(uint8_t interval) {
703 static uint8_t pos = 0;
704 static uint16_t last_timer = 0;
707 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
710 last_timer = timer_read();
712 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
713 val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
714 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
715 pos = (pos + 1) % 256;
719 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
720 __attribute__ ((weak))
721 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
723 void rgblight_effect_rainbow_mood(uint8_t interval) {
724 static uint16_t current_hue = 0;
725 static uint16_t last_timer = 0;
727 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
730 last_timer = timer_read();
731 rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
732 current_hue = (current_hue + 1) % 360;
736 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
737 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
738 #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
741 __attribute__ ((weak))
742 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
744 void rgblight_effect_rainbow_swirl(uint8_t interval) {
745 static uint16_t current_hue = 0;
746 static uint16_t last_timer = 0;
749 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
752 last_timer = timer_read();
753 for (i = 0; i < RGBLED_NUM; i++) {
754 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
755 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
760 current_hue = (current_hue + 1) % 360;
762 if (current_hue - 1 < 0) {
765 current_hue = current_hue - 1;
771 #ifdef RGBLIGHT_EFFECT_SNAKE
772 __attribute__ ((weak))
773 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
775 void rgblight_effect_snake(uint8_t interval) {
776 static uint8_t pos = 0;
777 static uint16_t last_timer = 0;
780 int8_t increment = 1;
784 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
787 last_timer = timer_read();
788 for (i = 0; i < RGBLED_NUM; i++) {
792 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
793 k = pos + j * increment;
798 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]);
803 if (increment == 1) {
805 pos = RGBLED_NUM - 1;
810 pos = (pos + 1) % RGBLED_NUM;
815 #ifdef RGBLIGHT_EFFECT_KNIGHT
816 __attribute__ ((weak))
817 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
819 void rgblight_effect_knight(uint8_t interval) {
820 static uint16_t last_timer = 0;
821 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
824 last_timer = timer_read();
826 static int8_t low_bound = 0;
827 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
828 static int8_t increment = 1;
831 // Set all the LEDs to 0
832 for (i = 0; i < RGBLED_NUM; i++) {
837 // Determine which LEDs should be lit up
838 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
839 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
841 if (i >= low_bound && i <= high_bound) {
842 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
851 // Move from low_bound to high_bound changing the direction we increment each
852 // time a boundary is hit.
853 low_bound += increment;
854 high_bound += increment;
856 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
857 increment = -increment;
862 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
863 void rgblight_effect_christmas(void) {
864 static uint16_t current_offset = 0;
865 static uint16_t last_timer = 0;
868 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
871 last_timer = timer_read();
872 current_offset = (current_offset + 1) % 2;
873 for (i = 0; i < RGBLED_NUM; i++) {
874 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
875 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
881 #ifdef RGBLIGHT_EFFECT_RGB_TEST
882 __attribute__ ((weak))
883 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
885 void rgblight_effect_rgbtest(void) {
886 static uint8_t pos = 0;
887 static uint16_t last_timer = 0;
888 static uint8_t maxval = 0;
889 uint8_t g; uint8_t r; uint8_t b;
891 if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
897 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
900 last_timer = timer_read();
903 case 0: r = maxval; break;
904 case 1: g = maxval; break;
905 case 2: b = maxval; break;
907 rgblight_setrgb(r, g, b);
912 #ifdef RGBLIGHT_EFFECT_ALTERNATING
913 void rgblight_effect_alternating(void){
914 static uint16_t last_timer = 0;
915 static uint16_t pos = 0;
916 if (timer_elapsed(last_timer) < 500) {
919 last_timer = timer_read();
921 for(int i = 0; i<RGBLED_NUM; i++){
922 if(i<RGBLED_NUM/2 && pos){
923 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
924 }else if (i>=RGBLED_NUM/2 && !pos){
925 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
927 sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);