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"
33 #include "led_tables.h"
34 #include "lib/lib8tion/lib8tion.h"
35 #ifdef VELOCIKEY_ENABLE
36 # include "velocikey.h"
40 /* for split keyboard */
41 # define RGBLIGHT_SPLIT_SET_CHANGE_MODE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_MODE
42 # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_HSVS
43 # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS rgblight_status.change_flags |= (RGBLIGHT_STATUS_CHANGE_MODE | RGBLIGHT_STATUS_CHANGE_HSVS)
44 # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER
45 # define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK
47 # define RGBLIGHT_SPLIT_SET_CHANGE_MODE
48 # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS
49 # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
50 # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
51 # define RGBLIGHT_SPLIT_ANIMATION_TICK
54 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
55 #define _RGBM_SINGLE_DYNAMIC(sym)
56 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
57 #define _RGBM_MULTI_DYNAMIC(sym)
58 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##sym,
59 #define _RGBM_TMP_DYNAMIC(sym, msym)
60 static uint8_t static_effect_table
[] = {
61 #include "rgblight_modes.h"
64 #define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
65 #define _RGBM_SINGLE_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
66 #define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
67 #define _RGBM_MULTI_DYNAMIC(sym) RGBLIGHT_MODE_##sym,
68 #define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##msym,
69 #define _RGBM_TMP_DYNAMIC(sym, msym) RGBLIGHT_MODE_##msym,
70 static uint8_t mode_base_table
[] = {
71 0, // RGBLIGHT_MODE_zero
72 #include "rgblight_modes.h"
75 static inline int is_static_effect(uint8_t mode
) { return memchr(static_effect_table
, mode
, sizeof(static_effect_table
)) != NULL
; }
77 #ifdef RGBLIGHT_LED_MAP
78 const uint8_t led_map
[] PROGMEM
= RGBLIGHT_LED_MAP
;
81 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
82 __attribute__((weak
)) const uint8_t RGBLED_GRADIENT_RANGES
[] PROGMEM
= {255, 170, 127, 85, 64};
85 rgblight_config_t rgblight_config
;
86 rgblight_status_t rgblight_status
= {.timer_enabled
= false};
87 bool is_rgblight_initialized
= false;
89 #ifdef RGBLIGHT_USE_TIMER
90 animation_status_t animation_status
= {};
94 LED_TYPE led
[RGBLED_NUM
];
95 # define LED_ARRAY led
98 static uint8_t clipping_start_pos
= 0;
99 static uint8_t clipping_num_leds
= RGBLED_NUM
;
100 static uint8_t effect_start_pos
= 0;
101 static uint8_t effect_end_pos
= RGBLED_NUM
;
102 static uint8_t effect_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
;
109 void rgblight_set_effect_range(uint8_t start_pos
, uint8_t num_leds
) {
110 if (start_pos
>= RGBLED_NUM
) return;
111 if (start_pos
+ num_leds
> RGBLED_NUM
) return;
112 effect_start_pos
= start_pos
;
113 effect_end_pos
= start_pos
+ num_leds
;
114 effect_num_leds
= num_leds
;
117 void sethsv_raw(uint8_t hue
, uint8_t sat
, uint8_t val
, LED_TYPE
*led1
) {
118 HSV hsv
= {hue
, sat
, val
};
119 RGB rgb
= hsv_to_rgb(hsv
);
120 setrgb(rgb
.r
, rgb
.g
, rgb
.b
, led1
);
123 void sethsv(uint8_t hue
, uint8_t sat
, uint8_t val
, LED_TYPE
*led1
) { sethsv_raw(hue
, sat
, val
> RGBLIGHT_LIMIT_VAL
? RGBLIGHT_LIMIT_VAL
: val
, led1
); }
125 void setrgb(uint8_t r
, uint8_t g
, uint8_t b
, LED_TYPE
*led1
) {
134 void rgblight_check_config(void) {
135 /* Add some out of bound checks for RGB light config */
137 if (rgblight_config
.mode
< RGBLIGHT_MODE_STATIC_LIGHT
) {
138 rgblight_config
.mode
= RGBLIGHT_MODE_STATIC_LIGHT
;
139 } else if (rgblight_config
.mode
> RGBLIGHT_MODES
) {
140 rgblight_config
.mode
= RGBLIGHT_MODES
;
143 if (rgblight_config
.val
> RGBLIGHT_LIMIT_VAL
) {
144 rgblight_config
.val
= RGBLIGHT_LIMIT_VAL
;
148 uint32_t eeconfig_read_rgblight(void) {
149 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
150 return eeprom_read_dword(EECONFIG_RGBLIGHT
);
156 void eeconfig_update_rgblight(uint32_t val
) {
157 #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
158 rgblight_check_config();
159 eeprom_update_dword(EECONFIG_RGBLIGHT
, val
);
163 void eeconfig_update_rgblight_default(void) {
164 rgblight_config
.enable
= 1;
165 rgblight_config
.mode
= RGBLIGHT_MODE_STATIC_LIGHT
;
166 rgblight_config
.hue
= 0;
167 rgblight_config
.sat
= UINT8_MAX
;
168 rgblight_config
.val
= RGBLIGHT_LIMIT_VAL
;
169 rgblight_config
.speed
= 0;
170 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
;
171 eeconfig_update_rgblight(rgblight_config
.raw
);
174 void eeconfig_debug_rgblight(void) {
175 dprintf("rgblight_config EEPROM:\n");
176 dprintf("rgblight_config.enable = %d\n", rgblight_config
.enable
);
177 dprintf("rghlight_config.mode = %d\n", rgblight_config
.mode
);
178 dprintf("rgblight_config.hue = %d\n", rgblight_config
.hue
);
179 dprintf("rgblight_config.sat = %d\n", rgblight_config
.sat
);
180 dprintf("rgblight_config.val = %d\n", rgblight_config
.val
);
181 dprintf("rgblight_config.speed = %d\n", rgblight_config
.speed
);
184 void rgblight_init(void) {
185 /* if already initialized, don't do it again.
186 If you must do it again, extern this and set to false, first.
187 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
188 if (is_rgblight_initialized
) {
192 dprintf("rgblight_init called.\n");
193 dprintf("rgblight_init start!\n");
194 if (!eeconfig_is_enabled()) {
195 dprintf("rgblight_init eeconfig is not enabled.\n");
197 eeconfig_update_rgblight_default();
199 rgblight_config
.raw
= eeconfig_read_rgblight();
200 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
;
201 if (!rgblight_config
.mode
) {
202 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
203 eeconfig_update_rgblight_default();
204 rgblight_config
.raw
= eeconfig_read_rgblight();
206 rgblight_check_config();
208 eeconfig_debug_rgblight(); // display current eeprom values
210 #ifdef RGBLIGHT_USE_TIMER
211 rgblight_timer_init(); // setup the timer
214 if (rgblight_config
.enable
) {
215 rgblight_mode_noeeprom(rgblight_config
.mode
);
218 is_rgblight_initialized
= true;
221 uint32_t rgblight_read_dword(void) { return rgblight_config
.raw
; }
223 void rgblight_update_dword(uint32_t dword
) {
224 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS
;
225 rgblight_config
.raw
= dword
;
226 if (rgblight_config
.enable
)
227 rgblight_mode_noeeprom(rgblight_config
.mode
);
229 #ifdef RGBLIGHT_USE_TIMER
230 rgblight_timer_disable();
236 void rgblight_increase(void) {
238 if (rgblight_config
.mode
< RGBLIGHT_MODES
) {
239 mode
= rgblight_config
.mode
+ 1;
243 void rgblight_decrease(void) {
245 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
246 if (rgblight_config
.mode
> RGBLIGHT_MODE_STATIC_LIGHT
) {
247 mode
= rgblight_config
.mode
- 1;
251 void rgblight_step_helper(bool write_to_eeprom
) {
253 mode
= rgblight_config
.mode
+ 1;
254 if (mode
> RGBLIGHT_MODES
) {
257 rgblight_mode_eeprom_helper(mode
, write_to_eeprom
);
259 void rgblight_step_noeeprom(void) { rgblight_step_helper(false); }
260 void rgblight_step(void) { rgblight_step_helper(true); }
261 void rgblight_step_reverse_helper(bool write_to_eeprom
) {
263 mode
= rgblight_config
.mode
- 1;
265 mode
= RGBLIGHT_MODES
;
267 rgblight_mode_eeprom_helper(mode
, write_to_eeprom
);
269 void rgblight_step_reverse_noeeprom(void) { rgblight_step_reverse_helper(false); }
270 void rgblight_step_reverse(void) { rgblight_step_reverse_helper(true); }
272 uint8_t rgblight_get_mode(void) {
273 if (!rgblight_config
.enable
) {
277 return rgblight_config
.mode
;
280 void rgblight_mode_eeprom_helper(uint8_t mode
, bool write_to_eeprom
) {
281 if (!rgblight_config
.enable
) {
284 if (mode
< RGBLIGHT_MODE_STATIC_LIGHT
) {
285 rgblight_config
.mode
= RGBLIGHT_MODE_STATIC_LIGHT
;
286 } else if (mode
> RGBLIGHT_MODES
) {
287 rgblight_config
.mode
= RGBLIGHT_MODES
;
289 rgblight_config
.mode
= mode
;
291 RGBLIGHT_SPLIT_SET_CHANGE_MODE
;
292 if (write_to_eeprom
) {
293 eeconfig_update_rgblight(rgblight_config
.raw
);
294 dprintf("rgblight mode [EEPROM]: %u\n", rgblight_config
.mode
);
296 dprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config
.mode
);
298 if (is_static_effect(rgblight_config
.mode
)) {
299 #ifdef RGBLIGHT_USE_TIMER
300 rgblight_timer_disable();
303 #ifdef RGBLIGHT_USE_TIMER
304 rgblight_timer_enable();
307 #ifdef RGBLIGHT_USE_TIMER
308 animation_status
.restart
= true;
310 rgblight_sethsv_noeeprom(rgblight_config
.hue
, rgblight_config
.sat
, rgblight_config
.val
);
313 void rgblight_mode(uint8_t mode
) { rgblight_mode_eeprom_helper(mode
, true); }
315 void rgblight_mode_noeeprom(uint8_t mode
) { rgblight_mode_eeprom_helper(mode
, false); }
317 void rgblight_toggle(void) {
318 dprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config
.enable
);
319 if (rgblight_config
.enable
) {
326 void rgblight_toggle_noeeprom(void) {
327 dprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config
.enable
);
328 if (rgblight_config
.enable
) {
329 rgblight_disable_noeeprom();
331 rgblight_enable_noeeprom();
335 void rgblight_enable(void) {
336 rgblight_config
.enable
= 1;
337 // No need to update EEPROM here. rgblight_mode() will do that, actually
338 // eeconfig_update_rgblight(rgblight_config.raw);
339 dprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config
.enable
);
340 rgblight_mode(rgblight_config
.mode
);
343 void rgblight_enable_noeeprom(void) {
344 rgblight_config
.enable
= 1;
345 dprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config
.enable
);
346 rgblight_mode_noeeprom(rgblight_config
.mode
);
349 void rgblight_disable(void) {
350 rgblight_config
.enable
= 0;
351 eeconfig_update_rgblight(rgblight_config
.raw
);
352 dprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config
.enable
);
353 #ifdef RGBLIGHT_USE_TIMER
354 rgblight_timer_disable();
356 RGBLIGHT_SPLIT_SET_CHANGE_MODE
;
361 void rgblight_disable_noeeprom(void) {
362 rgblight_config
.enable
= 0;
363 dprintf("rgblight disable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config
.enable
);
364 #ifdef RGBLIGHT_USE_TIMER
365 rgblight_timer_disable();
367 RGBLIGHT_SPLIT_SET_CHANGE_MODE
;
372 void rgblight_increase_hue_helper(bool write_to_eeprom
) {
373 uint8_t hue
= rgblight_config
.hue
+ RGBLIGHT_HUE_STEP
;
374 rgblight_sethsv_eeprom_helper(hue
, rgblight_config
.sat
, rgblight_config
.val
, write_to_eeprom
);
376 void rgblight_increase_hue_noeeprom(void) { rgblight_increase_hue_helper(false); }
377 void rgblight_increase_hue(void) { rgblight_increase_hue_helper(true); }
378 void rgblight_decrease_hue_helper(bool write_to_eeprom
) {
379 uint8_t hue
= rgblight_config
.hue
- RGBLIGHT_HUE_STEP
;
380 rgblight_sethsv_eeprom_helper(hue
, rgblight_config
.sat
, rgblight_config
.val
, write_to_eeprom
);
382 void rgblight_decrease_hue_noeeprom(void) { rgblight_decrease_hue_helper(false); }
383 void rgblight_decrease_hue(void) { rgblight_decrease_hue_helper(true); }
384 void rgblight_increase_sat_helper(bool write_to_eeprom
) {
385 uint8_t sat
= qadd8(rgblight_config
.sat
, RGBLIGHT_SAT_STEP
);
386 rgblight_sethsv_eeprom_helper(rgblight_config
.hue
, sat
, rgblight_config
.val
, write_to_eeprom
);
388 void rgblight_increase_sat_noeeprom(void) { rgblight_increase_sat_helper(false); }
389 void rgblight_increase_sat(void) { rgblight_increase_sat_helper(true); }
390 void rgblight_decrease_sat_helper(bool write_to_eeprom
) {
391 uint8_t sat
= qsub8(rgblight_config
.sat
, RGBLIGHT_SAT_STEP
);
392 rgblight_sethsv_eeprom_helper(rgblight_config
.hue
, sat
, rgblight_config
.val
, write_to_eeprom
);
394 void rgblight_decrease_sat_noeeprom(void) { rgblight_decrease_sat_helper(false); }
395 void rgblight_decrease_sat(void) { rgblight_decrease_sat_helper(true); }
396 void rgblight_increase_val_helper(bool write_to_eeprom
) {
397 uint8_t val
= qadd8(rgblight_config
.val
, RGBLIGHT_VAL_STEP
);
398 rgblight_sethsv_eeprom_helper(rgblight_config
.hue
, rgblight_config
.sat
, val
, write_to_eeprom
);
400 void rgblight_increase_val_noeeprom(void) { rgblight_increase_val_helper(false); }
401 void rgblight_increase_val(void) { rgblight_increase_val_helper(true); }
402 void rgblight_decrease_val_helper(bool write_to_eeprom
) {
403 uint8_t val
= qsub8(rgblight_config
.val
, RGBLIGHT_VAL_STEP
);
404 rgblight_sethsv_eeprom_helper(rgblight_config
.hue
, rgblight_config
.sat
, val
, write_to_eeprom
);
406 void rgblight_decrease_val_noeeprom(void) { rgblight_decrease_val_helper(false); }
407 void rgblight_decrease_val(void) { rgblight_decrease_val_helper(true); }
408 void rgblight_increase_speed(void) {
409 if (rgblight_config
.speed
< 3) rgblight_config
.speed
++;
410 // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
411 eeconfig_update_rgblight(rgblight_config
.raw
); // EECONFIG needs to be increased to support this
414 void rgblight_decrease_speed(void) {
415 if (rgblight_config
.speed
> 0) rgblight_config
.speed
--;
416 // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
417 eeconfig_update_rgblight(rgblight_config
.raw
); // EECONFIG needs to be increased to support this
420 void rgblight_sethsv_noeeprom_old(uint8_t hue
, uint8_t sat
, uint8_t val
) {
421 if (rgblight_config
.enable
) {
423 sethsv(hue
, sat
, val
, &tmp_led
);
424 rgblight_setrgb(tmp_led
.r
, tmp_led
.g
, tmp_led
.b
);
428 void rgblight_sethsv_eeprom_helper(uint8_t hue
, uint8_t sat
, uint8_t val
, bool write_to_eeprom
) {
429 if (rgblight_config
.enable
) {
430 rgblight_status
.base_mode
= mode_base_table
[rgblight_config
.mode
];
431 if (rgblight_config
.mode
== RGBLIGHT_MODE_STATIC_LIGHT
) {
434 sethsv(hue
, sat
, val
, &tmp_led
);
435 rgblight_setrgb(tmp_led
.r
, tmp_led
.g
, tmp_led
.b
);
437 // all LEDs in same color
438 if (1 == 0) { // dummy
440 #ifdef RGBLIGHT_EFFECT_BREATHING
441 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_BREATHING
) {
442 // breathing mode, ignore the change of val, use in memory value instead
443 val
= rgblight_config
.val
;
446 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
447 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_RAINBOW_MOOD
) {
448 // rainbow mood, ignore the change of hue
449 hue
= rgblight_config
.hue
;
452 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
453 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_RAINBOW_SWIRL
) {
454 // rainbow swirl, ignore the change of hue
455 hue
= rgblight_config
.hue
;
458 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
459 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_STATIC_GRADIENT
) {
461 uint8_t delta
= rgblight_config
.mode
- rgblight_status
.base_mode
;
462 bool direction
= (delta
% 2) == 0;
464 // probably due to how pgm_read_word is defined for ARM, but the ARM compiler really hates this line
465 uint8_t range
= pgm_read_word(&RGBLED_GRADIENT_RANGES
[delta
/ 2]);
467 uint8_t range
= RGBLED_GRADIENT_RANGES
[delta
/ 2];
469 for (uint8_t i
= 0; i
< effect_num_leds
; i
++) {
470 uint8_t _hue
= ((uint16_t)i
* (uint16_t)range
) / effect_num_leds
;
476 dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i
, _hue
, direction
, range
);
477 sethsv(_hue
, sat
, val
, (LED_TYPE
*)&led
[i
+ effect_start_pos
]);
483 #ifdef RGBLIGHT_SPLIT
484 if (rgblight_config
.hue
!= hue
|| rgblight_config
.sat
!= sat
|| rgblight_config
.val
!= val
) {
485 RGBLIGHT_SPLIT_SET_CHANGE_HSVS
;
488 rgblight_config
.hue
= hue
;
489 rgblight_config
.sat
= sat
;
490 rgblight_config
.val
= val
;
491 if (write_to_eeprom
) {
492 eeconfig_update_rgblight(rgblight_config
.raw
);
493 dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config
.hue
, rgblight_config
.sat
, rgblight_config
.val
);
495 dprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config
.hue
, rgblight_config
.sat
, rgblight_config
.val
);
500 void rgblight_sethsv(uint8_t hue
, uint8_t sat
, uint8_t val
) { rgblight_sethsv_eeprom_helper(hue
, sat
, val
, true); }
502 void rgblight_sethsv_noeeprom(uint8_t hue
, uint8_t sat
, uint8_t val
) { rgblight_sethsv_eeprom_helper(hue
, sat
, val
, false); }
504 uint8_t rgblight_get_hue(void) { return rgblight_config
.hue
; }
506 uint8_t rgblight_get_sat(void) { return rgblight_config
.sat
; }
508 uint8_t rgblight_get_val(void) { return rgblight_config
.val
; }
510 void rgblight_setrgb(uint8_t r
, uint8_t g
, uint8_t b
) {
511 if (!rgblight_config
.enable
) {
515 for (uint8_t i
= effect_start_pos
; i
< effect_end_pos
; i
++) {
526 void rgblight_setrgb_at(uint8_t r
, uint8_t g
, uint8_t b
, uint8_t index
) {
527 if (!rgblight_config
.enable
|| index
>= RGBLED_NUM
) {
540 void rgblight_sethsv_at(uint8_t hue
, uint8_t sat
, uint8_t val
, uint8_t index
) {
541 if (!rgblight_config
.enable
) {
546 sethsv(hue
, sat
, val
, &tmp_led
);
547 rgblight_setrgb_at(tmp_led
.r
, tmp_led
.g
, tmp_led
.b
, index
);
550 #if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
552 static uint8_t get_interval_time(const uint8_t *default_interval_address
, uint8_t velocikey_min
, uint8_t velocikey_max
) {
554 # ifdef VELOCIKEY_ENABLE
555 velocikey_enabled() ? velocikey_match_speed(velocikey_min
, velocikey_max
) :
557 pgm_read_byte(default_interval_address
);
562 void rgblight_setrgb_range(uint8_t r
, uint8_t g
, uint8_t b
, uint8_t start
, uint8_t end
) {
563 if (!rgblight_config
.enable
|| start
< 0 || start
>= end
|| end
> RGBLED_NUM
) {
567 for (uint8_t i
= start
; i
< end
; i
++) {
579 void rgblight_sethsv_range(uint8_t hue
, uint8_t sat
, uint8_t val
, uint8_t start
, uint8_t end
) {
580 if (!rgblight_config
.enable
) {
585 sethsv(hue
, sat
, val
, &tmp_led
);
586 rgblight_setrgb_range(tmp_led
.r
, tmp_led
.g
, tmp_led
.b
, start
, end
);
589 #ifndef RGBLIGHT_SPLIT
590 void rgblight_setrgb_master(uint8_t r
, uint8_t g
, uint8_t b
) { rgblight_setrgb_range(r
, g
, b
, 0, (uint8_t)RGBLED_NUM
/ 2); }
592 void rgblight_setrgb_slave(uint8_t r
, uint8_t g
, uint8_t b
) { rgblight_setrgb_range(r
, g
, b
, (uint8_t)RGBLED_NUM
/ 2, (uint8_t)RGBLED_NUM
); }
594 void rgblight_sethsv_master(uint8_t hue
, uint8_t sat
, uint8_t val
) { rgblight_sethsv_range(hue
, sat
, val
, 0, (uint8_t)RGBLED_NUM
/ 2); }
596 void rgblight_sethsv_slave(uint8_t hue
, uint8_t sat
, uint8_t val
) { rgblight_sethsv_range(hue
, sat
, val
, (uint8_t)RGBLED_NUM
/ 2, (uint8_t)RGBLED_NUM
); }
597 #endif // ifndef RGBLIGHT_SPLIT
599 #ifndef RGBLIGHT_CUSTOM_DRIVER
600 void rgblight_set(void) {
602 uint16_t num_leds
= clipping_num_leds
;
604 if (!rgblight_config
.enable
) {
605 for (uint8_t i
= effect_start_pos
; i
< effect_end_pos
; i
++) {
614 # ifdef RGBLIGHT_LED_MAP
615 LED_TYPE led0
[RGBLED_NUM
];
616 for (uint8_t i
= 0; i
< RGBLED_NUM
; i
++) {
617 led0
[i
] = led
[pgm_read_byte(&led_map
[i
])];
619 start_led
= led0
+ clipping_start_pos
;
621 start_led
= led
+ clipping_start_pos
;
623 ws2812_setleds(start_led
, num_leds
);
627 #ifdef RGBLIGHT_SPLIT
628 /* for split keyboard master side */
629 uint8_t rgblight_get_change_flags(void) { return rgblight_status
.change_flags
; }
631 void rgblight_clear_change_flags(void) { rgblight_status
.change_flags
= 0; }
633 void rgblight_get_syncinfo(rgblight_syncinfo_t
*syncinfo
) {
634 syncinfo
->config
= rgblight_config
;
635 syncinfo
->status
= rgblight_status
;
638 /* for split keyboard slave side */
639 void rgblight_update_sync(rgblight_syncinfo_t
*syncinfo
, bool write_to_eeprom
) {
640 if (syncinfo
->status
.change_flags
& RGBLIGHT_STATUS_CHANGE_MODE
) {
641 if (syncinfo
->config
.enable
) {
642 rgblight_config
.enable
= 1; // == rgblight_enable_noeeprom();
643 rgblight_mode_eeprom_helper(syncinfo
->config
.mode
, write_to_eeprom
);
645 rgblight_disable_noeeprom();
648 if (syncinfo
->status
.change_flags
& RGBLIGHT_STATUS_CHANGE_HSVS
) {
649 rgblight_sethsv_eeprom_helper(syncinfo
->config
.hue
, syncinfo
->config
.sat
, syncinfo
->config
.val
, write_to_eeprom
);
650 // rgblight_config.speed = config->speed; // NEED???
652 # ifdef RGBLIGHT_USE_TIMER
653 if (syncinfo
->status
.change_flags
& RGBLIGHT_STATUS_CHANGE_TIMER
) {
654 if (syncinfo
->status
.timer_enabled
) {
655 rgblight_timer_enable();
657 rgblight_timer_disable();
660 # ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
661 if (syncinfo
->status
.change_flags
& RGBLIGHT_STATUS_ANIMATION_TICK
) {
662 animation_status
.restart
= true;
664 # endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
665 # endif /* RGBLIGHT_USE_TIMER */
667 #endif /* RGBLIGHT_SPLIT */
669 #ifdef RGBLIGHT_USE_TIMER
671 typedef void (*effect_func_t
)(animation_status_t
*anim
);
673 // Animation timer -- use system timer (AVR Timer0)
674 void rgblight_timer_init(void) {
675 // OLD!!!! Animation timer -- AVR Timer3
676 // static uint8_t rgblight_timer_is_init = 0;
677 // if (rgblight_timer_is_init) {
680 // rgblight_timer_is_init = 1;
681 // /* Timer 3 setup */
682 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
683 // | _BV(CS30); // Clock selelct: clk/1
684 // /* Set TOP value */
685 // uint8_t sreg = SREG;
687 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
688 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
691 rgblight_status
.timer_enabled
= false;
692 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
;
694 void rgblight_timer_enable(void) {
695 if (!is_static_effect(rgblight_config
.mode
)) {
696 rgblight_status
.timer_enabled
= true;
698 animation_status
.last_timer
= timer_read();
699 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
;
700 dprintf("rgblight timer enabled.\n");
702 void rgblight_timer_disable(void) {
703 rgblight_status
.timer_enabled
= false;
704 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE
;
705 dprintf("rgblight timer disable.\n");
707 void rgblight_timer_toggle(void) {
708 dprintf("rgblight timer toggle.\n");
709 if (rgblight_status
.timer_enabled
) {
710 rgblight_timer_disable();
712 rgblight_timer_enable();
716 void rgblight_show_solid_color(uint8_t r
, uint8_t g
, uint8_t b
) {
718 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT
);
719 rgblight_setrgb(r
, g
, b
);
722 static void rgblight_effect_dummy(animation_status_t
*anim
) {
725 dprintf("rgblight_task() what happened?\n");
726 dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
727 dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
728 rgblight_config.mode, rgblight_status.base_mode,
729 rgblight_status.timer_enabled);
730 dprintf("last_timer = %d\n",anim->last_timer);
734 void rgblight_task(void) {
735 if (rgblight_status
.timer_enabled
) {
736 effect_func_t effect_func
= rgblight_effect_dummy
;
737 uint16_t interval_time
= 2000; // dummy interval
738 uint8_t delta
= rgblight_config
.mode
- rgblight_status
.base_mode
;
739 animation_status
.delta
= delta
;
741 // static light mode, do nothing here
742 if (1 == 0) { // dummy
744 # ifdef RGBLIGHT_EFFECT_BREATHING
745 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_BREATHING
) {
747 interval_time
= get_interval_time(&RGBLED_BREATHING_INTERVALS
[delta
], 1, 100);
748 effect_func
= rgblight_effect_breathing
;
751 # ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
752 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_RAINBOW_MOOD
) {
754 interval_time
= get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS
[delta
], 5, 100);
755 effect_func
= rgblight_effect_rainbow_mood
;
758 # ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
759 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_RAINBOW_SWIRL
) {
760 // rainbow swirl mode
761 interval_time
= get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS
[delta
/ 2], 1, 100);
762 effect_func
= rgblight_effect_rainbow_swirl
;
765 # ifdef RGBLIGHT_EFFECT_SNAKE
766 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_SNAKE
) {
768 interval_time
= get_interval_time(&RGBLED_SNAKE_INTERVALS
[delta
/ 2], 1, 200);
769 effect_func
= rgblight_effect_snake
;
772 # ifdef RGBLIGHT_EFFECT_KNIGHT
773 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_KNIGHT
) {
775 interval_time
= get_interval_time(&RGBLED_KNIGHT_INTERVALS
[delta
], 5, 100);
776 effect_func
= rgblight_effect_knight
;
779 # ifdef RGBLIGHT_EFFECT_CHRISTMAS
780 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_CHRISTMAS
) {
782 interval_time
= RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL
;
783 effect_func
= (effect_func_t
)rgblight_effect_christmas
;
786 # ifdef RGBLIGHT_EFFECT_RGB_TEST
787 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_RGB_TEST
) {
789 interval_time
= pgm_read_word(&RGBLED_RGBTEST_INTERVALS
[0]);
790 effect_func
= (effect_func_t
)rgblight_effect_rgbtest
;
793 # ifdef RGBLIGHT_EFFECT_ALTERNATING
794 else if (rgblight_status
.base_mode
== RGBLIGHT_MODE_ALTERNATING
) {
796 effect_func
= (effect_func_t
)rgblight_effect_alternating
;
799 if (animation_status
.restart
) {
800 animation_status
.restart
= false;
801 animation_status
.last_timer
= timer_read() - interval_time
- 1;
802 animation_status
.pos16
= 0; // restart signal to local each effect
804 if (timer_elapsed(animation_status
.last_timer
) >= interval_time
) {
805 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
806 static uint16_t report_last_timer
= 0;
807 static bool tick_flag
= false;
811 if (timer_elapsed(report_last_timer
) >= 30000) {
812 report_last_timer
= timer_read();
813 dprintf("rgblight animation tick report to slave\n");
814 RGBLIGHT_SPLIT_ANIMATION_TICK
;
817 oldpos16
= animation_status
.pos16
;
819 animation_status
.last_timer
+= interval_time
;
820 effect_func(&animation_status
);
821 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
822 if (animation_status
.pos16
== 0 && oldpos16
!= 0) {
830 #endif /* RGBLIGHT_USE_TIMER */
833 #ifdef RGBLIGHT_EFFECT_BREATHING
835 # ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
836 # ifndef RGBLIGHT_BREATHE_TABLE_SIZE
837 # define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
839 # include <rgblight_breathe_table.h>
842 __attribute__((weak
)) const uint8_t RGBLED_BREATHING_INTERVALS
[] PROGMEM
= {30, 20, 10, 5};
844 void rgblight_effect_breathing(animation_status_t
*anim
) {
847 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
848 # ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
849 val
= pgm_read_byte(&rgblight_effect_breathe_table
[anim
->pos
/ table_scale
]);
851 val
= (exp(sin((anim
->pos
/ 255.0) * M_PI
)) - RGBLIGHT_EFFECT_BREATHE_CENTER
/ M_E
) * (RGBLIGHT_EFFECT_BREATHE_MAX
/ (M_E
- 1 / M_E
));
853 rgblight_sethsv_noeeprom_old(rgblight_config
.hue
, rgblight_config
.sat
, val
);
854 anim
->pos
= (anim
->pos
+ 1);
858 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
859 __attribute__((weak
)) const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS
[] PROGMEM
= {120, 60, 30};
861 void rgblight_effect_rainbow_mood(animation_status_t
*anim
) {
862 rgblight_sethsv_noeeprom_old(anim
->current_hue
, rgblight_config
.sat
, rgblight_config
.val
);
867 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
868 # ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
869 # define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
872 __attribute__((weak
)) const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS
[] PROGMEM
= {100, 50, 20};
874 void rgblight_effect_rainbow_swirl(animation_status_t
*anim
) {
878 for (i
= 0; i
< effect_num_leds
; i
++) {
879 hue
= (RGBLIGHT_RAINBOW_SWIRL_RANGE
/ effect_num_leds
* i
+ anim
->current_hue
);
880 sethsv(hue
, rgblight_config
.sat
, rgblight_config
.val
, (LED_TYPE
*)&led
[i
+ effect_start_pos
]);
884 if (anim
->delta
% 2) {
892 #ifdef RGBLIGHT_EFFECT_SNAKE
893 __attribute__((weak
)) const uint8_t RGBLED_SNAKE_INTERVALS
[] PROGMEM
= {100, 50, 20};
895 void rgblight_effect_snake(animation_status_t
*anim
) {
896 static uint8_t pos
= 0;
899 int8_t increment
= 1;
901 if (anim
->delta
% 2) {
905 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
906 if (anim
->pos
== 0) { // restart signal
907 if (increment
== 1) {
908 pos
= effect_num_leds
- 1;
916 for (i
= 0; i
< effect_num_leds
; i
++) {
917 LED_TYPE
*ledp
= led
+ i
+ effect_start_pos
;
924 for (j
= 0; j
< RGBLIGHT_EFFECT_SNAKE_LENGTH
; j
++) {
925 k
= pos
+ j
* increment
;
926 if (k
> RGBLED_NUM
) {
930 k
= k
+ effect_num_leds
;
933 sethsv(rgblight_config
.hue
, rgblight_config
.sat
, (uint8_t)(rgblight_config
.val
* (RGBLIGHT_EFFECT_SNAKE_LENGTH
- j
) / RGBLIGHT_EFFECT_SNAKE_LENGTH
), ledp
);
938 if (increment
== 1) {
940 pos
= effect_num_leds
- 1;
941 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
946 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
951 pos
= (pos
+ 1) % effect_num_leds
;
952 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
959 #ifdef RGBLIGHT_EFFECT_KNIGHT
960 __attribute__((weak
)) const uint8_t RGBLED_KNIGHT_INTERVALS
[] PROGMEM
= {127, 63, 31};
962 void rgblight_effect_knight(animation_status_t
*anim
) {
963 static int8_t low_bound
= 0;
964 static int8_t high_bound
= RGBLIGHT_EFFECT_KNIGHT_LENGTH
- 1;
965 static int8_t increment
= 1;
968 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
969 if (anim
->pos
== 0) { // restart signal
972 high_bound
= RGBLIGHT_EFFECT_KNIGHT_LENGTH
- 1;
976 // Set all the LEDs to 0
977 for (i
= effect_start_pos
; i
< effect_end_pos
; i
++) {
985 // Determine which LEDs should be lit up
986 for (i
= 0; i
< RGBLIGHT_EFFECT_KNIGHT_LED_NUM
; i
++) {
987 cur
= (i
+ RGBLIGHT_EFFECT_KNIGHT_OFFSET
) % effect_num_leds
+ effect_start_pos
;
989 if (i
>= low_bound
&& i
<= high_bound
) {
990 sethsv(rgblight_config
.hue
, rgblight_config
.sat
, rgblight_config
.val
, (LED_TYPE
*)&led
[cur
]);
1002 // Move from low_bound to high_bound changing the direction we increment each
1003 // time a boundary is hit.
1004 low_bound
+= increment
;
1005 high_bound
+= increment
;
1007 if (high_bound
<= 0 || low_bound
>= RGBLIGHT_EFFECT_KNIGHT_LED_NUM
- 1) {
1008 increment
= -increment
;
1009 # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
1010 if (increment
== 1) {
1018 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
1019 void rgblight_effect_christmas(animation_status_t
*anim
) {
1023 anim
->current_offset
= (anim
->current_offset
+ 1) % 2;
1024 for (i
= 0; i
< effect_num_leds
; i
++) {
1025 hue
= 0 + ((i
/ RGBLIGHT_EFFECT_CHRISTMAS_STEP
+ anim
->current_offset
) % 2) * 85;
1026 sethsv(hue
, rgblight_config
.sat
, rgblight_config
.val
, (LED_TYPE
*)&led
[i
+ effect_start_pos
]);
1032 #ifdef RGBLIGHT_EFFECT_RGB_TEST
1033 __attribute__((weak
)) const uint16_t RGBLED_RGBTEST_INTERVALS
[] PROGMEM
= {1024};
1035 void rgblight_effect_rgbtest(animation_status_t
*anim
) {
1036 static uint8_t maxval
= 0;
1043 sethsv(0, 255, RGBLIGHT_LIMIT_VAL
, &tmp_led
);
1047 switch (anim
->pos
) {
1058 rgblight_setrgb(r
, g
, b
);
1059 anim
->pos
= (anim
->pos
+ 1) % 3;
1063 #ifdef RGBLIGHT_EFFECT_ALTERNATING
1064 void rgblight_effect_alternating(animation_status_t
*anim
) {
1065 for (int i
= 0; i
< effect_num_leds
; i
++) {
1066 LED_TYPE
*ledp
= led
+ i
+ effect_start_pos
;
1067 if (i
< effect_num_leds
/ 2 && anim
->pos
) {
1068 sethsv(rgblight_config
.hue
, rgblight_config
.sat
, rgblight_config
.val
, ledp
);
1069 } else if (i
>= effect_num_leds
/ 2 && !anim
->pos
) {
1070 sethsv(rgblight_config
.hue
, rgblight_config
.sat
, rgblight_config
.val
, ledp
);
1072 sethsv(rgblight_config
.hue
, rgblight_config
.sat
, 0, ledp
);
1076 anim
->pos
= (anim
->pos
+ 1) % 2;