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 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
35 #define _RGBM_SINGLE_DYNAMIC(sym)
36 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
37 #define _RGBM_MULTI_DYNAMIC(sym)
38 #define _RGBM_TMP_STATIC(sym) RGBLIGHT_MODE_ ## sym,
39 #define _RGBM_TMP_DYNAMIC(sym)
40 static uint8_t static_effect_table [] = {
44 static inline int is_static_effect(uint8_t mode) {
45 return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
48 #define MIN(a,b) (((a)<(b))?(a):(b))
49 #define MAX(a,b) (((a)>(b))?(a):(b))
51 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
52 __attribute__ ((weak))
53 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
56 rgblight_config_t rgblight_config;
57 bool is_rgblight_initialized = false;
59 LED_TYPE led[RGBLED_NUM];
60 bool rgblight_timer_enabled = false;
62 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
63 uint8_t r = 0, g = 0, b = 0, base, color;
65 if (val > RGBLIGHT_LIMIT_VAL) {
66 val=RGBLIGHT_LIMIT_VAL; // limit the val
69 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
74 base = ((255 - sat) * val) >> 8;
75 color = (val - base) * (hue % 60) / 60;
110 r = pgm_read_byte(&CIE1931_CURVE[r]);
111 g = pgm_read_byte(&CIE1931_CURVE[g]);
112 b = pgm_read_byte(&CIE1931_CURVE[b]);
114 setrgb(r, g, b, led1);
117 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
123 void rgblight_check_config(void) {
124 /* Add some out of bound checks for RGB light config */
126 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
127 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
129 else if (rgblight_config.mode > RGBLIGHT_MODES) {
130 rgblight_config.mode = RGBLIGHT_MODES;
133 if (rgblight_config.hue < 0) {
134 rgblight_config.hue = 0;
135 } else if (rgblight_config.hue > 360) {
136 rgblight_config.hue %= 360;
139 if (rgblight_config.sat < 0) {
140 rgblight_config.sat = 0;
141 } else if (rgblight_config.sat > 255) {
142 rgblight_config.sat = 255;
145 if (rgblight_config.val < 0) {
146 rgblight_config.val = 0;
147 } else if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
148 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
153 uint32_t eeconfig_read_rgblight(void) {
154 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
155 return eeprom_read_dword(EECONFIG_RGBLIGHT);
161 void eeconfig_update_rgblight(uint32_t val) {
162 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
163 rgblight_check_config();
164 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
168 void eeconfig_update_rgblight_default(void) {
169 //dprintf("eeconfig_update_rgblight_default\n");
170 rgblight_config.enable = 1;
171 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
172 rgblight_config.hue = 0;
173 rgblight_config.sat = 255;
174 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
175 rgblight_config.speed = 0;
176 eeconfig_update_rgblight(rgblight_config.raw);
179 void eeconfig_debug_rgblight(void) {
180 dprintf("rgblight_config eprom\n");
181 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
182 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
183 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
184 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
185 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
186 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
189 void rgblight_init(void) {
190 /* if already initialized, don't do it again.
191 If you must do it again, extern this and set to false, first.
192 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
193 if (is_rgblight_initialized) { return; }
195 debug_enable = 1; // Debug ON!
196 dprintf("rgblight_init called.\n");
197 dprintf("rgblight_init start!\n");
198 if (!eeconfig_is_enabled()) {
199 dprintf("rgblight_init eeconfig is not enabled.\n");
201 eeconfig_update_rgblight_default();
203 rgblight_config.raw = eeconfig_read_rgblight();
204 if (!rgblight_config.mode) {
205 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
206 eeconfig_update_rgblight_default();
207 rgblight_config.raw = eeconfig_read_rgblight();
209 rgblight_check_config();
211 eeconfig_debug_rgblight(); // display current eeprom values
213 #ifdef RGBLIGHT_USE_TIMER
214 rgblight_timer_init(); // setup the timer
217 if (rgblight_config.enable) {
218 rgblight_mode_noeeprom(rgblight_config.mode);
221 is_rgblight_initialized = true;
225 void rgblight_update_dword(uint32_t dword) {
226 rgblight_config.raw = dword;
227 eeconfig_update_rgblight(rgblight_config.raw);
228 if (rgblight_config.enable)
229 rgblight_mode(rgblight_config.mode);
231 #ifdef RGBLIGHT_USE_TIMER
232 rgblight_timer_disable();
238 void rgblight_increase(void) {
240 if (rgblight_config.mode < RGBLIGHT_MODES) {
241 mode = rgblight_config.mode + 1;
245 void rgblight_decrease(void) {
247 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
248 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
249 mode = rgblight_config.mode - 1;
253 void rgblight_step_helper(bool write_to_eeprom) {
255 mode = rgblight_config.mode + 1;
256 if (mode > RGBLIGHT_MODES) {
259 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
261 void rgblight_step_noeeprom(void) {
262 rgblight_step_helper(false);
264 void rgblight_step(void) {
265 rgblight_step_helper(true);
267 void rgblight_step_reverse_helper(bool write_to_eeprom) {
269 mode = rgblight_config.mode - 1;
271 mode = RGBLIGHT_MODES;
273 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
275 void rgblight_step_reverse_noeeprom(void) {
276 rgblight_step_reverse_helper(false);
278 void rgblight_step_reverse(void) {
279 rgblight_step_reverse_helper(true);
282 uint8_t rgblight_get_mode(void) {
283 if (!rgblight_config.enable) {
287 return rgblight_config.mode;
290 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
291 if (!rgblight_config.enable) {
294 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
295 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
296 } else if (mode > RGBLIGHT_MODES) {
297 rgblight_config.mode = RGBLIGHT_MODES;
299 rgblight_config.mode = mode;
301 if (write_to_eeprom) {
302 eeconfig_update_rgblight(rgblight_config.raw);
303 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
305 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
307 if( is_static_effect(rgblight_config.mode) ) {
308 #ifdef RGBLIGHT_USE_TIMER
309 rgblight_timer_disable();
312 #ifdef RGBLIGHT_USE_TIMER
313 rgblight_timer_enable();
316 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
319 void rgblight_mode(uint8_t mode) {
320 rgblight_mode_eeprom_helper(mode, true);
323 void rgblight_mode_noeeprom(uint8_t mode) {
324 rgblight_mode_eeprom_helper(mode, false);
328 void rgblight_toggle(void) {
329 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
330 if (rgblight_config.enable) {
338 void rgblight_toggle_noeeprom(void) {
339 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
340 if (rgblight_config.enable) {
341 rgblight_disable_noeeprom();
344 rgblight_enable_noeeprom();
348 void rgblight_enable(void) {
349 rgblight_config.enable = 1;
350 // No need to update EEPROM here. rgblight_mode() will do that, actually
351 //eeconfig_update_rgblight(rgblight_config.raw);
352 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
353 rgblight_mode(rgblight_config.mode);
356 void rgblight_enable_noeeprom(void) {
357 rgblight_config.enable = 1;
358 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
359 rgblight_mode_noeeprom(rgblight_config.mode);
362 void rgblight_disable(void) {
363 rgblight_config.enable = 0;
364 eeconfig_update_rgblight(rgblight_config.raw);
365 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
366 #ifdef RGBLIGHT_USE_TIMER
367 rgblight_timer_disable();
373 void rgblight_disable_noeeprom(void) {
374 rgblight_config.enable = 0;
375 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
376 #ifdef RGBLIGHT_USE_TIMER
377 rgblight_timer_disable();
384 // Deals with the messy details of incrementing an integer
385 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
386 int16_t new_value = value;
388 return MIN( MAX( new_value, min ), max );
391 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
392 int16_t new_value = value;
394 return MIN( MAX( new_value, min ), max );
397 void rgblight_increase_hue_helper(bool write_to_eeprom) {
399 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
400 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
402 void rgblight_increase_hue_noeeprom(void) {
403 rgblight_increase_hue_helper(false);
405 void rgblight_increase_hue(void) {
406 rgblight_increase_hue_helper(true);
408 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
410 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
411 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
413 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
415 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
417 void rgblight_decrease_hue_noeeprom(void) {
418 rgblight_decrease_hue_helper(false);
420 void rgblight_decrease_hue(void) {
421 rgblight_decrease_hue_helper(true);
423 void rgblight_increase_sat_helper(bool write_to_eeprom) {
425 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
428 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
430 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
432 void rgblight_increase_sat_noeeprom(void) {
433 rgblight_increase_sat_helper(false);
435 void rgblight_increase_sat(void) {
436 rgblight_increase_sat_helper(true);
438 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
440 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
443 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
445 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
447 void rgblight_decrease_sat_noeeprom(void) {
448 rgblight_decrease_sat_helper(false);
450 void rgblight_decrease_sat(void) {
451 rgblight_decrease_sat_helper(true);
453 void rgblight_increase_val_helper(bool write_to_eeprom) {
455 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
456 val = RGBLIGHT_LIMIT_VAL;
458 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
460 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
462 void rgblight_increase_val_noeeprom(void) {
463 rgblight_increase_val_helper(false);
465 void rgblight_increase_val(void) {
466 rgblight_increase_val_helper(true);
468 void rgblight_decrease_val_helper(bool write_to_eeprom) {
470 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
473 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
475 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
477 void rgblight_decrease_val_noeeprom(void) {
478 rgblight_decrease_val_helper(false);
480 void rgblight_decrease_val(void) {
481 rgblight_decrease_val_helper(true);
483 void rgblight_increase_speed(void) {
484 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
485 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
488 void rgblight_decrease_speed(void) {
489 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
490 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
493 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
494 if (rgblight_config.enable) {
496 sethsv(hue, sat, val, &tmp_led);
497 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
498 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
502 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
503 if (rgblight_config.enable) {
504 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
507 sethsv(hue, sat, val, &tmp_led);
508 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
510 // all LEDs in same color
511 if ( 1 == 0 ) { //dummy
513 #ifdef RGBLIGHT_EFFECT_BREATHING
514 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
515 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
516 // breathing mode, ignore the change of val, use in memory value instead
517 val = rgblight_config.val;
520 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
521 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
522 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
523 // rainbow mood, ignore the change of hue
524 hue = rgblight_config.hue;
527 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
528 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
529 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
530 // rainbow swirl, ignore the change of hue
531 hue = rgblight_config.hue;
534 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
535 else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
536 rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
539 int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
540 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
541 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
542 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
543 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
544 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
550 rgblight_config.hue = hue;
551 rgblight_config.sat = sat;
552 rgblight_config.val = val;
553 if (write_to_eeprom) {
554 eeconfig_update_rgblight(rgblight_config.raw);
555 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
557 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
562 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
563 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
566 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
567 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
570 uint16_t rgblight_get_hue(void) {
571 return rgblight_config.hue;
574 uint8_t rgblight_get_sat(void) {
575 return rgblight_config.sat;
578 uint8_t rgblight_get_val(void) {
579 return rgblight_config.val;
582 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
583 if (!rgblight_config.enable) { return; }
585 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
593 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
594 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
602 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
603 if (!rgblight_config.enable) { return; }
606 sethsv(hue, sat, val, &tmp_led);
607 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
610 void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
611 if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) { return; }
613 for (uint8_t i = start; i < end; i++) {
622 void rgblight_sethsv_range(uint16_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
623 if (!rgblight_config.enable) { return; }
626 sethsv(hue, sat, val, &tmp_led);
627 rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
630 void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) {
631 rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2);
634 void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b) {
635 rgblight_setrgb_range(r, g, b, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
638 void rgblight_sethsv_master(uint16_t hue, uint8_t sat, uint8_t val) {
639 rgblight_sethsv_range(hue, sat, val, 0, (uint8_t) RGBLED_NUM/2);
642 void rgblight_sethsv_slave(uint16_t hue, uint8_t sat, uint8_t val) {
643 rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
646 #ifndef RGBLIGHT_CUSTOM_DRIVER
647 void rgblight_set(void) {
648 if (rgblight_config.enable) {
650 ws2812_setleds_rgbw(led, RGBLED_NUM);
652 ws2812_setleds(led, RGBLED_NUM);
655 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
661 ws2812_setleds_rgbw(led, RGBLED_NUM);
663 ws2812_setleds(led, RGBLED_NUM);
669 #ifdef RGBLIGHT_USE_TIMER
671 // Animation timer -- AVR Timer3
672 void rgblight_timer_init(void) {
673 // static uint8_t rgblight_timer_is_init = 0;
674 // if (rgblight_timer_is_init) {
677 // rgblight_timer_is_init = 1;
678 // /* Timer 3 setup */
679 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
680 // | _BV(CS30); // Clock selelct: clk/1
681 // /* Set TOP value */
682 // uint8_t sreg = SREG;
684 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
685 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
688 rgblight_timer_enabled = true;
690 void rgblight_timer_enable(void) {
691 rgblight_timer_enabled = true;
692 dprintf("TIMER3 enabled.\n");
694 void rgblight_timer_disable(void) {
695 rgblight_timer_enabled = false;
696 dprintf("TIMER3 disabled.\n");
698 void rgblight_timer_toggle(void) {
699 rgblight_timer_enabled ^= rgblight_timer_enabled;
700 dprintf("TIMER3 toggled.\n");
703 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
705 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
706 rgblight_setrgb(r, g, b);
709 void rgblight_task(void) {
710 if (rgblight_timer_enabled) {
711 // static light mode, do nothing here
712 if ( 1 == 0 ) { //dummy
714 #ifdef RGBLIGHT_EFFECT_BREATHING
715 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
716 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
718 rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
721 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
722 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
723 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
725 rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
728 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
729 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
730 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
731 // rainbow swirl mode
732 rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
735 #ifdef RGBLIGHT_EFFECT_SNAKE
736 else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
737 rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
739 rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
742 #ifdef RGBLIGHT_EFFECT_KNIGHT
743 else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
744 rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
746 rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
749 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
750 else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
752 rgblight_effect_christmas();
755 #ifdef RGBLIGHT_EFFECT_RGB_TEST
756 else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
758 rgblight_effect_rgbtest();
761 #ifdef RGBLIGHT_EFFECT_ALTERNATING
762 else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
763 rgblight_effect_alternating();
769 #endif /* RGBLIGHT_USE_TIMER */
772 #ifdef RGBLIGHT_EFFECT_BREATHING
773 __attribute__ ((weak))
774 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
776 void rgblight_effect_breathing(uint8_t interval) {
777 static uint8_t pos = 0;
778 static uint16_t last_timer = 0;
781 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
784 last_timer = timer_read();
786 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
787 val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
788 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
789 pos = (pos + 1) % 256;
793 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
794 __attribute__ ((weak))
795 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
797 void rgblight_effect_rainbow_mood(uint8_t interval) {
798 static uint16_t current_hue = 0;
799 static uint16_t last_timer = 0;
801 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
804 last_timer = timer_read();
805 rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
806 current_hue = (current_hue + 1) % 360;
810 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
811 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
812 #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
815 __attribute__ ((weak))
816 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
818 void rgblight_effect_rainbow_swirl(uint8_t interval) {
819 static uint16_t current_hue = 0;
820 static uint16_t last_timer = 0;
823 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
826 last_timer = timer_read();
827 for (i = 0; i < RGBLED_NUM; i++) {
828 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
829 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
834 current_hue = (current_hue + 1) % 360;
836 if (current_hue - 1 < 0) {
839 current_hue = current_hue - 1;
845 #ifdef RGBLIGHT_EFFECT_SNAKE
846 __attribute__ ((weak))
847 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
849 void rgblight_effect_snake(uint8_t interval) {
850 static uint8_t pos = 0;
851 static uint16_t last_timer = 0;
854 int8_t increment = 1;
858 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
861 last_timer = timer_read();
862 for (i = 0; i < RGBLED_NUM; i++) {
866 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
867 k = pos + j * increment;
872 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]);
877 if (increment == 1) {
879 pos = RGBLED_NUM - 1;
884 pos = (pos + 1) % RGBLED_NUM;
889 #ifdef RGBLIGHT_EFFECT_KNIGHT
890 __attribute__ ((weak))
891 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
893 void rgblight_effect_knight(uint8_t interval) {
894 static uint16_t last_timer = 0;
895 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
898 last_timer = timer_read();
900 static int8_t low_bound = 0;
901 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
902 static int8_t increment = 1;
905 // Set all the LEDs to 0
906 for (i = 0; i < RGBLED_NUM; i++) {
911 // Determine which LEDs should be lit up
912 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
913 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
915 if (i >= low_bound && i <= high_bound) {
916 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
925 // Move from low_bound to high_bound changing the direction we increment each
926 // time a boundary is hit.
927 low_bound += increment;
928 high_bound += increment;
930 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
931 increment = -increment;
936 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
937 void rgblight_effect_christmas(void) {
938 static uint16_t current_offset = 0;
939 static uint16_t last_timer = 0;
942 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
945 last_timer = timer_read();
946 current_offset = (current_offset + 1) % 2;
947 for (i = 0; i < RGBLED_NUM; i++) {
948 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
949 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
955 #ifdef RGBLIGHT_EFFECT_RGB_TEST
956 __attribute__ ((weak))
957 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
959 void rgblight_effect_rgbtest(void) {
960 static uint8_t pos = 0;
961 static uint16_t last_timer = 0;
962 static uint8_t maxval = 0;
963 uint8_t g; uint8_t r; uint8_t b;
965 if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
971 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
974 last_timer = timer_read();
977 case 0: r = maxval; break;
978 case 1: g = maxval; break;
979 case 2: b = maxval; break;
981 rgblight_setrgb(r, g, b);
986 #ifdef RGBLIGHT_EFFECT_ALTERNATING
987 void rgblight_effect_alternating(void){
988 static uint16_t last_timer = 0;
989 static uint16_t pos = 0;
990 if (timer_elapsed(last_timer) < 500) {
993 last_timer = timer_read();
995 for(int i = 0; i<RGBLED_NUM; i++){
996 if(i<RGBLED_NUM/2 && pos){
997 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
998 }else if (i>=RGBLED_NUM/2 && !pos){
999 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1001 sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
1005 pos = (pos + 1) % 2;