clang-format changes
[jackhill/qmk/firmware.git] / quantum / rgblight.c
CommitLineData
23839b8c 1/* Copyright 2016-2017 Yang Liu
2 *
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.
7 *
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.
12 *
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/>.
15 */
ad49db8c 16#include <math.h>
6d6d91c8 17#include <string.h>
ade22f8e 18#ifdef __AVR__
b624f32f 19# include <avr/eeprom.h>
20# include <avr/interrupt.h>
ade22f8e 21#endif
0f507f01 22#ifdef STM32_EEPROM_ENABLE
b624f32f 23# include "hal.h"
24# include "eeprom.h"
25# include "eeprom_stm32.h"
0f507f01 26#endif
ade22f8e 27#include "wait.h"
0a40654b
YL
28#include "progmem.h"
29#include "timer.h"
30#include "rgblight.h"
22ba36a4 31#include "color.h"
0a40654b 32#include "debug.h"
f113f954 33#include "led_tables.h"
22ba36a4 34#include "lib/lib8tion/lib8tion.h"
c1c5922a 35#ifdef VELOCIKEY_ENABLE
b624f32f 36# include "velocikey.h"
c1c5922a 37#endif
0a40654b 38
dfab177f 39#ifdef RGBLIGHT_SPLIT
b624f32f 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
dfab177f 46#else
b624f32f 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
dfab177f
TI
52#endif
53
b624f32f 54#define _RGBM_SINGLE_STATIC(sym) RGBLIGHT_MODE_##sym,
6d6d91c8 55#define _RGBM_SINGLE_DYNAMIC(sym)
b624f32f 56#define _RGBM_MULTI_STATIC(sym) RGBLIGHT_MODE_##sym,
6d6d91c8 57#define _RGBM_MULTI_DYNAMIC(sym)
b624f32f 58#define _RGBM_TMP_STATIC(sym, msym) RGBLIGHT_MODE_##sym,
dfab177f 59#define _RGBM_TMP_DYNAMIC(sym, msym)
b624f32f 60static uint8_t static_effect_table[] = {
dfab177f
TI
61#include "rgblight_modes.h"
62};
63
b624f32f 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,
70static uint8_t mode_base_table[] = {
71 0, // RGBLIGHT_MODE_zero
dfab177f 72#include "rgblight_modes.h"
6d6d91c8
TI
73};
74
b624f32f 75static inline int is_static_effect(uint8_t mode) { return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL; }
6d6d91c8 76
f7fd7f67
TI
77#ifdef RGBLIGHT_LED_MAP
78const uint8_t led_map[] PROGMEM = RGBLIGHT_LED_MAP;
79#endif
80
6d6d91c8 81#ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
b624f32f 82__attribute__((weak)) const uint8_t RGBLED_GRADIENT_RANGES[] PROGMEM = {255, 170, 127, 85, 64};
6d6d91c8 83#endif
0a40654b
YL
84
85rgblight_config_t rgblight_config;
b624f32f 86rgblight_status_t rgblight_status = {.timer_enabled = false};
87bool is_rgblight_initialized = false;
0a40654b 88
dfab177f
TI
89#ifdef RGBLIGHT_USE_TIMER
90animation_status_t animation_status = {};
91#endif
92
5fcd744d 93#ifndef LED_ARRAY
e9f74875 94LED_TYPE led[RGBLED_NUM];
b624f32f 95# define LED_ARRAY led
5fcd744d
X
96#endif
97
f077204f 98static uint8_t clipping_start_pos = 0;
b624f32f 99static uint8_t clipping_num_leds = RGBLED_NUM;
100static uint8_t effect_start_pos = 0;
101static uint8_t effect_end_pos = RGBLED_NUM;
102static uint8_t effect_num_leds = RGBLED_NUM;
f077204f
D
103
104void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) {
b624f32f 105 clipping_start_pos = start_pos;
106 clipping_num_leds = num_leds;
f077204f
D
107}
108
670a9b7f 109void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds) {
b624f32f 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;
670a9b7f 115}
f077204f 116
670a9b7f 117void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
b624f32f 118 HSV hsv = {hue, sat, val};
119 RGB rgb = hsv_to_rgb(hsv);
120 setrgb(rgb.r, rgb.g, rgb.b, led1);
0a40654b
YL
121}
122
b624f32f 123void 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); }
670a9b7f 124
e9f74875 125void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
b624f32f 126 (*led1).r = r;
127 (*led1).g = g;
128 (*led1).b = b;
0a40654b
YL
129}
130
cc146e32 131void rgblight_check_config(void) {
b624f32f 132 /* Add some out of bound checks for RGB light config */
133
134 if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
135 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
136 } else if (rgblight_config.mode > RGBLIGHT_MODES) {
137 rgblight_config.mode = RGBLIGHT_MODES;
138 }
139
140 if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
141 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
142 }
cc146e32 143}
0a40654b
YL
144
145uint32_t eeconfig_read_rgblight(void) {
b624f32f 146#if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
ade22f8e 147 return eeprom_read_dword(EECONFIG_RGBLIGHT);
b624f32f 148#else
ade22f8e 149 return 0;
b624f32f 150#endif
0a40654b 151}
cc146e32 152
620ac4b2 153void eeconfig_update_rgblight(uint32_t val) {
b624f32f 154#if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
cc146e32 155 rgblight_check_config();
ade22f8e 156 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
b624f32f 157#endif
0a40654b 158}
cc146e32 159
620ac4b2 160void eeconfig_update_rgblight_default(void) {
b624f32f 161 rgblight_config.enable = 1;
162 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
163 rgblight_config.hue = 0;
164 rgblight_config.sat = UINT8_MAX;
165 rgblight_config.val = RGBLIGHT_LIMIT_VAL;
166 rgblight_config.speed = 0;
167 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
168 eeconfig_update_rgblight(rgblight_config.raw);
0a40654b 169}
cc146e32 170
0a40654b 171void eeconfig_debug_rgblight(void) {
b624f32f 172 dprintf("rgblight_config EEPROM:\n");
173 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
174 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
175 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
176 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
177 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
178 dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
0a40654b
YL
179}
180
181void rgblight_init(void) {
b624f32f 182 /* if already initialized, don't do it again.
183 If you must do it again, extern this and set to false, first.
184 This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
185 if (is_rgblight_initialized) {
186 return;
187 }
188
189 debug_enable = 1; // Debug ON!
190 dprintf("rgblight_init called.\n");
191 dprintf("rgblight_init start!\n");
192 if (!eeconfig_is_enabled()) {
193 dprintf("rgblight_init eeconfig is not enabled.\n");
194 eeconfig_init();
195 eeconfig_update_rgblight_default();
196 }
3a860c4b 197 rgblight_config.raw = eeconfig_read_rgblight();
b624f32f 198 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
199 if (!rgblight_config.mode) {
200 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
201 eeconfig_update_rgblight_default();
202 rgblight_config.raw = eeconfig_read_rgblight();
203 }
204 rgblight_check_config();
cc146e32 205
b624f32f 206 eeconfig_debug_rgblight(); // display current eeprom values
0a40654b 207
6d6d91c8 208#ifdef RGBLIGHT_USE_TIMER
b624f32f 209 rgblight_timer_init(); // setup the timer
6d6d91c8 210#endif
0a40654b 211
b624f32f 212 if (rgblight_config.enable) {
213 rgblight_mode_noeeprom(rgblight_config.mode);
214 }
cc146e32 215
b624f32f 216 is_rgblight_initialized = true;
0a40654b
YL
217}
218
b624f32f 219uint32_t rgblight_read_dword(void) { return rgblight_config.raw; }
9c4424ae 220
2e23689b 221void rgblight_update_dword(uint32_t dword) {
b624f32f 222 RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS;
223 rgblight_config.raw = dword;
224 if (rgblight_config.enable)
225 rgblight_mode_noeeprom(rgblight_config.mode);
226 else {
6d6d91c8 227#ifdef RGBLIGHT_USE_TIMER
b624f32f 228 rgblight_timer_disable();
6d6d91c8 229#endif
b624f32f 230 rgblight_set();
231 }
2e23689b
JH
232}
233
0a40654b 234void rgblight_increase(void) {
b624f32f 235 uint8_t mode = 0;
236 if (rgblight_config.mode < RGBLIGHT_MODES) {
237 mode = rgblight_config.mode + 1;
238 }
239 rgblight_mode(mode);
0a40654b 240}
0a40654b 241void rgblight_decrease(void) {
b624f32f 242 uint8_t mode = 0;
243 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
244 if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
245 mode = rgblight_config.mode - 1;
246 }
247 rgblight_mode(mode);
0a40654b 248}
9e6ee477 249void rgblight_step_helper(bool write_to_eeprom) {
b624f32f 250 uint8_t mode = 0;
251 mode = rgblight_config.mode + 1;
252 if (mode > RGBLIGHT_MODES) {
253 mode = 1;
254 }
255 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
9e6ee477 256}
b624f32f 257void rgblight_step_noeeprom(void) { rgblight_step_helper(false); }
258void rgblight_step(void) { rgblight_step_helper(true); }
9e6ee477 259void rgblight_step_reverse_helper(bool write_to_eeprom) {
b624f32f 260 uint8_t mode = 0;
261 mode = rgblight_config.mode - 1;
262 if (mode < 1) {
263 mode = RGBLIGHT_MODES;
264 }
265 rgblight_mode_eeprom_helper(mode, write_to_eeprom);
5a1b68d5 266}
b624f32f 267void rgblight_step_reverse_noeeprom(void) { rgblight_step_reverse_helper(false); }
268void rgblight_step_reverse(void) { rgblight_step_reverse_helper(true); }
0a40654b 269
b7688590 270uint8_t rgblight_get_mode(void) {
b624f32f 271 if (!rgblight_config.enable) {
272 return false;
273 }
4580d3a7 274
b624f32f 275 return rgblight_config.mode;
4580d3a7 276}
277
751719e6 278void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
b624f32f 279 if (!rgblight_config.enable) {
280 return;
281 }
282 if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
283 rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
284 } else if (mode > RGBLIGHT_MODES) {
285 rgblight_config.mode = RGBLIGHT_MODES;
286 } else {
287 rgblight_config.mode = mode;
288 }
289 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
290 if (write_to_eeprom) {
291 eeconfig_update_rgblight(rgblight_config.raw);
292 dprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
293 } else {
294 dprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
295 }
296 if (is_static_effect(rgblight_config.mode)) {
6d6d91c8 297#ifdef RGBLIGHT_USE_TIMER
b624f32f 298 rgblight_timer_disable();
6d6d91c8 299#endif
b624f32f 300 } else {
6d6d91c8 301#ifdef RGBLIGHT_USE_TIMER
b624f32f 302 rgblight_timer_enable();
6d6d91c8 303#endif
b624f32f 304 }
dfab177f
TI
305#ifdef RGBLIGHT_USE_TIMER
306 animation_status.restart = true;
307#endif
b624f32f 308 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
751719e6
DJ
309}
310
b624f32f 311void rgblight_mode(uint8_t mode) { rgblight_mode_eeprom_helper(mode, true); }
0a40654b 312
b624f32f 313void rgblight_mode_noeeprom(uint8_t mode) { rgblight_mode_eeprom_helper(mode, false); }
751719e6 314
0a40654b 315void rgblight_toggle(void) {
b624f32f 316 dprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
317 if (rgblight_config.enable) {
318 rgblight_disable();
319 } else {
320 rgblight_enable();
321 }
0a40654b
YL
322}
323
751719e6 324void rgblight_toggle_noeeprom(void) {
b624f32f 325 dprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
326 if (rgblight_config.enable) {
327 rgblight_disable_noeeprom();
328 } else {
329 rgblight_enable_noeeprom();
330 }
751719e6
DJ
331}
332
285c5a91 333void rgblight_enable(void) {
b624f32f 334 rgblight_config.enable = 1;
335 // No need to update EEPROM here. rgblight_mode() will do that, actually
336 // eeconfig_update_rgblight(rgblight_config.raw);
337 dprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
338 rgblight_mode(rgblight_config.mode);
285c5a91
EZ
339}
340
751719e6 341void rgblight_enable_noeeprom(void) {
b624f32f 342 rgblight_config.enable = 1;
343 dprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
344 rgblight_mode_noeeprom(rgblight_config.mode);
751719e6
DJ
345}
346
16546ee0 347void rgblight_disable(void) {
b624f32f 348 rgblight_config.enable = 0;
349 eeconfig_update_rgblight(rgblight_config.raw);
350 dprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
6d6d91c8 351#ifdef RGBLIGHT_USE_TIMER
b624f32f 352 rgblight_timer_disable();
6d6d91c8 353#endif
b624f32f 354 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
355 wait_ms(50);
356 rgblight_set();
16546ee0
CG
357}
358
751719e6 359void rgblight_disable_noeeprom(void) {
b624f32f 360 rgblight_config.enable = 0;
361 dprintf("rgblight disable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
6d6d91c8 362#ifdef RGBLIGHT_USE_TIMER
751719e6 363 rgblight_timer_disable();
6d6d91c8 364#endif
b624f32f 365 RGBLIGHT_SPLIT_SET_CHANGE_MODE;
366 wait_ms(50);
367 rgblight_set();
751719e6
DJ
368}
369
9e6ee477 370void rgblight_increase_hue_helper(bool write_to_eeprom) {
b624f32f 371 uint8_t hue = rgblight_config.hue + RGBLIGHT_HUE_STEP;
372 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
9e6ee477 373}
b624f32f 374void rgblight_increase_hue_noeeprom(void) { rgblight_increase_hue_helper(false); }
375void rgblight_increase_hue(void) { rgblight_increase_hue_helper(true); }
9e6ee477 376void rgblight_decrease_hue_helper(bool write_to_eeprom) {
b624f32f 377 uint8_t hue = rgblight_config.hue - RGBLIGHT_HUE_STEP;
378 rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
9e6ee477 379}
b624f32f 380void rgblight_decrease_hue_noeeprom(void) { rgblight_decrease_hue_helper(false); }
381void rgblight_decrease_hue(void) { rgblight_decrease_hue_helper(true); }
9e6ee477 382void rgblight_increase_sat_helper(bool write_to_eeprom) {
b624f32f 383 uint8_t sat = qadd8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
384 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
9e6ee477 385}
b624f32f 386void rgblight_increase_sat_noeeprom(void) { rgblight_increase_sat_helper(false); }
387void rgblight_increase_sat(void) { rgblight_increase_sat_helper(true); }
9e6ee477 388void rgblight_decrease_sat_helper(bool write_to_eeprom) {
b624f32f 389 uint8_t sat = qsub8(rgblight_config.sat, RGBLIGHT_SAT_STEP);
390 rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
9e6ee477 391}
b624f32f 392void rgblight_decrease_sat_noeeprom(void) { rgblight_decrease_sat_helper(false); }
393void rgblight_decrease_sat(void) { rgblight_decrease_sat_helper(true); }
9e6ee477 394void rgblight_increase_val_helper(bool write_to_eeprom) {
b624f32f 395 uint8_t val = qadd8(rgblight_config.val, RGBLIGHT_VAL_STEP);
396 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
9e6ee477 397}
b624f32f 398void rgblight_increase_val_noeeprom(void) { rgblight_increase_val_helper(false); }
399void rgblight_increase_val(void) { rgblight_increase_val_helper(true); }
9e6ee477 400void rgblight_decrease_val_helper(bool write_to_eeprom) {
b624f32f 401 uint8_t val = qsub8(rgblight_config.val, RGBLIGHT_VAL_STEP);
402 rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
0a40654b 403}
b624f32f 404void rgblight_decrease_val_noeeprom(void) { rgblight_decrease_val_helper(false); }
405void rgblight_decrease_val(void) { rgblight_decrease_val_helper(true); }
afacd423 406void rgblight_increase_speed(void) {
b624f32f 407 if (rgblight_config.speed < 3) rgblight_config.speed++;
408 // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?
409 eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
afacd423 410}
411
412void rgblight_decrease_speed(void) {
b624f32f 413 if (rgblight_config.speed > 0) rgblight_config.speed--;
414 // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED??
415 eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this
afacd423 416}
0a40654b 417
22ba36a4 418void rgblight_sethsv_noeeprom_old(uint8_t hue, uint8_t sat, uint8_t val) {
b624f32f 419 if (rgblight_config.enable) {
420 LED_TYPE tmp_led;
421 sethsv(hue, sat, val, &tmp_led);
422 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
423 }
0a40654b 424}
751719e6 425
22ba36a4 426void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
b624f32f 427 if (rgblight_config.enable) {
428 rgblight_status.base_mode = mode_base_table[rgblight_config.mode];
429 if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
430 // same static color
431 LED_TYPE tmp_led;
432 sethsv(hue, sat, val, &tmp_led);
433 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
434 } else {
435 // all LEDs in same color
436 if (1 == 0) { // dummy
437 }
6d6d91c8 438#ifdef RGBLIGHT_EFFECT_BREATHING
b624f32f 439 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
440 // breathing mode, ignore the change of val, use in memory value instead
441 val = rgblight_config.val;
442 }
6d6d91c8
TI
443#endif
444#ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
b624f32f 445 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
446 // rainbow mood, ignore the change of hue
447 hue = rgblight_config.hue;
448 }
6d6d91c8
TI
449#endif
450#ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
b624f32f 451 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
452 // rainbow swirl, ignore the change of hue
453 hue = rgblight_config.hue;
454 }
6d6d91c8
TI
455#endif
456#ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
b624f32f 457 else if (rgblight_status.base_mode == RGBLIGHT_MODE_STATIC_GRADIENT) {
458 // static gradient
459 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
460 bool direction = (delta % 2) == 0;
461# ifdef __AVR__
462 // probably due to how pgm_read_word is defined for ARM, but the ARM compiler really hates this line
463 uint8_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[delta / 2]);
464# else
465 uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2];
466# endif
467 for (uint8_t i = 0; i < effect_num_leds; i++) {
468 uint8_t _hue = ((uint16_t)i * (uint16_t)range) / effect_num_leds;
469 if (direction) {
470 _hue = hue + _hue;
471 } else {
472 _hue = hue - _hue;
473 }
474 dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range);
475 sethsv(_hue, sat, val, (LED_TYPE *)&led[i + effect_start_pos]);
476 }
477 rgblight_set();
478 }
22ba36a4 479#endif
a7882b1f 480 }
dfab177f 481#ifdef RGBLIGHT_SPLIT
b624f32f 482 if (rgblight_config.hue != hue || rgblight_config.sat != sat || rgblight_config.val != val) {
483 RGBLIGHT_SPLIT_SET_CHANGE_HSVS;
484 }
dfab177f 485#endif
b624f32f 486 rgblight_config.hue = hue;
487 rgblight_config.sat = sat;
488 rgblight_config.val = val;
489 if (write_to_eeprom) {
490 eeconfig_update_rgblight(rgblight_config.raw);
491 dprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
492 } else {
493 dprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
494 }
751719e6 495 }
3a860c4b
JO
496}
497
b624f32f 498void rgblight_sethsv(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, true); }
751719e6 499
b624f32f 500void rgblight_sethsv_noeeprom(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_eeprom_helper(hue, sat, val, false); }
751719e6 501
b624f32f 502uint8_t rgblight_get_hue(void) { return rgblight_config.hue; }
12e66330 503
b624f32f 504uint8_t rgblight_get_sat(void) { return rgblight_config.sat; }
12e66330 505
b624f32f 506uint8_t rgblight_get_val(void) { return rgblight_config.val; }
12e66330 507
3a860c4b 508void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
b624f32f 509 if (!rgblight_config.enable) {
510 return;
511 }
512
513 for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
514 led[i].r = r;
515 led[i].g = g;
516 led[i].b = b;
517 }
518 rgblight_set();
0a40654b
YL
519}
520
16546ee0 521void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
b624f32f 522 if (!rgblight_config.enable || index >= RGBLED_NUM) {
523 return;
524 }
16546ee0 525
b624f32f 526 led[index].r = r;
527 led[index].g = g;
528 led[index].b = b;
529 rgblight_set();
16546ee0
CG
530}
531
22ba36a4 532void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index) {
b624f32f 533 if (!rgblight_config.enable) {
534 return;
535 }
16546ee0 536
b624f32f 537 LED_TYPE tmp_led;
538 sethsv(hue, sat, val, &tmp_led);
539 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
16546ee0
CG
540}
541
b624f32f 542#if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT)
c1c5922a 543
b624f32f 544static uint8_t get_interval_time(const uint8_t *default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) {
545 return
546# ifdef VELOCIKEY_ENABLE
547 velocikey_enabled() ? velocikey_match_speed(velocikey_min, velocikey_max) :
548# endif
549 pgm_read_byte(default_interval_address);
c1c5922a
CL
550}
551
552#endif
553
642f6cf1 554void rgblight_setrgb_range(uint8_t r, uint8_t g, uint8_t b, uint8_t start, uint8_t end) {
b624f32f 555 if (!rgblight_config.enable || start < 0 || start >= end || end > RGBLED_NUM) {
556 return;
557 }
558
559 for (uint8_t i = start; i < end; i++) {
560 led[i].r = r;
561 led[i].g = g;
562 led[i].b = b;
563 }
564 rgblight_set();
565 wait_ms(1);
642f6cf1
E
566}
567
22ba36a4 568void rgblight_sethsv_range(uint8_t hue, uint8_t sat, uint8_t val, uint8_t start, uint8_t end) {
b624f32f 569 if (!rgblight_config.enable) {
570 return;
571 }
642f6cf1 572
b624f32f 573 LED_TYPE tmp_led;
574 sethsv(hue, sat, val, &tmp_led);
575 rgblight_setrgb_range(tmp_led.r, tmp_led.g, tmp_led.b, start, end);
642f6cf1
E
576}
577
670a9b7f 578#ifndef RGBLIGHT_SPLIT
b624f32f 579void rgblight_setrgb_master(uint8_t r, uint8_t g, uint8_t b) { rgblight_setrgb_range(r, g, b, 0, (uint8_t)RGBLED_NUM / 2); }
642f6cf1 580
b624f32f 581void 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); }
642f6cf1 582
b624f32f 583void rgblight_sethsv_master(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_range(hue, sat, val, 0, (uint8_t)RGBLED_NUM / 2); }
642f6cf1 584
b624f32f 585void 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); }
586#endif // ifndef RGBLIGHT_SPLIT
642f6cf1 587
d4cd5dda 588#ifndef RGBLIGHT_CUSTOM_DRIVER
0a40654b 589void rgblight_set(void) {
b624f32f 590 LED_TYPE *start_led;
591 uint16_t num_leds = clipping_num_leds;
592
593 if (!rgblight_config.enable) {
594 for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) {
595 led[i].r = 0;
596 led[i].g = 0;
597 led[i].b = 0;
598 }
3a860c4b 599 }
b624f32f 600# ifdef RGBLIGHT_LED_MAP
601 LED_TYPE led0[RGBLED_NUM];
602 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
603 led0[i] = led[pgm_read_byte(&led_map[i])];
604 }
605 start_led = led0 + clipping_start_pos;
606# else
607 start_led = led + clipping_start_pos;
608# endif
609# ifdef RGBW
610 ws2812_setleds_rgbw(start_led, num_leds);
611# else
612 ws2812_setleds(start_led, num_leds);
613# endif
0a40654b 614}
d4cd5dda 615#endif
0a40654b 616
dfab177f
TI
617#ifdef RGBLIGHT_SPLIT
618/* for split keyboard master side */
b624f32f 619uint8_t rgblight_get_change_flags(void) { return rgblight_status.change_flags; }
dfab177f 620
b624f32f 621void rgblight_clear_change_flags(void) { rgblight_status.change_flags = 0; }
dfab177f
TI
622
623void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) {
624 syncinfo->config = rgblight_config;
625 syncinfo->status = rgblight_status;
626}
627
628/* for split keyboard slave side */
629void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) {
630 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) {
631 if (syncinfo->config.enable) {
b624f32f 632 rgblight_config.enable = 1; // == rgblight_enable_noeeprom();
dfab177f
TI
633 rgblight_mode_eeprom_helper(syncinfo->config.mode, write_to_eeprom);
634 } else {
635 rgblight_disable_noeeprom();
636 }
637 }
638 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_HSVS) {
639 rgblight_sethsv_eeprom_helper(syncinfo->config.hue, syncinfo->config.sat, syncinfo->config.val, write_to_eeprom);
640 // rgblight_config.speed = config->speed; // NEED???
641 }
b624f32f 642# ifdef RGBLIGHT_USE_TIMER
dfab177f
TI
643 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_TIMER) {
644 if (syncinfo->status.timer_enabled) {
645 rgblight_timer_enable();
646 } else {
647 rgblight_timer_disable();
648 }
649 }
b624f32f 650# ifndef RGBLIGHT_SPLIT_NO_ANIMATION_SYNC
dfab177f
TI
651 if (syncinfo->status.change_flags & RGBLIGHT_STATUS_ANIMATION_TICK) {
652 animation_status.restart = true;
653 }
b624f32f 654# endif /* RGBLIGHT_SPLIT_NO_ANIMATION_SYNC */
655# endif /* RGBLIGHT_USE_TIMER */
dfab177f
TI
656}
657#endif /* RGBLIGHT_SPLIT */
658
6d6d91c8 659#ifdef RGBLIGHT_USE_TIMER
57e08eb8 660
dfab177f
TI
661typedef void (*effect_func_t)(animation_status_t *anim);
662
663// Animation timer -- use system timer (AVR Timer0)
0a40654b 664void rgblight_timer_init(void) {
b624f32f 665 // OLD!!!! Animation timer -- AVR Timer3
666 // static uint8_t rgblight_timer_is_init = 0;
667 // if (rgblight_timer_is_init) {
668 // return;
669 // }
670 // rgblight_timer_is_init = 1;
671 // /* Timer 3 setup */
672 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
673 // | _BV(CS30); // Clock selelct: clk/1
674 // /* Set TOP value */
675 // uint8_t sreg = SREG;
676 // cli();
677 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
678 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
679 // SREG = sreg;
680
681 rgblight_status.timer_enabled = false;
682 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
0a40654b
YL
683}
684void rgblight_timer_enable(void) {
b624f32f 685 if (!is_static_effect(rgblight_config.mode)) {
686 rgblight_status.timer_enabled = true;
687 }
688 animation_status.last_timer = timer_read();
689 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
690 dprintf("rgblight timer enabled.\n");
0a40654b
YL
691}
692void rgblight_timer_disable(void) {
b624f32f 693 rgblight_status.timer_enabled = false;
694 RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE;
695 dprintf("rgblight timer disable.\n");
0a40654b
YL
696}
697void rgblight_timer_toggle(void) {
b624f32f 698 dprintf("rgblight timer toggle.\n");
699 if (rgblight_status.timer_enabled) {
700 rgblight_timer_disable();
701 } else {
702 rgblight_timer_enable();
703 }
0a40654b
YL
704}
705
4094544d 706void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
b624f32f 707 rgblight_enable();
708 rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
709 rgblight_setrgb(r, g, b);
4094544d
EZ
710}
711
dfab177f
TI
712static void rgblight_effect_dummy(animation_status_t *anim) {
713 // do nothing
714 /********
715 dprintf("rgblight_task() what happened?\n");
716 dprintf("is_static_effect %d\n", is_static_effect(rgblight_config.mode));
717 dprintf("mode = %d, base_mode = %d, timer_enabled %d, ",
718 rgblight_config.mode, rgblight_status.base_mode,
719 rgblight_status.timer_enabled);
720 dprintf("last_timer = %d\n",anim->last_timer);
721 **/
722}
723
e9f74875 724void rgblight_task(void) {
b624f32f 725 if (rgblight_status.timer_enabled) {
726 effect_func_t effect_func = rgblight_effect_dummy;
727 uint16_t interval_time = 2000; // dummy interval
728 uint8_t delta = rgblight_config.mode - rgblight_status.base_mode;
729 animation_status.delta = delta;
730
731 // static light mode, do nothing here
732 if (1 == 0) { // dummy
733 }
734# ifdef RGBLIGHT_EFFECT_BREATHING
735 else if (rgblight_status.base_mode == RGBLIGHT_MODE_BREATHING) {
736 // breathing mode
737 interval_time = get_interval_time(&RGBLED_BREATHING_INTERVALS[delta], 1, 100);
738 effect_func = rgblight_effect_breathing;
739 }
740# endif
741# ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
742 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_MOOD) {
743 // rainbow mood mode
744 interval_time = get_interval_time(&RGBLED_RAINBOW_MOOD_INTERVALS[delta], 5, 100);
745 effect_func = rgblight_effect_rainbow_mood;
746 }
747# endif
748# ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
749 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RAINBOW_SWIRL) {
750 // rainbow swirl mode
751 interval_time = get_interval_time(&RGBLED_RAINBOW_SWIRL_INTERVALS[delta / 2], 1, 100);
752 effect_func = rgblight_effect_rainbow_swirl;
753 }
754# endif
755# ifdef RGBLIGHT_EFFECT_SNAKE
756 else if (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE) {
757 // snake mode
758 interval_time = get_interval_time(&RGBLED_SNAKE_INTERVALS[delta / 2], 1, 200);
759 effect_func = rgblight_effect_snake;
760 }
761# endif
762# ifdef RGBLIGHT_EFFECT_KNIGHT
763 else if (rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT) {
764 // knight mode
765 interval_time = get_interval_time(&RGBLED_KNIGHT_INTERVALS[delta], 5, 100);
766 effect_func = rgblight_effect_knight;
767 }
768# endif
769# ifdef RGBLIGHT_EFFECT_CHRISTMAS
770 else if (rgblight_status.base_mode == RGBLIGHT_MODE_CHRISTMAS) {
771 // christmas mode
772 interval_time = RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL;
773 effect_func = (effect_func_t)rgblight_effect_christmas;
774 }
775# endif
776# ifdef RGBLIGHT_EFFECT_RGB_TEST
777 else if (rgblight_status.base_mode == RGBLIGHT_MODE_RGB_TEST) {
778 // RGB test mode
779 interval_time = pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0]);
780 effect_func = (effect_func_t)rgblight_effect_rgbtest;
781 }
782# endif
783# ifdef RGBLIGHT_EFFECT_ALTERNATING
784 else if (rgblight_status.base_mode == RGBLIGHT_MODE_ALTERNATING) {
785 interval_time = 500;
786 effect_func = (effect_func_t)rgblight_effect_alternating;
787 }
788# endif
789 if (animation_status.restart) {
790 animation_status.restart = false;
791 animation_status.last_timer = timer_read() - interval_time - 1;
792 animation_status.pos16 = 0; // restart signal to local each effect
793 }
794 if (timer_elapsed(animation_status.last_timer) >= interval_time) {
795# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
796 static uint16_t report_last_timer = 0;
797 static bool tick_flag = false;
798 uint16_t oldpos16;
799 if (tick_flag) {
800 tick_flag = false;
801 if (timer_elapsed(report_last_timer) >= 30000) {
802 report_last_timer = timer_read();
803 dprintf("rgblight animation tick report to slave\n");
804 RGBLIGHT_SPLIT_ANIMATION_TICK;
805 }
806 }
807 oldpos16 = animation_status.pos16;
808# endif
809 animation_status.last_timer += interval_time;
810 effect_func(&animation_status);
811# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
812 if (animation_status.pos16 == 0 && oldpos16 != 0) {
813 tick_flag = true;
814 }
815# endif
dfab177f 816 }
dfab177f 817 }
3a860c4b
JO
818}
819
6d6d91c8
TI
820#endif /* RGBLIGHT_USE_TIMER */
821
3a860c4b 822// Effects
6d6d91c8 823#ifdef RGBLIGHT_EFFECT_BREATHING
3da8d46a 824
b624f32f 825# ifndef RGBLIGHT_EFFECT_BREATHE_CENTER
826# ifndef RGBLIGHT_BREATHE_TABLE_SIZE
827# define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256 or 128 or 64
828# endif
829# include <rgblight_breathe_table.h>
830# endif
3da8d46a 831
b624f32f 832__attribute__((weak)) const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
6d6d91c8 833
dfab177f 834void rgblight_effect_breathing(animation_status_t *anim) {
b624f32f 835 float val;
0a40654b 836
b624f32f 837 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
838# ifdef RGBLIGHT_EFFECT_BREATHE_TABLE
839 val = pgm_read_byte(&rgblight_effect_breathe_table[anim->pos / table_scale]);
840# else
841 val = (exp(sin((anim->pos / 255.0) * M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER / M_E) * (RGBLIGHT_EFFECT_BREATHE_MAX / (M_E - 1 / M_E));
842# endif
843 rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
844 anim->pos = (anim->pos + 1);
0a40654b 845}
6d6d91c8
TI
846#endif
847
848#ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
b624f32f 849__attribute__((weak)) const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
6d6d91c8 850
dfab177f 851void rgblight_effect_rainbow_mood(animation_status_t *anim) {
b624f32f 852 rgblight_sethsv_noeeprom_old(anim->current_hue, rgblight_config.sat, rgblight_config.val);
853 anim->current_hue++;
0a40654b 854}
6d6d91c8
TI
855#endif
856
857#ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
b624f32f 858# ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
859# define RGBLIGHT_RAINBOW_SWIRL_RANGE 255
860# endif
94f58322 861
b624f32f 862__attribute__((weak)) const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
6d6d91c8 863
dfab177f 864void rgblight_effect_rainbow_swirl(animation_status_t *anim) {
b624f32f 865 uint8_t hue;
866 uint8_t i;
c1c5922a 867
b624f32f 868 for (i = 0; i < effect_num_leds; i++) {
869 hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue);
870 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
871 }
872 rgblight_set();
3a860c4b 873
b624f32f 874 if (anim->delta % 2) {
875 anim->current_hue++;
876 } else {
877 anim->current_hue--;
878 }
0a40654b 879}
6d6d91c8
TI
880#endif
881
882#ifdef RGBLIGHT_EFFECT_SNAKE
b624f32f 883__attribute__((weak)) const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
6d6d91c8 884
dfab177f 885void rgblight_effect_snake(animation_status_t *anim) {
b624f32f 886 static uint8_t pos = 0;
887 uint8_t i, j;
888 int8_t k;
889 int8_t increment = 1;
c1c5922a 890
b624f32f 891 if (anim->delta % 2) {
892 increment = -1;
dfab177f 893 }
dfab177f 894
b624f32f 895# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
896 if (anim->pos == 0) { // restart signal
897 if (increment == 1) {
898 pos = effect_num_leds - 1;
899 } else {
900 pos = 0;
901 }
902 anim->pos = 1;
3a860c4b 903 }
b624f32f 904# endif
905
906 for (i = 0; i < effect_num_leds; i++) {
907 LED_TYPE *ledp = led + i + effect_start_pos;
908 ledp->r = 0;
909 ledp->g = 0;
910 ledp->b = 0;
911 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
912 k = pos + j * increment;
913 if (k < 0) {
914 k = k + effect_num_leds;
915 }
916 if (i == k) {
917 sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val * (RGBLIGHT_EFFECT_SNAKE_LENGTH - j) / RGBLIGHT_EFFECT_SNAKE_LENGTH), ledp);
918 }
919 }
920 }
921 rgblight_set();
922 if (increment == 1) {
923 if (pos - 1 < 0) {
924 pos = effect_num_leds - 1;
925# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
926 anim->pos = 0;
927# endif
928 } else {
929 pos -= 1;
930# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
931 anim->pos = 1;
932# endif
933 }
3a860c4b 934 } else {
b624f32f 935 pos = (pos + 1) % effect_num_leds;
936# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
937 anim->pos = pos;
938# endif
3a860c4b 939 }
0a40654b 940}
6d6d91c8
TI
941#endif
942
943#ifdef RGBLIGHT_EFFECT_KNIGHT
b624f32f 944__attribute__((weak)) const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
6d6d91c8 945
dfab177f 946void rgblight_effect_knight(animation_status_t *anim) {
b624f32f 947 static int8_t low_bound = 0;
948 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
949 static int8_t increment = 1;
950 uint8_t i, cur;
951
952# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
953 if (anim->pos == 0) { // restart signal
954 anim->pos = 1;
955 low_bound = 0;
956 high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
957 increment = 1;
3a860c4b 958 }
b624f32f 959# endif
960 // Set all the LEDs to 0
961 for (i = effect_start_pos; i < effect_end_pos; i++) {
962 led[i].r = 0;
963 led[i].g = 0;
964 led[i].b = 0;
965 }
966 // Determine which LEDs should be lit up
967 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
968 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos;
4edfa97e 969
b624f32f 970 if (i >= low_bound && i <= high_bound) {
971 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
972 } else {
973 led[cur].r = 0;
974 led[cur].g = 0;
975 led[cur].b = 0;
976 }
977 }
978 rgblight_set();
979
980 // Move from low_bound to high_bound changing the direction we increment each
981 // time a boundary is hit.
982 low_bound += increment;
983 high_bound += increment;
984
985 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
986 increment = -increment;
987# if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC)
988 if (increment == 1) {
989 anim->pos = 0;
990 }
991# endif
dfab177f 992 }
3a860c4b 993}
6d6d91c8 994#endif
3a860c4b 995
6d6d91c8 996#ifdef RGBLIGHT_EFFECT_CHRISTMAS
dfab177f 997void rgblight_effect_christmas(animation_status_t *anim) {
b624f32f 998 uint8_t hue;
999 uint8_t i;
1000
1001 anim->current_offset = (anim->current_offset + 1) % 2;
1002 for (i = 0; i < effect_num_leds; i++) {
1003 hue = 0 + ((i / RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85;
1004 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]);
1005 }
1006 rgblight_set();
cae269b0 1007}
6d6d91c8
TI
1008#endif
1009
1010#ifdef RGBLIGHT_EFFECT_RGB_TEST
b624f32f 1011__attribute__((weak)) const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
cae269b0 1012
dfab177f 1013void rgblight_effect_rgbtest(animation_status_t *anim) {
b624f32f 1014 static uint8_t maxval = 0;
1015 uint8_t g;
1016 uint8_t r;
1017 uint8_t b;
1018
1019 if (maxval == 0) {
1020 LED_TYPE tmp_led;
1021 sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
1022 maxval = tmp_led.r;
1023 }
1024 g = r = b = 0;
1025 switch (anim->pos) {
1026 case 0:
1027 r = maxval;
1028 break;
1029 case 1:
1030 g = maxval;
1031 break;
1032 case 2:
1033 b = maxval;
1034 break;
1035 }
1036 rgblight_setrgb(r, g, b);
1037 anim->pos = (anim->pos + 1) % 3;
52297346 1038}
6d6d91c8 1039#endif
52297346 1040
6d6d91c8 1041#ifdef RGBLIGHT_EFFECT_ALTERNATING
dfab177f 1042void rgblight_effect_alternating(animation_status_t *anim) {
b624f32f 1043 for (int i = 0; i < effect_num_leds; i++) {
1044 LED_TYPE *ledp = led + i + effect_start_pos;
1045 if (i < effect_num_leds / 2 && anim->pos) {
1046 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
1047 } else if (i >= effect_num_leds / 2 && !anim->pos) {
1048 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp);
1049 } else {
1050 sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp);
1051 }
1052 }
1053 rgblight_set();
1054 anim->pos = (anim->pos + 1) % 2;
595f3cbe 1055}
6d6d91c8 1056#endif