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