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_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER
42 #define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK
44 #define RGBLIGHT_SPLIT_SET_CHANGE_MODE
45 #define RGBLIGHT_SPLIT_SET_CHANGE_HSVS
46 #define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
47 #define RGBLIGHT_SPLIT_ANIMATION_TICK
50 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
51 #define _RGBM_SINGLE_DYNAMIC(sym)
52 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
53 #define _RGBM_MULTI_DYNAMIC(sym)
54 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_ ## sym,
55 #define _RGBM_TMP_DYNAMIC(sym, msym)
56 static uint8_t static_effect_table [] = {
57 #include "rgblight_modes.h"
60 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_ ## sym,
61 #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_ ## sym,
62 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_ ## sym,
63 #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_ ## sym,
64 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_ ## msym,
65 #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_ ## msym,
66 static uint8_t mode_base_table [] = {
67 0, // RGBLIGHT_MODE_zero
68 #include "rgblight_modes.h"
71 static inline int is_static_effect(uint8_t mode) {
72 return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
75 #define MIN(a,b) (((a)<(b))?(a):(b))
76 #define MAX(a,b) (((a)>(b))?(a):(b))
78 #ifdef RGBLIGHT_LED_MAP
79 const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
82 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
83 __attribute__ ((weak))
84 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
87 rgblight_config_t rgblight_config;
88 rgblight_status_t rgblight_status = { .timer_enabled = false };
89 bool is_rgblight_initialized = false;
91 #ifdef RGBLIGHT_USE_TIMER
92 animation_status_t animation_status = {};
96 LED_TYPE led[RGBLED_NUM];
101 static uint8_t clipping_start_pos = 0;
102 static uint8_t clipping_num_leds = RGBLED_NUM;
104 void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
105 clipping_start_pos = start_pos;
106 clipping_num_leds = num_leds;
110 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
111 uint8_t r = 0, g = 0, b = 0, base, color;
113 if (val > RGBLIGHT_LIMIT_VAL) {
114 val=RGBLIGHT_LIMIT_VAL; // limit the val
117 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
122 base = ((255 - sat) * val) >> 8;
123 color = (val - base) * (hue % 60) / 60;
158 r = pgm_read_byte(&CIE1931_CURVE[r]);
159 g = pgm_read_byte(&CIE1931_CURVE[g]);
160 b = pgm_read_byte(&CIE1931_CURVE[b]);
162 setrgb(r, g, b, led1);
165 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
171 void rgblight_check_config(void) {
172 /* Add some out of bound checks for RGB light config */
174 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
175 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
177 else if (rgblight_config.mode > RGBLIGHT_MODES) {
178 rgblight_config.mode = RGBLIGHT_MODES;
181 if (rgblight_config.hue < 0) {
182 rgblight_config.hue = 0;
183 } else if (rgblight_config.hue > 360) {
184 rgblight_config.hue %= 360;
187 if (rgblight_config.sat < 0) {
188 rgblight_config.sat = 0;
189 } else if (rgblight_config.sat > 255) {
190 rgblight_config.sat = 255;
193 if (rgblight_config.val < 0) {
194 rgblight_config.val = 0;
195 } else if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
196 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
201 uint32_t eeconfig_read_rgblight(void) {
202 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
203 return eeprom_read_dword(EECONFIG_RGBLIGHT);
209 void eeconfig_update_rgblight(uint32_t val) {
210 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
211 rgblight_check_config();
212 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
216 void eeconfig_update_rgblight_default(void) {
217 //dprintf("eeconfig_update_rgblight_default\n");
218 rgblight_config.enable = 1;
219 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
220 rgblight_config.hue = 0;
221 rgblight_config.sat = 255;
222 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
223 rgblight_config.speed = 0;
224 eeconfig_update_rgblight(rgblight_config.raw);
227 void eeconfig_debug_rgblight(void) {
228 dprintf("rgblight_config eprom\n");
229 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
230 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
231 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
232 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
233 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
234 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
237 void rgblight_init(void) {
238 /* if already initialized, don't do it again.
239 If you must do it again, extern this and set to false, first.
240 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
241 if (is_rgblight_initialized) { return; }
243 debug_enable = 1; // Debug ON!
244 dprintf("rgblight_init called.\n");
245 dprintf("rgblight_init start!\n");
246 if (!eeconfig_is_enabled()) {
247 dprintf("rgblight_init eeconfig is not enabled.\n");
249 eeconfig_update_rgblight_default();
251 rgblight_config.raw = eeconfig_read_rgblight();
252 RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
253 if (!rgblight_config.mode) {
254 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
255 eeconfig_update_rgblight_default();
256 rgblight_config.raw = eeconfig_read_rgblight();
258 rgblight_check_config();
260 eeconfig_debug_rgblight(); // display current eeprom values
262 #ifdef RGBLIGHT_USE_TIMER
263 rgblight_timer_init(); // setup the timer
266 if (rgblight_config.enable) {
267 rgblight_mode_noeeprom(rgblight_config.mode);
270 is_rgblight_initialized = true;
274 uint32_t rgblight_read_dword(void) {
275 return rgblight_config.raw;
278 void rgblight_update_dword(uint32_t dword) {
279 rgblight_config.raw = dword;
280 if (rgblight_config.enable)
281 rgblight_mode_noeeprom(rgblight_config.mode);
283 #ifdef RGBLIGHT_USE_TIMER
284 rgblight_timer_disable();
290 void rgblight_increase(void) {
292 if (rgblight_config.mode < RGBLIGHT_MODES) {
293 mode = rgblight_config.mode + 1;
297 void rgblight_decrease(void) {
299 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
300 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
301 mode = rgblight_config.mode - 1;
305 void rgblight_step_helper(bool write_to_eeprom) {
307 mode = rgblight_config.mode + 1;
308 if (mode > RGBLIGHT_MODES) {
311 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
313 void rgblight_step_noeeprom(void) {
314 rgblight_step_helper(false);
316 void rgblight_step(void) {
317 rgblight_step_helper(true);
319 void rgblight_step_reverse_helper(bool write_to_eeprom) {
321 mode = rgblight_config.mode - 1;
323 mode = RGBLIGHT_MODES;
325 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
327 void rgblight_step_reverse_noeeprom(void) {
328 rgblight_step_reverse_helper(false);
330 void rgblight_step_reverse(void) {
331 rgblight_step_reverse_helper(true);
334 uint8_t rgblight_get_mode(void) {
335 if (!rgblight_config.enable) {
339 return rgblight_config.mode;
342 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
343 if (!rgblight_config.enable) {
346 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
347 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
348 } else if (mode > RGBLIGHT_MODES) {
349 rgblight_config.mode = RGBLIGHT_MODES;
351 rgblight_config.mode = mode;
353 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
354 if (write_to_eeprom) {
355 eeconfig_update_rgblight(rgblight_config.raw);
356 xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
358 xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
360 if( is_static_effect(rgblight_config.mode) ) {
361 #ifdef RGBLIGHT_USE_TIMER
362 rgblight_timer_disable();
365 #ifdef RGBLIGHT_USE_TIMER
366 rgblight_timer_enable();
369 #ifdef RGBLIGHT_USE_TIMER
370 animation_status.restart = true;
372 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
375 void rgblight_mode(uint8_t mode) {
376 rgblight_mode_eeprom_helper(mode, true);
379 void rgblight_mode_noeeprom(uint8_t mode) {
380 rgblight_mode_eeprom_helper(mode, false);
384 void rgblight_toggle(void) {
385 xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
386 if (rgblight_config.enable) {
394 void rgblight_toggle_noeeprom(void) {
395 xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
396 if (rgblight_config.enable) {
397 rgblight_disable_noeeprom();
400 rgblight_enable_noeeprom();
404 void rgblight_enable(void) {
405 rgblight_config.enable = 1;
406 // No need to update EEPROM here. rgblight_mode() will do that, actually
407 //eeconfig_update_rgblight(rgblight_config.raw);
408 xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
409 rgblight_mode(rgblight_config.mode);
412 void rgblight_enable_noeeprom(void) {
413 rgblight_config.enable = 1;
414 xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
415 rgblight_mode_noeeprom(rgblight_config.mode);
418 void rgblight_disable(void) {
419 rgblight_config.enable = 0;
420 eeconfig_update_rgblight(rgblight_config.raw);
421 xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
422 #ifdef RGBLIGHT_USE_TIMER
423 rgblight_timer_disable();
425 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
430 void rgblight_disable_noeeprom(void) {
431 rgblight_config.enable = 0;
432 xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
433 #ifdef RGBLIGHT_USE_TIMER
434 rgblight_timer_disable();
436 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
442 // Deals with the messy details of incrementing an integer
443 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
444 int16_t new_value = value;
446 return MIN( MAX( new_value, min ), max );
449 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
450 int16_t new_value = value;
452 return MIN( MAX( new_value, min ), max );
455 void rgblight_increase_hue_helper(bool write_to_eeprom) {
457 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
458 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
460 void rgblight_increase_hue_noeeprom(void) {
461 rgblight_increase_hue_helper(false);
463 void rgblight_increase_hue(void) {
464 rgblight_increase_hue_helper(true);
466 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
468 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
469 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
471 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
473 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
475 void rgblight_decrease_hue_noeeprom(void) {
476 rgblight_decrease_hue_helper(false);
478 void rgblight_decrease_hue(void) {
479 rgblight_decrease_hue_helper(true);
481 void rgblight_increase_sat_helper(bool write_to_eeprom) {
483 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
486 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
488 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
490 void rgblight_increase_sat_noeeprom(void) {
491 rgblight_increase_sat_helper(false);
493 void rgblight_increase_sat(void) {
494 rgblight_increase_sat_helper(true);
496 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
498 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
501 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
503 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
505 void rgblight_decrease_sat_noeeprom(void) {
506 rgblight_decrease_sat_helper(false);
508 void rgblight_decrease_sat(void) {
509 rgblight_decrease_sat_helper(true);
511 void rgblight_increase_val_helper(bool write_to_eeprom) {
513 if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
514 val = RGBLIGHT_LIMIT_VAL;
516 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
518 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
520 void rgblight_increase_val_noeeprom(void) {
521 rgblight_increase_val_helper(false);
523 void rgblight_increase_val(void) {
524 rgblight_increase_val_helper(true);
526 void rgblight_decrease_val_helper(bool write_to_eeprom) {
528 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
531 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
533 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
535 void rgblight_decrease_val_noeeprom(void) {
536 rgblight_decrease_val_helper(false);
538 void rgblight_decrease_val(void) {
539 rgblight_decrease_val_helper(true);
541 void rgblight_increase_speed(void) {
542 rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
543 //RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
544 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
547 void rgblight_decrease_speed(void) {
548 rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
549 //RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
550 eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
553 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
554 if (rgblight_config.enable) {
556 sethsv(hue, sat, val, &tmp_led);
557 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
558 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
562 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
563 if (rgblight_config.enable) {
564 rgblight_status.base_mode = mode_base_table[rgblight_config.mode];
565 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
568 sethsv(hue, sat, val, &tmp_led);
569 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
571 // all LEDs in same color
572 if ( 1 == 0 ) { //dummy
574 #ifdef RGBLIGHT_EFFECT_BREATHING
575 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING ) {
576 // breathing mode, ignore the change of val, use in memory value instead
577 val = rgblight_config.val;
580 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
581 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
582 // rainbow mood, ignore the change of hue
583 hue = rgblight_config.hue;
586 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
587 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
588 // rainbow swirl, ignore the change of hue
589 hue = rgblight_config.hue;
592 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
593 else if (rgblight_status.base_mode == RGBLIGHT_MODE_STATIC_GRADIENT) {
596 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
597 int8_t direction = (delta % 2) ? -1 : 1;
598 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[delta / 2]);
599 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
600 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
601 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
602 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
608 #ifdef RGBLIGHT_SPLIT
609 if( rgblight_config.hue != hue ||
610 rgblight_config.sat != sat ||
611 rgblight_config.val != val ) {
612 RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
615 rgblight_config.hue = hue;
616 rgblight_config.sat = sat;
617 rgblight_config.val = val;
618 if (write_to_eeprom) {
619 eeconfig_update_rgblight(rgblight_config.raw);
620 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
622 xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
627 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
628 rgblight_sethsv_eeprom_helper(hue, sat, val, true);
631 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
632 rgblight_sethsv_eeprom_helper(hue, sat, val, false);
635 uint16_t rgblight_get_hue(void) {
636 return rgblight_config.hue;
639 uint8_t rgblight_get_sat(void) {
640 return rgblight_config.sat;
643 uint8_t rgblight_get_val(void) {
644 return rgblight_config.val;
647 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
648 if (!rgblight_config.enable) { return; }
650 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
658 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
659 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
667 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
668 if (!rgblight_config.enable) { return; }
671 sethsv(hue, sat, val, &tmp_led);
672 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
675 #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) \
676 || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
678 static uint8_t get_interval_time(const uint8_t* default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
680 #ifdef VELOCIKEY_ENABLE
681 velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
683 pgm_read_byte(default_interval_address);
688 void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
689 if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) { return; }
691 for (uint8_t i = start; i < end; i++) {
700 void rgblight_sethsv_range(uint16_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
701 if (!rgblight_config.enable) { return; }
704 sethsv(hue, sat, val, &tmp_led);
705 rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
708 void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) {
709 rgblight_setrgb_range(r, g, b, 0 , (uint8_t) RGBLED_NUM/2);
712 void rgblight_setrgb_slave(uint8_t r, uint8_t g, uint8_t b) {
713 rgblight_setrgb_range(r, g, b, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
716 void rgblight_sethsv_master(uint16_t hue, uint8_t sat, uint8_t val) {
717 rgblight_sethsv_range(hue, sat, val, 0, (uint8_t) RGBLED_NUM/2);
720 void rgblight_sethsv_slave(uint16_t hue, uint8_t sat, uint8_t val) {
721 rgblight_sethsv_range(hue, sat, val, (uint8_t) RGBLED_NUM/2, (uint8_t) RGBLED_NUM);
724 #ifndef RGBLIGHT_CUSTOM_DRIVER
725 void rgblight_set(void) {
726 LED_TYPE *start_led = led + clipping_start_pos;
727 uint16_t num_leds = clipping_num_leds;
728 if (rgblight_config.enable) {
729 #ifdef RGBLIGHT_LED_MAP
730 LED_TYPE led0[RGBLED_NUM];
731 for(uint8_t i = 0; i < RGBLED_NUM; i++) {
732 led0[i] = led[pgm_read_byte(&led_map[i])];
734 start_led = led0 + clipping_start_pos;
737 ws2812_setleds_rgbw(start_led, num_leds);
739 ws2812_setleds(start_led, num_leds);
742 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
748 ws2812_setleds_rgbw(start_led, num_leds);
750 ws2812_setleds(start_led, num_leds);
756 #ifdef RGBLIGHT_SPLIT
757 /* for split keyboard master side */
758 uint8_t rgblight_get_change_flags(void) {
759 return rgblight_status.change_flags;
762 void rgblight_clear_change_flags(void) {
763 rgblight_status.change_flags = 0;
766 void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
767 syncinfo->config = rgblight_config;
768 syncinfo->status = rgblight_status;
771 /* for split keyboard slave side */
772 void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
773 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
774 if (syncinfo->config.enable) {
775 rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
776 rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
778 rgblight_disable_noeeprom();
781 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
782 rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
783 // rgblight_config.speed = config->speed; // NEED???
785 #ifdef RGBLIGHT_USE_TIMER
786 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
787 if (syncinfo->status.timer_enabled) {
788 rgblight_timer_enable();
790 rgblight_timer_disable();
793 #ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
794 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
795 animation_status.restart = true;
797 #endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
798 #endif /* RGBLIGHT_USE_TIMER */
800 #endif /* RGBLIGHT_SPLIT */
802 #ifdef RGBLIGHT_USE_TIMER
804 typedef void (*effect_func_t)(animation_status_t *anim);
806 // Animation timer -- use system timer (AVR Timer0)
807 void rgblight_timer_init(void) {
808 // OLD!!!! Animation timer -- AVR Timer3
809 // static uint8_t rgblight_timer_is_init = 0;
810 // if (rgblight_timer_is_init) {
813 // rgblight_timer_is_init = 1;
814 // /* Timer 3 setup */
815 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
816 // | _BV(CS30); // Clock selelct: clk/1
817 // /* Set TOP value */
818 // uint8_t sreg = SREG;
820 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
821 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
824 rgblight_status.timer_enabled = false;
825 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
827 void rgblight_timer_enable(void) {
828 if( !is_static_effect(rgblight_config.mode) ) {
829 rgblight_status.timer_enabled = true;
831 animation_status.last_timer = timer_read();
832 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
833 dprintf("rgblight timer enabled.\n");
835 void rgblight_timer_disable(void) {
836 rgblight_status.timer_enabled = false;
837 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
838 dprintf("rgblight timer disable.\n");
840 void rgblight_timer_toggle(void) {
841 dprintf("rgblight timer toggle.\n");
842 if(rgblight_status.timer_enabled) {
843 rgblight_timer_disable();
845 rgblight_timer_enable();
849 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
851 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
852 rgblight_setrgb(r, g, b);
855 static void rgblight_effect_dummy(animation_status_t *anim) {
858 dprintf("rgblight_task() what happened?\n");
859 dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
860 dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
861 rgblight_config.mode, rgblight_status.base_mode,
862 rgblight_status.timer_enabled);
863 dprintf("last_timer = %d\n",anim->last_timer);
867 void rgblight_task(void) {
868 if (rgblight_status.timer_enabled) {
869 effect_func_t effect_func = rgblight_effect_dummy;
870 uint16_t interval_time = 2000; // dummy interval
871 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
872 animation_status.delta = delta;
874 // static light mode, do nothing here
875 if ( 1 == 0 ) { //dummy
877 #ifdef RGBLIGHT_EFFECT_BREATHING
878 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
880 interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
881 effect_func = rgblight_effect_breathing;
884 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
885 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
887 interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
888 effect_func = rgblight_effect_rainbow_mood;
891 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
892 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
893 // rainbow swirl mode
894 interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
895 effect_func = rgblight_effect_rainbow_swirl;
898 #ifdef RGBLIGHT_EFFECT_SNAKE
899 else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
901 interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
902 effect_func = rgblight_effect_snake;
905 #ifdef RGBLIGHT_EFFECT_KNIGHT
906 else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
908 interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
909 effect_func = rgblight_effect_knight;
912 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
913 else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
915 interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
916 effect_func = (effect_func_t)rgblight_effect_christmas;
919 #ifdef RGBLIGHT_EFFECT_RGB_TEST
920 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
922 interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
923 effect_func = (effect_func_t)rgblight_effect_rgbtest;
926 #ifdef RGBLIGHT_EFFECT_ALTERNATING
927 else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING){
929 effect_func = (effect_func_t)rgblight_effect_alternating;
932 if (animation_status.restart) {
933 animation_status.restart = false;
934 animation_status.last_timer = timer_read() - interval_time - 1;
935 animation_status.pos16 = 0; // restart signal to local each effect
937 if (timer_elapsed(animation_status.last_timer) >= interval_time) {
938 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
939 static uint16_t report_last_timer = 0;
940 static bool tick_flag = false;
944 //dprintf("rgblight animation tick\n");
945 if (timer_elapsed(report_last_timer) >= 30000) {
946 report_last_timer = timer_read();
947 dprintf("rgblight animation tick report to slave\n");
948 RGBLIGHT_SPLIT_ANIMATION_TICK;
951 oldpos16 = animation_status.pos16;
952 //dprintf("call effect function\n");
954 animation_status.last_timer += interval_time;
955 effect_func(&animation_status);
956 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
957 //dprintf("pos16, oldpos16 = %d %d\n",
958 // animation_status.pos16,oldpos16);
959 if (animation_status.pos16 == 0 && oldpos16 != 0) {
960 //dprintf("flag on\n");
968 #endif /* RGBLIGHT_USE_TIMER */
971 #ifdef RGBLIGHT_EFFECT_BREATHING
972 __attribute__ ((weak))
973 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
975 void rgblight_effect_breathing(animation_status_t *anim) {
978 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
979 val = (exp(sin((anim->pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
980 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
981 anim->pos = (anim->pos + 1) % 256;
985 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
986 __attribute__ ((weak))
987 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
989 void rgblight_effect_rainbow_mood(animation_status_t *anim) {
990 rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
991 anim->current_hue = (anim->current_hue + 1) % 360;
995 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
996 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
997 #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
1000 __attribute__ ((weak))
1001 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
1003 void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
1007 for (i = 0; i < RGBLED_NUM; i++) {
1008 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + anim->current_hue) % 360;
1009 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1013 if (anim->delta % 2) {
1014 anim->current_hue = (anim->current_hue + 1) % 360;
1016 if (anim->current_hue - 1 < 0) {
1017 anim->current_hue = 359;
1019 anim->current_hue = anim->current_hue - 1;
1025 #ifdef RGBLIGHT_EFFECT_SNAKE
1026 __attribute__ ((weak))
1027 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
1029 void rgblight_effect_snake(animation_status_t *anim) {
1030 static uint8_t pos = 0;
1033 int8_t increment = 1;
1035 if (anim->delta % 2) {
1039 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1040 if (anim->pos == 0) { // restart signal
1041 if (increment == 1) {
1042 pos = RGBLED_NUM - 1;
1050 for (i = 0; i < RGBLED_NUM; i++) {
1054 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
1055 k = pos + j * increment;
1060 sethsv(rgblight_config.hue, rgblight_config.sat,
1061 (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH),
1062 (LED_TYPE *)&led[i]);
1067 if (increment == 1) {
1069 pos = RGBLED_NUM - 1;
1070 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1075 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1080 pos = (pos + 1) % RGBLED_NUM;
1081 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1088 #ifdef RGBLIGHT_EFFECT_KNIGHT
1089 __attribute__ ((weak))
1090 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
1092 void rgblight_effect_knight(animation_status_t *anim) {
1094 static int8_t low_bound = 0;
1095 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
1096 static int8_t increment = 1;
1099 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1100 if (anim->pos == 0) { // restart signal
1103 high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
1107 // Set all the LEDs to 0
1108 for (i = 0; i < RGBLED_NUM; i++) {
1113 // Determine which LEDs should be lit up
1114 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
1115 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
1117 if (i >= low_bound && i <= high_bound) {
1118 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
1127 // Move from low_bound to high_bound changing the direction we increment each
1128 // time a boundary is hit.
1129 low_bound += increment;
1130 high_bound += increment;
1132 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
1133 increment = -increment;
1134 #if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1135 if (increment == 1) {
1143 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
1144 void rgblight_effect_christmas(animation_status_t *anim) {
1148 anim->current_offset = (anim->current_offset + 1) % 2;
1149 for (i = 0; i < RGBLED_NUM; i++) {
1150 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 120;
1151 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1157 #ifdef RGBLIGHT_EFFECT_RGB_TEST
1158 __attribute__ ((weak))
1159 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
1161 void rgblight_effect_rgbtest(animation_status_t *anim) {
1162 static uint8_t maxval = 0;
1163 uint8_t g; uint8_t r; uint8_t b;
1167 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
1171 switch( anim->pos ) {
1172 case 0: r = maxval; break;
1173 case 1: g = maxval; break;
1174 case 2: b = maxval; break;
1176 rgblight_setrgb(r, g, b);
1177 anim->pos = (anim->pos + 1) % 3;
1181 #ifdef RGBLIGHT_EFFECT_ALTERNATING
1182 void rgblight_effect_alternating(animation_status_t *anim) {
1184 for(int i = 0; i<RGBLED_NUM; i++){
1185 if(i<RGBLED_NUM/2 && anim->pos){
1186 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1187 }else if (i>=RGBLED_NUM/2 && !anim->pos){
1188 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
1190 sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
1194 anim->pos = (anim->pos + 1) % 2;