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