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"
33 #ifdef VELOCIKEY_ENABLE
34 #include "velocikey.h"
37 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
38 #define _RGBM_SINGLE_DYNAMIC(sym)
39 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
40 #define _RGBM_MULTI_DYNAMIC(sym)
41 #define _RGBM_TMP_STATIC(sym) RGBLIGHT_MODE_ ## sym,
42 #define _RGBM_TMP_DYNAMIC(sym)
43 static uint8_t static_effect_table [] = {
47 static inline int is_static_effect(uint8_t mode) {
48 return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
51 #define MIN(a,b) (((a)<(b))?(a):(b))
52 #define MAX(a,b) (((a)>(b))?(a):(b))
54 #ifdef RGBLIGHT_LED_MAP
55 const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
58 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
59 __attribute__ ((weak))
60 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
63 rgblight_config_t rgblight_config;
64 bool is_rgblight_initialized = false;
66 LED_TYPE led[RGBLED_NUM];
67 bool rgblight_timer_enabled = false;
69 static uint8_t clipping_start_pos = 0;
70 static uint8_t clipping_num_leds = RGBLED_NUM;
72 void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
73 clipping_start_pos = start_pos;
74 clipping_num_leds = num_leds;
78 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
79 uint8_t r = 0, g = 0, b = 0, base, color;
81 if (val > RGBLIGHT_LIMIT_VAL) {
82 val=RGBLIGHT_LIMIT_VAL; // limit the val
85 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
90 base = ((255 - sat) * val) >> 8;
91 color = (val - base) * (hue % 60) / 60;
126 r = pgm_read_byte(&CIE1931_CURVE[r]);
127 g = pgm_read_byte(&CIE1931_CURVE[g]);
128 b = pgm_read_byte(&CIE1931_CURVE[b]);
130 setrgb(r, g, b, led1);
133 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
139 void rgblight_check_config(void) {
140 /* Add some out of bound checks for RGB light config */
142 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
143 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
145 else if (rgblight_config.mode > RGBLIGHT_MODES) {
146 rgblight_config.mode = RGBLIGHT_MODES;
149 if (rgblight_config.hue < 0) {
150 rgblight_config.hue = 0;
151 } else if (rgblight_config.hue > 360) {
152 rgblight_config.hue %= 360;
155 if (rgblight_config.sat < 0) {
156 rgblight_config.sat = 0;
157 } else if (rgblight_config.sat > 255) {
158 rgblight_config.sat = 255;
161 if (rgblight_config.val < 0) {
162 rgblight_config.val = 0;
163 } else if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
164 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
169 uint32_t eeconfig_read_rgblight(void) {
170 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
171 return eeprom_read_dword(EECONFIG_RGBLIGHT);
177 void eeconfig_update_rgblight(uint32_t val) {
178 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
179 rgblight_check_config();
180 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
184 void eeconfig_update_rgblight_default(void) {
185 //dprintf("eeconfig_update_rgblight_default\n");
186 rgblight_config.enable = 1;
187 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
188 rgblight_config.hue = 0;
189 rgblight_config.sat = 255;
190 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
191 rgblight_config.speed = 0;
192 eeconfig_update_rgblight(rgblight_config.raw);
195 void eeconfig_debug_rgblight(void) {
196 dprintf("rgblight_config eprom\n");
197 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
198 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
199 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
200 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
201 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
202 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
205 void rgblight_init(void) {
206 /* if already initialized, don't do it again.
207 If you must do it again, extern this and set to false, first.
208 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
209 if (is_rgblight_initialized) { return; }
211 debug_enable = 1; // Debug ON!
212 dprintf("rgblight_init called.\n");
213 dprintf("rgblight_init start!\n");
214 if (!eeconfig_is_enabled()) {
215 dprintf("rgblight_init eeconfig is not enabled.\n");
217 eeconfig_update_rgblight_default();
219 rgblight_config.raw = eeconfig_read_rgblight();
220 if (!rgblight_config.mode) {
221 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
222 eeconfig_update_rgblight_default();
223 rgblight_config.raw = eeconfig_read_rgblight();
225 rgblight_check_config();
227 eeconfig_debug_rgblight(); // display current eeprom values
229 #ifdef RGBLIGHT_USE_TIMER
230 rgblight_timer_init(); // setup the timer
233 if (rgblight_config.enable) {
234 rgblight_mode_noeeprom(rgblight_config.mode);
237 is_rgblight_initialized = true;
241 uint32_t rgblight_read_dword(void) {
242 return rgblight_config.raw;
245 void rgblight_update_dword(uint32_t dword) {
246 rgblight_config.raw = dword;
247 if (rgblight_config.enable)
248 rgblight_mode_noeeprom(rgblight_config.mode);
250 #ifdef RGBLIGHT_USE_TIMER
251 rgblight_timer_disable();
257 void rgblight_increase(void) {
259 if (rgblight_config.mode < RGBLIGHT_MODES) {
260 mode = rgblight_config.mode + 1;
264 void rgblight_decrease(void) {
266 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
267 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
268 mode = rgblight_config.mode - 1;
272 void rgblight_step_helper(bool write_to_eeprom) {
274 mode = rgblight_config.mode + 1;
275 if (mode > RGBLIGHT_MODES) {
278 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
280 void rgblight_step_noeeprom(void) {
281 rgblight_step_helper(false);
283 void rgblight_step(void) {
284 rgblight_step_helper(true);
286 void rgblight_step_reverse_helper(bool write_to_eeprom) {
288 mode = rgblight_config.mode - 1;
290 mode = RGBLIGHT_MODES;
292 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
294 void rgblight_step_reverse_noeeprom(void) {
295 rgblight_step_reverse_helper(false);
297 void rgblight_step_reverse(void) {
298 rgblight_step_reverse_helper(true);
301 uint8_t rgblight_get_mode(void) {
302 if (!rgblight_config.enable) {
306 return rgblight_config.mode;
309 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
310 if (!rgblight_config.enable) {
313 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
314 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
315 } else if (mode > RGBLIGHT_MODES) {
316 rgblight_config.mode = RGBLIGHT_MODES;
318 rgblight_config.mode = mode;
320 if (write_to_eeprom) {
321 eeconfig_update_rgblight(rgblight_config.raw);
322 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
324 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
326 if( is_static_effect(rgblight_config.mode) ) {
327 #ifdef RGBLIGHT_USE_TIMER
328 rgblight_timer_disable();
331 #ifdef RGBLIGHT_USE_TIMER
332 rgblight_timer_enable();
335 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
338 void rgblight_mode(uint8_t mode) {
339 rgblight_mode_eeprom_helper(mode, true);
342 void rgblight_mode_noeeprom(uint8_t mode) {
343 rgblight_mode_eeprom_helper(mode, false);
347 void rgblight_toggle(void) {
348 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
349 if (rgblight_config.enable) {
357 void rgblight_toggle_noeeprom(void) {
358 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
359 if (rgblight_config.enable) {
360 rgblight_disable_noeeprom();
363 rgblight_enable_noeeprom();
367 void rgblight_enable(void) {
368 rgblight_config.enable = 1;
369 // No need to update EEPROM here. rgblight_mode() will do that, actually
370 //eeconfig_update_rgblight(rgblight_config.raw);
371 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
372 rgblight_mode(rgblight_config.mode);
375 void rgblight_enable_noeeprom(void) {
376 rgblight_config.enable = 1;
377 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
378 rgblight_mode_noeeprom(rgblight_config.mode);
381 void rgblight_disable(void) {
382 rgblight_config.enable = 0;
383 eeconfig_update_rgblight(rgblight_config.raw);
384 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
385 #ifdef RGBLIGHT_USE_TIMER
386 rgblight_timer_disable();
392 void rgblight_disable_noeeprom(void) {
393 rgblight_config.enable = 0;
394 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
395 #ifdef RGBLIGHT_USE_TIMER
396 rgblight_timer_disable();
403 // Deals with the messy details of incrementing an integer
404 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
405 int16_t new_value = value;
407 return MIN( MAX( new_value, min ), max );
410 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
411 int16_t new_value = value;
413 return MIN( MAX( new_value, min ), max );
416 void rgblight_increase_hue_helper(bool write_to_eeprom) {
418 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
419 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
421 void rgblight_increase_hue_noeeprom(void) {
422 rgblight_increase_hue_helper(false);
424 void rgblight_increase_hue(void) {
425 rgblight_increase_hue_helper(true);
427 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
429 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
430 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
432 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
434 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
436 void rgblight_decrease_hue_noeeprom(void) {
437 rgblight_decrease_hue_helper(false);
439 void rgblight_decrease_hue(void) {
440 rgblight_decrease_hue_helper(true);
442 void rgblight_increase_sat_helper(bool write_to_eeprom) {
444 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
447 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
449 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
451 void rgblight_increase_sat_noeeprom(void) {
452 rgblight_increase_sat_helper(false);
454 void rgblight_increase_sat(void) {
455 rgblight_increase_sat_helper(true);
457 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
459 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
462 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
464 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
466 void rgblight_decrease_sat_noeeprom(void) {
467 rgblight_decrease_sat_helper(false);
469 void rgblight_decrease_sat(void) {
470 rgblight_decrease_sat_helper(true);
472 void rgblight_increase_val_helper(bool write_to_eeprom) {
474 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
475 val = RGBLIGHT_LIMIT_VAL;
477 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
479 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
481 void rgblight_increase_val_noeeprom(void) {
482 rgblight_increase_val_helper(false);
484 void rgblight_increase_val(void) {
485 rgblight_increase_val_helper(true);
487 void rgblight_decrease_val_helper(bool write_to_eeprom) {
489 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
492 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
494 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
496 void rgblight_decrease_val_noeeprom(void) {
497 rgblight_decrease_val_helper(false);
499 void rgblight_decrease_val(void) {
500 rgblight_decrease_val_helper(true);
502 void rgblight_increase_speed(void) {
503 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
504 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
507 void rgblight_decrease_speed(void) {
508 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
509 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
512 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
513 if (rgblight_config.enable) {
515 sethsv(hue, sat, val, &tmp_led);
516 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
517 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
521 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
522 if (rgblight_config.enable) {
523 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
526 sethsv(hue, sat, val, &tmp_led);
527 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
529 // all LEDs in same color
530 if ( 1 == 0 ) { //dummy
532 #ifdef RGBLIGHT_EFFECT_BREATHING
533 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
534 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
535 // breathing mode, ignore the change of val, use in memory value instead
536 val = rgblight_config.val;
539 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
540 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
541 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
542 // rainbow mood, ignore the change of hue
543 hue = rgblight_config.hue;
546 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
547 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
548 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
549 // rainbow swirl, ignore the change of hue
550 hue = rgblight_config.hue;
553 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
554 else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
555 rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
558 int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
559 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
560 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
561 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
562 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
563 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
569 rgblight_config.hue = hue;
570 rgblight_config.sat = sat;
571 rgblight_config.val = val;
572 if (write_to_eeprom) {
573 eeconfig_update_rgblight(rgblight_config.raw);
574 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
576 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
581 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
582 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
585 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
586 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
589 uint16_t rgblight_get_hue(void) {
590 return rgblight_config.hue;
593 uint8_t rgblight_get_sat(void) {
594 return rgblight_config.sat;
597 uint8_t rgblight_get_val(void) {
598 return rgblight_config.val;
601 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
602 if (!rgblight_config.enable) { return; }
604 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
612 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
613 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
621 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
622 if (!rgblight_config.enable) { return; }
625 sethsv(hue, sat, val, &tmp_led);
626 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
629 #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) \
630 || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
632 static uint8_t get_interval_time(const uint8_t* default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
634 #ifdef VELOCIKEY_ENABLE
635 velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
637 pgm_read_byte(default_interval_address);
642 void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
643 if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) { return; }
645 for (uint8_t i = start; i < end; i++) {
654 void rgblight_sethsv_range(uint16_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
655 if (!rgblight_config.enable) { return; }
658 sethsv(hue, sat, val, &tmp_led);
659 rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
662 void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) {
663 rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2);
666 void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b) {
667 rgblight_setrgb_range(r, g, b, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
670 void rgblight_sethsv_master(uint16_t hue, uint8_t sat, uint8_t val) {
671 rgblight_sethsv_range(hue, sat, val, 0, (uint8_t) RGBLED_NUM/2);
674 void rgblight_sethsv_slave(uint16_t hue, uint8_t sat, uint8_t val) {
675 rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
678 #ifndef RGBLIGHT_CUSTOM_DRIVER
679 void rgblight_set(void) {
680 LED_TYPE *start_led = led + clipping_start_pos;
681 uint16_t num_leds = clipping_num_leds;
682 if (rgblight_config.enable) {
683 #ifdef RGBLIGHT_LED_MAP
684 LED_TYPE led0[RGBLED_NUM];
685 for(uint8_t i = 0; i < RGBLED_NUM; i++) {
686 led0[i] = led[pgm_read_byte(&led_map[i])];
688 start_led = led0 + clipping_start_pos;
691 ws2812_setleds_rgbw(start_led, num_leds);
693 ws2812_setleds(start_led, num_leds);
696 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
702 ws2812_setleds_rgbw(start_led, num_leds);
704 ws2812_setleds(start_led, num_leds);
710 #ifdef RGBLIGHT_USE_TIMER
712 // Animation timer -- AVR Timer3
713 void rgblight_timer_init(void) {
714 // static uint8_t rgblight_timer_is_init = 0;
715 // if (rgblight_timer_is_init) {
718 // rgblight_timer_is_init = 1;
719 // /* Timer 3 setup */
720 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
721 // | _BV(CS30); // Clock selelct: clk/1
722 // /* Set TOP value */
723 // uint8_t sreg = SREG;
725 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
726 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
729 rgblight_timer_enabled = true;
731 void rgblight_timer_enable(void) {
732 rgblight_timer_enabled = true;
733 dprintf("TIMER3 enabled.\n");
735 void rgblight_timer_disable(void) {
736 rgblight_timer_enabled = false;
737 dprintf("TIMER3 disabled.\n");
739 void rgblight_timer_toggle(void) {
740 rgblight_timer_enabled ^= rgblight_timer_enabled;
741 dprintf("TIMER3 toggled.\n");
744 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
746 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
747 rgblight_setrgb(r, g, b);
750 void rgblight_task(void) {
752 if (rgblight_timer_enabled) {
753 // static light mode, do nothing here
754 if ( 1 == 0 ) { //dummy
756 #ifdef RGBLIGHT_EFFECT_BREATHING
757 else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
758 rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
760 rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
763 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
764 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
765 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
767 rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
770 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
771 else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
772 rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
773 // rainbow swirl mode
774 rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
777 #ifdef RGBLIGHT_EFFECT_SNAKE
778 else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
779 rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
781 rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
784 #ifdef RGBLIGHT_EFFECT_KNIGHT
785 else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
786 rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
788 rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
791 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
792 else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
794 rgblight_effect_christmas();
797 #ifdef RGBLIGHT_EFFECT_RGB_TEST
798 else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
800 rgblight_effect_rgbtest();
803 #ifdef RGBLIGHT_EFFECT_ALTERNATING
804 else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
805 rgblight_effect_alternating();
811 #endif /* RGBLIGHT_USE_TIMER */
814 #ifdef RGBLIGHT_EFFECT_BREATHING
815 __attribute__ ((weak))
816 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
818 void rgblight_effect_breathing(uint8_t interval) {
819 static uint8_t pos = 0;
820 static uint16_t last_timer = 0;
823 uint8_t interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[interval], 1, 100);
825 if (timer_elapsed(last_timer) < interval_time) {
828 last_timer = timer_read();
830 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
831 val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
832 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
833 pos = (pos + 1) % 256;
837 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
838 __attribute__ ((weak))
839 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
841 void rgblight_effect_rainbow_mood(uint8_t interval) {
842 static uint16_t current_hue = 0;
843 static uint16_t last_timer = 0;
845 uint8_t interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[interval], 5, 100);
847 if (timer_elapsed(last_timer) < interval_time) {
850 last_timer = timer_read();
851 rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
852 current_hue = (current_hue + 1) % 360;
856 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
857 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
858 #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
861 __attribute__ ((weak))
862 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
864 void rgblight_effect_rainbow_swirl(uint8_t interval) {
865 static uint16_t current_hue = 0;
866 static uint16_t last_timer = 0;
870 uint8_t interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2], 1, 100);
872 if (timer_elapsed(last_timer) < interval_time) {
875 last_timer = timer_read();
876 for (i = 0; i < RGBLED_NUM; i++) {
877 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
878 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
883 current_hue = (current_hue + 1) % 360;
885 if (current_hue - 1 < 0) {
888 current_hue = current_hue - 1;
894 #ifdef RGBLIGHT_EFFECT_SNAKE
895 __attribute__ ((weak))
896 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
898 void rgblight_effect_snake(uint8_t interval) {
899 static uint8_t pos = 0;
900 static uint16_t last_timer = 0;
903 int8_t increment = 1;
908 uint8_t interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[interval / 2], 1, 200);
910 if (timer_elapsed(last_timer) < interval_time) {
913 last_timer = timer_read();
914 for (i = 0; i < RGBLED_NUM; i++) {
918 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
919 k = pos + j * increment;
924 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]);
929 if (increment == 1) {
931 pos = RGBLED_NUM - 1;
936 pos = (pos + 1) % RGBLED_NUM;
941 #ifdef RGBLIGHT_EFFECT_KNIGHT
942 __attribute__ ((weak))
943 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
945 void rgblight_effect_knight(uint8_t interval) {
946 static uint16_t last_timer = 0;
948 uint8_t interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[interval], 5, 100);
950 if (timer_elapsed(last_timer) < interval_time) {
953 last_timer = timer_read();
955 static int8_t low_bound = 0;
956 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
957 static int8_t increment = 1;
960 // Set all the LEDs to 0
961 for (i = 0; i < RGBLED_NUM; i++) {
966 // Determine which LEDs should be lit up
967 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
968 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
970 if (i >= low_bound && i <= high_bound) {
971 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
980 // Move from low_bound to high_bound changing the direction we increment each
981 // time a boundary is hit.
982 low_bound += increment;
983 high_bound += increment;
985 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
986 increment = -increment;
991 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
992 void rgblight_effect_christmas(void) {
993 static uint16_t current_offset = 0;
994 static uint16_t last_timer = 0;
997 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
1000 last_timer = timer_read();
1001 current_offset = (current_offset + 1) % 2;
1002 for (i = 0; i < RGBLED_NUM; i++) {
1003 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
1004 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1010 #ifdef RGBLIGHT_EFFECT_RGB_TEST
1011 __attribute__ ((weak))
1012 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
1014 void rgblight_effect_rgbtest(void) {
1015 static uint8_t pos = 0;
1016 static uint16_t last_timer = 0;
1017 static uint8_t maxval = 0;
1018 uint8_t g; uint8_t r; uint8_t b;
1020 if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
1026 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
1029 last_timer = timer_read();
1032 case 0: r = maxval; break;
1033 case 1: g = maxval; break;
1034 case 2: b = maxval; break;
1036 rgblight_setrgb(r, g, b);
1037 pos = (pos + 1) % 3;
1041 #ifdef RGBLIGHT_EFFECT_ALTERNATING
1042 void rgblight_effect_alternating(void){
1043 static uint16_t last_timer = 0;
1044 static uint16_t pos = 0;
1045 if (timer_elapsed(last_timer) < 500) {
1048 last_timer = timer_read();
1050 for(int i = 0; i<RGBLED_NUM; i++){
1051 if(i<RGBLED_NUM/2 && pos){
1052 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1053 }else if (i>=RGBLED_NUM/2 && !pos){
1054 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1056 sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
1060 pos = (pos + 1) % 2;