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