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