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"
38 /* for split keyboard */
39 #define RGBLIGHT_SPLIT_SET_CHANGE_MODE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_MODE
40 #define RGBLIGHT_SPLIT_SET_CHANGE_HSVS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_HSVS
41 #define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS rgblight_status.change_flags |= (RGBLIGHT_STATUS_CHANGE_MODE|RGBLIGHT_STATUS_CHANGE_HSVS)
42 #define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER
43 #define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK
45 #define RGBLIGHT_SPLIT_SET_CHANGE_MODE
46 #define RGBLIGHT_SPLIT_SET_CHANGE_HSVS
47 #define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
48 #define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
49 #define RGBLIGHT_SPLIT_ANIMATION_TICK
52 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
53 #define _RGBM_SINGLE_DYNAMIC(sym)
54 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
55 #define _RGBM_MULTI_DYNAMIC(sym)
56 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_ ## sym,
57 #define _RGBM_TMP_DYNAMIC(sym, msym)
58 static uint8_t static_effect_table [] = {
59 #include "rgblight_modes.h"
62 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
63 #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_ ## sym,
64 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
65 #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_ ## sym,
66 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_ ## msym,
67 #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_ ## msym,
68 static uint8_t mode_base_table [] = {
69 0, // RGBLIGHT_MODE_zero
70 #include "rgblight_modes.h"
73 static inline int is_static_effect(uint8_t mode) {
74 return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
77 #define MIN(a,b) (((a)<(b))?(a):(b))
78 #define MAX(a,b) (((a)>(b))?(a):(b))
80 #ifdef RGBLIGHT_LED_MAP
81 const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
84 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
85 __attribute__ ((weak))
86 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
89 rgblight_config_t rgblight_config;
90 rgblight_status_t rgblight_status = { .timer_enabled = false };
91 bool is_rgblight_initialized = false;
93 #ifdef RGBLIGHT_USE_TIMER
94 animation_status_t animation_status = {};
98 LED_TYPE led[RGBLED_NUM];
103 static uint8_t clipping_start_pos = 0;
104 static uint8_t clipping_num_leds = RGBLED_NUM;
106 void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
107 clipping_start_pos = start_pos;
108 clipping_num_leds = num_leds;
112 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
113 uint8_t r = 0, g = 0, b = 0, base, color;
115 if (val > RGBLIGHT_LIMIT_VAL) {
116 val=RGBLIGHT_LIMIT_VAL; // limit the val
119 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
124 base = ((255 - sat) * val) >> 8;
125 color = (val - base) * (hue % 60) / 60;
160 r = pgm_read_byte(&CIE1931_CURVE[r]);
161 g = pgm_read_byte(&CIE1931_CURVE[g]);
162 b = pgm_read_byte(&CIE1931_CURVE[b]);
164 setrgb(r, g, b, led1);
167 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
173 void rgblight_check_config(void) {
174 /* Add some out of bound checks for RGB light config */
176 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
177 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
179 else if (rgblight_config.mode > RGBLIGHT_MODES) {
180 rgblight_config.mode = RGBLIGHT_MODES;
183 if (rgblight_config.hue < 0) {
184 rgblight_config.hue = 0;
185 } else if (rgblight_config.hue > 360) {
186 rgblight_config.hue %= 360;
189 if (rgblight_config.sat < 0) {
190 rgblight_config.sat = 0;
191 } else if (rgblight_config.sat > 255) {
192 rgblight_config.sat = 255;
195 if (rgblight_config.val < 0) {
196 rgblight_config.val = 0;
197 } else if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
198 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
203 uint32_t eeconfig_read_rgblight(void) {
204 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
205 return eeprom_read_dword(EECONFIG_RGBLIGHT);
211 void eeconfig_update_rgblight(uint32_t val) {
212 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
213 rgblight_check_config();
214 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
218 void eeconfig_update_rgblight_default(void) {
219 //dprintf("eeconfig_update_rgblight_default\n");
220 rgblight_config.enable = 1;
221 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
222 rgblight_config.hue = 0;
223 rgblight_config.sat = 255;
224 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
225 rgblight_config.speed = 0;
226 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
227 eeconfig_update_rgblight(rgblight_config.raw);
230 void eeconfig_debug_rgblight(void) {
231 dprintf("rgblight_config eprom\n");
232 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
233 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
234 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
235 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
236 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
237 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
240 void rgblight_init(void) {
241 /* if already initialized, don't do it again.
242 If you must do it again, extern this and set to false, first.
243 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
244 if (is_rgblight_initialized) { return; }
246 debug_enable = 1; // Debug ON!
247 dprintf("rgblight_init called.\n");
248 dprintf("rgblight_init start!\n");
249 if (!eeconfig_is_enabled()) {
250 dprintf("rgblight_init eeconfig is not enabled.\n");
252 eeconfig_update_rgblight_default();
254 rgblight_config.raw = eeconfig_read_rgblight();
255 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
256 if (!rgblight_config.mode) {
257 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
258 eeconfig_update_rgblight_default();
259 rgblight_config.raw = eeconfig_read_rgblight();
261 rgblight_check_config();
263 eeconfig_debug_rgblight(); // display current eeprom values
265 #ifdef RGBLIGHT_USE_TIMER
266 rgblight_timer_init(); // setup the timer
269 if (rgblight_config.enable) {
270 rgblight_mode_noeeprom(rgblight_config.mode);
273 is_rgblight_initialized = true;
277 uint32_t rgblight_read_dword(void) {
278 return rgblight_config.raw;
281 void rgblight_update_dword(uint32_t dword) {
282 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
283 rgblight_config.raw = dword;
284 if (rgblight_config.enable)
285 rgblight_mode_noeeprom(rgblight_config.mode);
287 #ifdef RGBLIGHT_USE_TIMER
288 rgblight_timer_disable();
294 void rgblight_increase(void) {
296 if (rgblight_config.mode < RGBLIGHT_MODES) {
297 mode = rgblight_config.mode + 1;
301 void rgblight_decrease(void) {
303 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
304 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
305 mode = rgblight_config.mode - 1;
309 void rgblight_step_helper(bool write_to_eeprom) {
311 mode = rgblight_config.mode + 1;
312 if (mode > RGBLIGHT_MODES) {
315 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
317 void rgblight_step_noeeprom(void) {
318 rgblight_step_helper(false);
320 void rgblight_step(void) {
321 rgblight_step_helper(true);
323 void rgblight_step_reverse_helper(bool write_to_eeprom) {
325 mode = rgblight_config.mode - 1;
327 mode = RGBLIGHT_MODES;
329 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
331 void rgblight_step_reverse_noeeprom(void) {
332 rgblight_step_reverse_helper(false);
334 void rgblight_step_reverse(void) {
335 rgblight_step_reverse_helper(true);
338 uint8_t rgblight_get_mode(void) {
339 if (!rgblight_config.enable) {
343 return rgblight_config.mode;
346 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
347 if (!rgblight_config.enable) {
350 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
351 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
352 } else if (mode > RGBLIGHT_MODES) {
353 rgblight_config.mode = RGBLIGHT_MODES;
355 rgblight_config.mode = mode;
357 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
358 if (write_to_eeprom) {
359 eeconfig_update_rgblight(rgblight_config.raw);
360 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
362 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
364 if( is_static_effect(rgblight_config.mode) ) {
365 #ifdef RGBLIGHT_USE_TIMER
366 rgblight_timer_disable();
369 #ifdef RGBLIGHT_USE_TIMER
370 rgblight_timer_enable();
373 #ifdef RGBLIGHT_USE_TIMER
374 animation_status.restart = true;
376 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
379 void rgblight_mode(uint8_t mode) {
380 rgblight_mode_eeprom_helper(mode, true);
383 void rgblight_mode_noeeprom(uint8_t mode) {
384 rgblight_mode_eeprom_helper(mode, false);
388 void rgblight_toggle(void) {
389 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
390 if (rgblight_config.enable) {
398 void rgblight_toggle_noeeprom(void) {
399 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
400 if (rgblight_config.enable) {
401 rgblight_disable_noeeprom();
404 rgblight_enable_noeeprom();
408 void rgblight_enable(void) {
409 rgblight_config.enable = 1;
410 // No need to update EEPROM here. rgblight_mode() will do that, actually
411 //eeconfig_update_rgblight(rgblight_config.raw);
412 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
413 rgblight_mode(rgblight_config.mode);
416 void rgblight_enable_noeeprom(void) {
417 rgblight_config.enable = 1;
418 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
419 rgblight_mode_noeeprom(rgblight_config.mode);
422 void rgblight_disable(void) {
423 rgblight_config.enable = 0;
424 eeconfig_update_rgblight(rgblight_config.raw);
425 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
426 #ifdef RGBLIGHT_USE_TIMER
427 rgblight_timer_disable();
429 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
434 void rgblight_disable_noeeprom(void) {
435 rgblight_config.enable = 0;
436 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
437 #ifdef RGBLIGHT_USE_TIMER
438 rgblight_timer_disable();
440 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
446 // Deals with the messy details of incrementing an integer
447 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
448 int16_t new_value = value;
450 return MIN( MAX( new_value, min ), max );
453 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
454 int16_t new_value = value;
456 return MIN( MAX( new_value, min ), max );
459 void rgblight_increase_hue_helper(bool write_to_eeprom) {
461 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
462 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
464 void rgblight_increase_hue_noeeprom(void) {
465 rgblight_increase_hue_helper(false);
467 void rgblight_increase_hue(void) {
468 rgblight_increase_hue_helper(true);
470 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
472 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
473 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
475 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
477 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
479 void rgblight_decrease_hue_noeeprom(void) {
480 rgblight_decrease_hue_helper(false);
482 void rgblight_decrease_hue(void) {
483 rgblight_decrease_hue_helper(true);
485 void rgblight_increase_sat_helper(bool write_to_eeprom) {
487 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
490 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
492 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
494 void rgblight_increase_sat_noeeprom(void) {
495 rgblight_increase_sat_helper(false);
497 void rgblight_increase_sat(void) {
498 rgblight_increase_sat_helper(true);
500 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
502 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
505 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
507 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
509 void rgblight_decrease_sat_noeeprom(void) {
510 rgblight_decrease_sat_helper(false);
512 void rgblight_decrease_sat(void) {
513 rgblight_decrease_sat_helper(true);
515 void rgblight_increase_val_helper(bool write_to_eeprom) {
517 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
518 val = RGBLIGHT_LIMIT_VAL;
520 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
522 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
524 void rgblight_increase_val_noeeprom(void) {
525 rgblight_increase_val_helper(false);
527 void rgblight_increase_val(void) {
528 rgblight_increase_val_helper(true);
530 void rgblight_decrease_val_helper(bool write_to_eeprom) {
532 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
535 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
537 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
539 void rgblight_decrease_val_noeeprom(void) {
540 rgblight_decrease_val_helper(false);
542 void rgblight_decrease_val(void) {
543 rgblight_decrease_val_helper(true);
545 void rgblight_increase_speed(void) {
546 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
547 //RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
548 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
551 void rgblight_decrease_speed(void) {
552 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
553 //RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
554 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
557 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
558 if (rgblight_config.enable) {
560 sethsv(hue, sat, val, &tmp_led);
561 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
562 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
566 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
567 if (rgblight_config.enable) {
568 rgblight_status.base_mode = mode_base_table[rgblight_config.mode];
569 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
572 sethsv(hue, sat, val, &tmp_led);
573 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
575 // all LEDs in same color
576 if ( 1 == 0 ) { //dummy
578 #ifdef RGBLIGHT_EFFECT_BREATHING
579 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING ) {
580 // breathing mode, ignore the change of val, use in memory value instead
581 val = rgblight_config.val;
584 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
585 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
586 // rainbow mood, ignore the change of hue
587 hue = rgblight_config.hue;
590 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
591 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
592 // rainbow swirl, ignore the change of hue
593 hue = rgblight_config.hue;
596 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
597 else if (rgblight_status.base_mode == RGBLIGHT_MODE_STATIC_GRADIENT) {
600 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
601 int8_t direction = (delta % 2) ? -1 : 1;
602 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[delta / 2]);
603 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
604 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
605 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
606 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
612 #ifdef RGBLIGHT_SPLIT
613 if( rgblight_config.hue != hue ||
614 rgblight_config.sat != sat ||
615 rgblight_config.val != val ) {
616 RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
619 rgblight_config.hue = hue;
620 rgblight_config.sat = sat;
621 rgblight_config.val = val;
622 if (write_to_eeprom) {
623 eeconfig_update_rgblight(rgblight_config.raw);
624 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
626 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
631 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
632 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
635 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
636 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
639 uint16_t rgblight_get_hue(void) {
640 return rgblight_config.hue;
643 uint8_t rgblight_get_sat(void) {
644 return rgblight_config.sat;
647 uint8_t rgblight_get_val(void) {
648 return rgblight_config.val;
651 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
652 if (!rgblight_config.enable) { return; }
654 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
662 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
663 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
671 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
672 if (!rgblight_config.enable) { return; }
675 sethsv(hue, sat, val, &tmp_led);
676 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
679 #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) \
680 || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
682 static uint8_t get_interval_time(const uint8_t* default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
684 #ifdef VELOCIKEY_ENABLE
685 velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
687 pgm_read_byte(default_interval_address);
692 void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
693 if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) { return; }
695 for (uint8_t i = start; i < end; i++) {
704 void rgblight_sethsv_range(uint16_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
705 if (!rgblight_config.enable) { return; }
708 sethsv(hue, sat, val, &tmp_led);
709 rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
712 void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) {
713 rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2);
716 void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b) {
717 rgblight_setrgb_range(r, g, b, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
720 void rgblight_sethsv_master(uint16_t hue, uint8_t sat, uint8_t val) {
721 rgblight_sethsv_range(hue, sat, val, 0, (uint8_t) RGBLED_NUM/2);
724 void rgblight_sethsv_slave(uint16_t hue, uint8_t sat, uint8_t val) {
725 rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
728 #ifndef RGBLIGHT_CUSTOM_DRIVER
729 void rgblight_set(void) {
730 LED_TYPE *start_led = led + clipping_start_pos;
731 uint16_t num_leds = clipping_num_leds;
732 if (rgblight_config.enable) {
733 #ifdef RGBLIGHT_LED_MAP
734 LED_TYPE led0[RGBLED_NUM];
735 for(uint8_t i = 0; i < RGBLED_NUM; i++) {
736 led0[i] = led[pgm_read_byte(&led_map[i])];
738 start_led = led0 + clipping_start_pos;
741 ws2812_setleds_rgbw(start_led, num_leds);
743 ws2812_setleds(start_led, num_leds);
746 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
752 ws2812_setleds_rgbw(start_led, num_leds);
754 ws2812_setleds(start_led, num_leds);
760 #ifdef RGBLIGHT_SPLIT
761 /* for split keyboard master side */
762 uint8_t rgblight_get_change_flags(void) {
763 return rgblight_status.change_flags;
766 void rgblight_clear_change_flags(void) {
767 rgblight_status.change_flags = 0;
770 void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
771 syncinfo->config = rgblight_config;
772 syncinfo->status = rgblight_status;
775 /* for split keyboard slave side */
776 void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
777 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
778 if (syncinfo->config.enable) {
779 rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
780 rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
782 rgblight_disable_noeeprom();
785 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
786 rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
787 // rgblight_config.speed = config->speed; // NEED???
789 #ifdef RGBLIGHT_USE_TIMER
790 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
791 if (syncinfo->status.timer_enabled) {
792 rgblight_timer_enable();
794 rgblight_timer_disable();
797 #ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
798 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
799 animation_status.restart = true;
801 #endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
802 #endif /* RGBLIGHT_USE_TIMER */
804 #endif /* RGBLIGHT_SPLIT */
806 #ifdef RGBLIGHT_USE_TIMER
808 typedef void (*effect_func_t)(animation_status_t *anim);
810 // Animation timer -- use system timer (AVR Timer0)
811 void rgblight_timer_init(void) {
812 // OLD!!!! Animation timer -- AVR Timer3
813 // static uint8_t rgblight_timer_is_init = 0;
814 // if (rgblight_timer_is_init) {
817 // rgblight_timer_is_init = 1;
818 // /* Timer 3 setup */
819 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
820 // | _BV(CS30); // Clock selelct: clk/1
821 // /* Set TOP value */
822 // uint8_t sreg = SREG;
824 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
825 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
828 rgblight_status.timer_enabled = false;
829 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
831 void rgblight_timer_enable(void) {
832 if( !is_static_effect(rgblight_config.mode) ) {
833 rgblight_status.timer_enabled = true;
835 animation_status.last_timer = timer_read();
836 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
837 dprintf("rgblight timer enabled.\n");
839 void rgblight_timer_disable(void) {
840 rgblight_status.timer_enabled = false;
841 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
842 dprintf("rgblight timer disable.\n");
844 void rgblight_timer_toggle(void) {
845 dprintf("rgblight timer toggle.\n");
846 if(rgblight_status.timer_enabled) {
847 rgblight_timer_disable();
849 rgblight_timer_enable();
853 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
855 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
856 rgblight_setrgb(r, g, b);
859 static void rgblight_effect_dummy(animation_status_t *anim) {
862 dprintf("rgblight_task() what happened?\n");
863 dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
864 dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
865 rgblight_config.mode, rgblight_status.base_mode,
866 rgblight_status.timer_enabled);
867 dprintf("last_timer = %d\n",anim->last_timer);
871 void rgblight_task(void) {
872 if (rgblight_status.timer_enabled) {
873 effect_func_t effect_func = rgblight_effect_dummy;
874 uint16_t interval_time = 2000; // dummy interval
875 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
876 animation_status.delta = delta;
878 // static light mode, do nothing here
879 if ( 1 == 0 ) { //dummy
881 #ifdef RGBLIGHT_EFFECT_BREATHING
882 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
884 interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
885 effect_func = rgblight_effect_breathing;
888 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
889 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
891 interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
892 effect_func = rgblight_effect_rainbow_mood;
895 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
896 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
897 // rainbow swirl mode
898 interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
899 effect_func = rgblight_effect_rainbow_swirl;
902 #ifdef RGBLIGHT_EFFECT_SNAKE
903 else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
905 interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
906 effect_func = rgblight_effect_snake;
909 #ifdef RGBLIGHT_EFFECT_KNIGHT
910 else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
912 interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
913 effect_func = rgblight_effect_knight;
916 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
917 else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
919 interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
920 effect_func = (effect_func_t)rgblight_effect_christmas;
923 #ifdef RGBLIGHT_EFFECT_RGB_TEST
924 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
926 interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
927 effect_func = (effect_func_t)rgblight_effect_rgbtest;
930 #ifdef RGBLIGHT_EFFECT_ALTERNATING
931 else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING){
933 effect_func = (effect_func_t)rgblight_effect_alternating;
936 if (animation_status.restart) {
937 animation_status.restart = false;
938 animation_status.last_timer = timer_read() - interval_time - 1;
939 animation_status.pos16 = 0; // restart signal to local each effect
941 if (timer_elapsed(animation_status.last_timer) >= interval_time) {
942 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
943 static uint16_t report_last_timer = 0;
944 static bool tick_flag = false;
948 //dprintf("rgblight animation tick\n");
949 if (timer_elapsed(report_last_timer) >= 30000) {
950 report_last_timer = timer_read();
951 dprintf("rgblight animation tick report to slave\n");
952 RGBLIGHT_SPLIT_ANIMATION_TICK;
955 oldpos16 = animation_status.pos16;
956 //dprintf("call effect function\n");
958 animation_status.last_timer += interval_time;
959 effect_func(&animation_status);
960 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
961 //dprintf("pos16, oldpos16 = %d %d\n",
962 // animation_status.pos16,oldpos16);
963 if (animation_status.pos16 == 0 && oldpos16 != 0) {
964 //dprintf("flag on\n");
972 #endif /* RGBLIGHT_USE_TIMER */
975 #ifdef RGBLIGHT_EFFECT_BREATHING
976 __attribute__ ((weak))
977 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
979 void rgblight_effect_breathing(animation_status_t *anim) {
982 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
983 val = (exp(sin((anim->pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
984 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
985 anim->pos = (anim->pos + 1) % 256;
989 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
990 __attribute__ ((weak))
991 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
993 void rgblight_effect_rainbow_mood(animation_status_t *anim) {
994 rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
995 anim->current_hue = (anim->current_hue + 1) % 360;
999 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
1000 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
1001 #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
1004 __attribute__ ((weak))
1005 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
1007 void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
1011 for (i = 0; i < RGBLED_NUM; i++) {
1012 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + anim->current_hue) % 360;
1013 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1017 if (anim->delta % 2) {
1018 anim->current_hue = (anim->current_hue + 1) % 360;
1020 if (anim->current_hue - 1 < 0) {
1021 anim->current_hue = 359;
1023 anim->current_hue = anim->current_hue - 1;
1029 #ifdef RGBLIGHT_EFFECT_SNAKE
1030 __attribute__ ((weak))
1031 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
1033 void rgblight_effect_snake(animation_status_t *anim) {
1034 static uint8_t pos = 0;
1037 int8_t increment = 1;
1039 if (anim->delta % 2) {
1043 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1044 if (anim->pos == 0) { // restart signal
1045 if (increment == 1) {
1046 pos = RGBLED_NUM - 1;
1054 for (i = 0; i < RGBLED_NUM; i++) {
1058 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
1059 k = pos + j * increment;
1064 sethsv(rgblight_config.hue, rgblight_config.sat,
1065 (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH),
1066 (LED_TYPE *)&led[i]);
1071 if (increment == 1) {
1073 pos = RGBLED_NUM - 1;
1074 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1079 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1084 pos = (pos + 1) % RGBLED_NUM;
1085 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1092 #ifdef RGBLIGHT_EFFECT_KNIGHT
1093 __attribute__ ((weak))
1094 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
1096 void rgblight_effect_knight(animation_status_t *anim) {
1098 static int8_t low_bound = 0;
1099 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
1100 static int8_t increment = 1;
1103 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1104 if (anim->pos == 0) { // restart signal
1107 high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
1111 // Set all the LEDs to 0
1112 for (i = 0; i < RGBLED_NUM; i++) {
1117 // Determine which LEDs should be lit up
1118 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
1119 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
1121 if (i >= low_bound && i <= high_bound) {
1122 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
1131 // Move from low_bound to high_bound changing the direction we increment each
1132 // time a boundary is hit.
1133 low_bound += increment;
1134 high_bound += increment;
1136 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
1137 increment = -increment;
1138 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1139 if (increment == 1) {
1147 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
1148 void rgblight_effect_christmas(animation_status_t *anim) {
1152 anim->current_offset = (anim->current_offset + 1) % 2;
1153 for (i = 0; i < RGBLED_NUM; i++) {
1154 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 120;
1155 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1161 #ifdef RGBLIGHT_EFFECT_RGB_TEST
1162 __attribute__ ((weak))
1163 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
1165 void rgblight_effect_rgbtest(animation_status_t *anim) {
1166 static uint8_t maxval = 0;
1167 uint8_t g; uint8_t r; uint8_t b;
1171 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
1175 switch( anim->pos ) {
1176 case 0: r = maxval; break;
1177 case 1: g = maxval; break;
1178 case 2: b = maxval; break;
1180 rgblight_setrgb(r, g, b);
1181 anim->pos = (anim->pos + 1) % 3;
1185 #ifdef RGBLIGHT_EFFECT_ALTERNATING
1186 void rgblight_effect_alternating(animation_status_t *anim) {
1188 for(int i = 0; i<RGBLED_NUM; i++){
1189 if(i<RGBLED_NUM/2 && anim->pos){
1190 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1191 }else if (i>=RGBLED_NUM/2 && !anim->pos){
1192 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1194 sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
1198 anim->pos = (anim->pos + 1) % 2;