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> |
0a40654b YL |
17 | #include <avr/eeprom.h> |
18 | #include <avr/interrupt.h> | |
19 | #include <util/delay.h> | |
20 | #include "progmem.h" | |
21 | #include "timer.h" | |
22 | #include "rgblight.h" | |
23 | #include "debug.h" | |
f113f954 | 24 | #include "led_tables.h" |
0a40654b | 25 | |
9b0e21f8 | 26 | __attribute__ ((weak)) |
0a40654b | 27 | const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5}; |
9b0e21f8 | 28 | __attribute__ ((weak)) |
0a40654b | 29 | const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30}; |
9b0e21f8 | 30 | __attribute__ ((weak)) |
0a40654b | 31 | const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20}; |
9b0e21f8 | 32 | __attribute__ ((weak)) |
0a40654b | 33 | const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20}; |
9b0e21f8 | 34 | __attribute__ ((weak)) |
4580d3a7 | 35 | const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31}; |
a7882b1f | 36 | __attribute__ ((weak)) |
37 | const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90}; | |
0a40654b YL |
38 | |
39 | rgblight_config_t rgblight_config; | |
40 | rgblight_config_t inmem_config; | |
0a40654b | 41 | |
e9f74875 JH |
42 | LED_TYPE led[RGBLED_NUM]; |
43 | uint8_t rgblight_inited = 0; | |
44 | bool rgblight_timer_enabled = false; | |
0a40654b | 45 | |
e9f74875 | 46 | void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { |
ea2d2f5d | 47 | uint8_t r = 0, g = 0, b = 0, base, color; |
0a40654b | 48 | |
3a860c4b JO |
49 | if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. |
50 | r = val; | |
51 | g = val; | |
52 | b = val; | |
53 | } else { | |
54 | base = ((255 - sat) * val) >> 8; | |
ea2d2f5d | 55 | color = (val - base) * (hue % 60) / 60; |
3a860c4b JO |
56 | |
57 | switch (hue / 60) { | |
58 | case 0: | |
59 | r = val; | |
ea2d2f5d | 60 | g = base + color; |
3a860c4b JO |
61 | b = base; |
62 | break; | |
63 | case 1: | |
ea2d2f5d | 64 | r = val - color; |
3a860c4b JO |
65 | g = val; |
66 | b = base; | |
67 | break; | |
68 | case 2: | |
69 | r = base; | |
70 | g = val; | |
ea2d2f5d | 71 | b = base + color; |
3a860c4b JO |
72 | break; |
73 | case 3: | |
74 | r = base; | |
ea2d2f5d | 75 | g = val - color; |
3a860c4b JO |
76 | b = val; |
77 | break; | |
78 | case 4: | |
ea2d2f5d | 79 | r = base + color; |
3a860c4b JO |
80 | g = base; |
81 | b = val; | |
82 | break; | |
83 | case 5: | |
84 | r = val; | |
85 | g = base; | |
ea2d2f5d | 86 | b = val - color; |
3a860c4b JO |
87 | break; |
88 | } | |
89 | } | |
f113f954 FS |
90 | r = pgm_read_byte(&CIE1931_CURVE[r]); |
91 | g = pgm_read_byte(&CIE1931_CURVE[g]); | |
92 | b = pgm_read_byte(&CIE1931_CURVE[b]); | |
ea2d2f5d JO |
93 | |
94 | setrgb(r, g, b, led1); | |
0a40654b YL |
95 | } |
96 | ||
e9f74875 | 97 | void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) { |
0a40654b YL |
98 | (*led1).r = r; |
99 | (*led1).g = g; | |
100 | (*led1).b = b; | |
101 | } | |
102 | ||
103 | ||
104 | uint32_t eeconfig_read_rgblight(void) { | |
105 | return eeprom_read_dword(EECONFIG_RGBLIGHT); | |
106 | } | |
620ac4b2 ET |
107 | void eeconfig_update_rgblight(uint32_t val) { |
108 | eeprom_update_dword(EECONFIG_RGBLIGHT, val); | |
0a40654b | 109 | } |
620ac4b2 | 110 | void eeconfig_update_rgblight_default(void) { |
3a860c4b JO |
111 | dprintf("eeconfig_update_rgblight_default\n"); |
112 | rgblight_config.enable = 1; | |
113 | rgblight_config.mode = 1; | |
e9f74875 JH |
114 | rgblight_config.hue = 0; |
115 | rgblight_config.sat = 255; | |
116 | rgblight_config.val = 255; | |
3a860c4b | 117 | eeconfig_update_rgblight(rgblight_config.raw); |
0a40654b YL |
118 | } |
119 | void eeconfig_debug_rgblight(void) { | |
3a860c4b JO |
120 | dprintf("rgblight_config eprom\n"); |
121 | dprintf("rgblight_config.enable = %d\n", rgblight_config.enable); | |
122 | dprintf("rghlight_config.mode = %d\n", rgblight_config.mode); | |
123 | dprintf("rgblight_config.hue = %d\n", rgblight_config.hue); | |
124 | dprintf("rgblight_config.sat = %d\n", rgblight_config.sat); | |
125 | dprintf("rgblight_config.val = %d\n", rgblight_config.val); | |
0a40654b YL |
126 | } |
127 | ||
128 | void rgblight_init(void) { | |
129 | debug_enable = 1; // Debug ON! | |
3a860c4b | 130 | dprintf("rgblight_init called.\n"); |
0a40654b | 131 | rgblight_inited = 1; |
3a860c4b | 132 | dprintf("rgblight_init start!\n"); |
0a40654b | 133 | if (!eeconfig_is_enabled()) { |
3a860c4b | 134 | dprintf("rgblight_init eeconfig is not enabled.\n"); |
0a40654b | 135 | eeconfig_init(); |
3a860c4b | 136 | eeconfig_update_rgblight_default(); |
0a40654b YL |
137 | } |
138 | rgblight_config.raw = eeconfig_read_rgblight(); | |
3a860c4b JO |
139 | if (!rgblight_config.mode) { |
140 | dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n"); | |
141 | eeconfig_update_rgblight_default(); | |
142 | rgblight_config.raw = eeconfig_read_rgblight(); | |
143 | } | |
144 | eeconfig_debug_rgblight(); // display current eeprom values | |
0a40654b | 145 | |
3774a7fc | 146 | #ifdef RGBLIGHT_ANIMATIONS |
3a860c4b JO |
147 | rgblight_timer_init(); // setup the timer |
148 | #endif | |
0a40654b YL |
149 | |
150 | if (rgblight_config.enable) { | |
151 | rgblight_mode(rgblight_config.mode); | |
152 | } | |
153 | } | |
154 | ||
2e23689b JH |
155 | void rgblight_update_dword(uint32_t dword) { |
156 | rgblight_config.raw = dword; | |
157 | eeconfig_update_rgblight(rgblight_config.raw); | |
158 | if (rgblight_config.enable) | |
159 | rgblight_mode(rgblight_config.mode); | |
160 | else { | |
161 | #ifdef RGBLIGHT_ANIMATIONS | |
162 | rgblight_timer_disable(); | |
163 | #endif | |
164 | rgblight_set(); | |
165 | } | |
166 | } | |
167 | ||
0a40654b | 168 | void rgblight_increase(void) { |
3a860c4b | 169 | uint8_t mode = 0; |
0a40654b YL |
170 | if (rgblight_config.mode < RGBLIGHT_MODES) { |
171 | mode = rgblight_config.mode + 1; | |
172 | } | |
3a860c4b | 173 | rgblight_mode(mode); |
0a40654b | 174 | } |
0a40654b | 175 | void rgblight_decrease(void) { |
3a860c4b JO |
176 | uint8_t mode = 0; |
177 | // Mode will never be < 1. If it ever is, eeprom needs to be initialized. | |
178 | if (rgblight_config.mode > 1) { | |
179 | mode = rgblight_config.mode - 1; | |
0a40654b | 180 | } |
3a860c4b | 181 | rgblight_mode(mode); |
0a40654b | 182 | } |
0a40654b | 183 | void rgblight_step(void) { |
3a860c4b | 184 | uint8_t mode = 0; |
0a40654b YL |
185 | mode = rgblight_config.mode + 1; |
186 | if (mode > RGBLIGHT_MODES) { | |
187 | mode = 1; | |
188 | } | |
3a860c4b | 189 | rgblight_mode(mode); |
0a40654b | 190 | } |
5a1b68d5 | 191 | void rgblight_step_reverse(void) { |
192 | uint8_t mode = 0; | |
193 | mode = rgblight_config.mode - 1; | |
194 | if (mode < 1) { | |
195 | mode = RGBLIGHT_MODES; | |
196 | } | |
197 | rgblight_mode(mode); | |
198 | } | |
0a40654b | 199 | |
4580d3a7 | 200 | uint32_t rgblight_get_mode(void) { |
201 | if (!rgblight_config.enable) { | |
202 | return false; | |
203 | } | |
204 | ||
205 | return rgblight_config.mode; | |
206 | } | |
207 | ||
0a40654b | 208 | void rgblight_mode(uint8_t mode) { |
3a860c4b JO |
209 | if (!rgblight_config.enable) { |
210 | return; | |
211 | } | |
212 | if (mode < 1) { | |
213 | rgblight_config.mode = 1; | |
214 | } else if (mode > RGBLIGHT_MODES) { | |
215 | rgblight_config.mode = RGBLIGHT_MODES; | |
216 | } else { | |
217 | rgblight_config.mode = mode; | |
218 | } | |
620ac4b2 | 219 | eeconfig_update_rgblight(rgblight_config.raw); |
97f8f378 | 220 | xprintf("rgblight mode: %u\n", rgblight_config.mode); |
3a860c4b | 221 | if (rgblight_config.mode == 1) { |
3774a7fc | 222 | #ifdef RGBLIGHT_ANIMATIONS |
3a860c4b JO |
223 | rgblight_timer_disable(); |
224 | #endif | |
0e548f8b | 225 | } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) { |
3a860c4b JO |
226 | // MODE 2-5, breathing |
227 | // MODE 6-8, rainbow mood | |
228 | // MODE 9-14, rainbow swirl | |
229 | // MODE 15-20, snake | |
230 | // MODE 21-23, knight | |
4580d3a7 | 231 | // MODE 24, xmas |
232 | // MODE 25-34, static rainbow | |
3a860c4b | 233 | |
3774a7fc | 234 | #ifdef RGBLIGHT_ANIMATIONS |
3a860c4b JO |
235 | rgblight_timer_enable(); |
236 | #endif | |
a7882b1f | 237 | } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) { |
238 | // MODE 25-34, static gradient | |
239 | ||
240 | #ifdef RGBLIGHT_ANIMATIONS | |
241 | rgblight_timer_disable(); | |
242 | #endif | |
3a860c4b | 243 | } |
0a40654b YL |
244 | rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); |
245 | } | |
246 | ||
247 | void rgblight_toggle(void) { | |
248 | rgblight_config.enable ^= 1; | |
620ac4b2 | 249 | eeconfig_update_rgblight(rgblight_config.raw); |
97f8f378 | 250 | xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable); |
3a860c4b JO |
251 | if (rgblight_config.enable) { |
252 | rgblight_mode(rgblight_config.mode); | |
253 | } else { | |
3774a7fc | 254 | #ifdef RGBLIGHT_ANIMATIONS |
3a860c4b JO |
255 | rgblight_timer_disable(); |
256 | #endif | |
257 | _delay_ms(50); | |
258 | rgblight_set(); | |
259 | } | |
0a40654b YL |
260 | } |
261 | ||
285c5a91 EZ |
262 | void rgblight_enable(void) { |
263 | rgblight_config.enable = 1; | |
264 | eeconfig_update_rgblight(rgblight_config.raw); | |
265 | xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable); | |
266 | rgblight_mode(rgblight_config.mode); | |
267 | } | |
268 | ||
0a40654b | 269 | |
3a860c4b JO |
270 | void rgblight_increase_hue(void) { |
271 | uint16_t hue; | |
0a40654b YL |
272 | hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360; |
273 | rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val); | |
274 | } | |
3a860c4b JO |
275 | void rgblight_decrease_hue(void) { |
276 | uint16_t hue; | |
277 | if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) { | |
278 | hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360; | |
279 | } else { | |
280 | hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360; | |
281 | } | |
0a40654b YL |
282 | rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val); |
283 | } | |
284 | void rgblight_increase_sat(void) { | |
3a860c4b | 285 | uint8_t sat; |
0a40654b YL |
286 | if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) { |
287 | sat = 255; | |
288 | } else { | |
3a860c4b | 289 | sat = rgblight_config.sat + RGBLIGHT_SAT_STEP; |
0a40654b YL |
290 | } |
291 | rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val); | |
292 | } | |
3a860c4b JO |
293 | void rgblight_decrease_sat(void) { |
294 | uint8_t sat; | |
0a40654b YL |
295 | if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) { |
296 | sat = 0; | |
297 | } else { | |
3a860c4b | 298 | sat = rgblight_config.sat - RGBLIGHT_SAT_STEP; |
0a40654b YL |
299 | } |
300 | rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val); | |
301 | } | |
3a860c4b JO |
302 | void rgblight_increase_val(void) { |
303 | uint8_t val; | |
0a40654b YL |
304 | if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) { |
305 | val = 255; | |
306 | } else { | |
3a860c4b | 307 | val = rgblight_config.val + RGBLIGHT_VAL_STEP; |
0a40654b YL |
308 | } |
309 | rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val); | |
310 | } | |
311 | void rgblight_decrease_val(void) { | |
3a860c4b | 312 | uint8_t val; |
0a40654b YL |
313 | if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) { |
314 | val = 0; | |
315 | } else { | |
3a860c4b | 316 | val = rgblight_config.val - RGBLIGHT_VAL_STEP; |
0a40654b YL |
317 | } |
318 | rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val); | |
319 | } | |
320 | ||
3a860c4b JO |
321 | void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { |
322 | inmem_config.raw = rgblight_config.raw; | |
0a40654b | 323 | if (rgblight_config.enable) { |
e9f74875 | 324 | LED_TYPE tmp_led; |
0a40654b | 325 | sethsv(hue, sat, val, &tmp_led); |
3a860c4b JO |
326 | inmem_config.hue = hue; |
327 | inmem_config.sat = sat; | |
328 | inmem_config.val = val; | |
0a40654b YL |
329 | // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val); |
330 | rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b); | |
331 | } | |
332 | } | |
3a860c4b | 333 | void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) { |
0a40654b | 334 | if (rgblight_config.enable) { |
3a860c4b JO |
335 | if (rgblight_config.mode == 1) { |
336 | // same static color | |
337 | rgblight_sethsv_noeeprom(hue, sat, val); | |
338 | } else { | |
339 | // all LEDs in same color | |
340 | if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { | |
341 | // breathing mode, ignore the change of val, use in memory value instead | |
342 | val = rgblight_config.val; | |
343 | } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) { | |
344 | // rainbow mood and rainbow swirl, ignore the change of hue | |
345 | hue = rgblight_config.hue; | |
a7882b1f | 346 | } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) { |
347 | // static gradient | |
348 | uint16_t _hue; | |
349 | int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1; | |
350 | uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]); | |
351 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | |
352 | _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360; | |
353 | dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range); | |
354 | sethsv(_hue, sat, val, (LED_TYPE *)&led[i]); | |
355 | } | |
356 | rgblight_set(); | |
3a860c4b JO |
357 | } |
358 | } | |
359 | rgblight_config.hue = hue; | |
360 | rgblight_config.sat = sat; | |
361 | rgblight_config.val = val; | |
362 | eeconfig_update_rgblight(rgblight_config.raw); | |
363 | xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val); | |
364 | } | |
365 | } | |
366 | ||
367 | void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { | |
0a40654b | 368 | // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b); |
3a860c4b | 369 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { |
0a40654b YL |
370 | led[i].r = r; |
371 | led[i].g = g; | |
372 | led[i].b = b; | |
373 | } | |
374 | rgblight_set(); | |
0a40654b YL |
375 | } |
376 | ||
d4cd5dda | 377 | #ifndef RGBLIGHT_CUSTOM_DRIVER |
0a40654b | 378 | void rgblight_set(void) { |
3a860c4b | 379 | if (rgblight_config.enable) { |
b8679bbe JH |
380 | #ifdef RGBW |
381 | ws2812_setleds_rgbw(led, RGBLED_NUM); | |
382 | #else | |
383 | ws2812_setleds(led, RGBLED_NUM); | |
384 | #endif | |
3a860c4b JO |
385 | } else { |
386 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | |
387 | led[i].r = 0; | |
388 | led[i].g = 0; | |
389 | led[i].b = 0; | |
390 | } | |
b8679bbe JH |
391 | #ifdef RGBW |
392 | ws2812_setleds_rgbw(led, RGBLED_NUM); | |
393 | #else | |
394 | ws2812_setleds(led, RGBLED_NUM); | |
395 | #endif | |
3a860c4b | 396 | } |
0a40654b | 397 | } |
d4cd5dda | 398 | #endif |
0a40654b | 399 | |
3774a7fc | 400 | #ifdef RGBLIGHT_ANIMATIONS |
57e08eb8 | 401 | |
0a40654b YL |
402 | // Animation timer -- AVR Timer3 |
403 | void rgblight_timer_init(void) { | |
e9f74875 JH |
404 | // static uint8_t rgblight_timer_is_init = 0; |
405 | // if (rgblight_timer_is_init) { | |
406 | // return; | |
407 | // } | |
408 | // rgblight_timer_is_init = 1; | |
409 | // /* Timer 3 setup */ | |
410 | // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP | |
411 | // | _BV(CS30); // Clock selelct: clk/1 | |
412 | // /* Set TOP value */ | |
413 | // uint8_t sreg = SREG; | |
414 | // cli(); | |
415 | // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff; | |
416 | // OCR3AL = RGBLED_TIMER_TOP & 0xff; | |
417 | // SREG = sreg; | |
418 | ||
419 | rgblight_timer_enabled = true; | |
0a40654b YL |
420 | } |
421 | void rgblight_timer_enable(void) { | |
e9f74875 | 422 | rgblight_timer_enabled = true; |
3a860c4b | 423 | dprintf("TIMER3 enabled.\n"); |
0a40654b YL |
424 | } |
425 | void rgblight_timer_disable(void) { | |
e9f74875 | 426 | rgblight_timer_enabled = false; |
3a860c4b | 427 | dprintf("TIMER3 disabled.\n"); |
0a40654b YL |
428 | } |
429 | void rgblight_timer_toggle(void) { | |
e9f74875 | 430 | rgblight_timer_enabled ^= rgblight_timer_enabled; |
3a860c4b | 431 | dprintf("TIMER3 toggled.\n"); |
0a40654b YL |
432 | } |
433 | ||
4094544d EZ |
434 | void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) { |
435 | rgblight_enable(); | |
436 | rgblight_mode(1); | |
437 | rgblight_setrgb(r, g, b); | |
438 | } | |
439 | ||
e9f74875 JH |
440 | void rgblight_task(void) { |
441 | if (rgblight_timer_enabled) { | |
442 | // mode = 1, static light, do nothing here | |
443 | if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { | |
444 | // mode = 2 to 5, breathing mode | |
445 | rgblight_effect_breathing(rgblight_config.mode - 2); | |
446 | } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) { | |
447 | // mode = 6 to 8, rainbow mood mod | |
448 | rgblight_effect_rainbow_mood(rgblight_config.mode - 6); | |
449 | } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) { | |
450 | // mode = 9 to 14, rainbow swirl mode | |
451 | rgblight_effect_rainbow_swirl(rgblight_config.mode - 9); | |
452 | } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) { | |
453 | // mode = 15 to 20, snake mode | |
454 | rgblight_effect_snake(rgblight_config.mode - 15); | |
455 | } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { | |
456 | // mode = 21 to 23, knight mode | |
457 | rgblight_effect_knight(rgblight_config.mode - 21); | |
0e548f8b | 458 | } else if (rgblight_config.mode == 24) { |
cae269b0 JH |
459 | // mode = 24, christmas mode |
460 | rgblight_effect_christmas(); | |
e9f74875 | 461 | } |
3a860c4b JO |
462 | } |
463 | } | |
464 | ||
465 | // Effects | |
0a40654b | 466 | void rgblight_effect_breathing(uint8_t interval) { |
3a860c4b JO |
467 | static uint8_t pos = 0; |
468 | static uint16_t last_timer = 0; | |
ad49db8c | 469 | float val; |
0a40654b | 470 | |
3a860c4b JO |
471 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) { |
472 | return; | |
473 | } | |
474 | last_timer = timer_read(); | |
0a40654b | 475 | |
ad49db8c | 476 | |
477 | // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/ | |
478 | val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E)); | |
479 | rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, val); | |
3a860c4b | 480 | pos = (pos + 1) % 256; |
0a40654b | 481 | } |
0a40654b | 482 | void rgblight_effect_rainbow_mood(uint8_t interval) { |
3a860c4b JO |
483 | static uint16_t current_hue = 0; |
484 | static uint16_t last_timer = 0; | |
0a40654b | 485 | |
3a860c4b JO |
486 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) { |
487 | return; | |
488 | } | |
489 | last_timer = timer_read(); | |
490 | rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val); | |
491 | current_hue = (current_hue + 1) % 360; | |
0a40654b | 492 | } |
0a40654b | 493 | void rgblight_effect_rainbow_swirl(uint8_t interval) { |
3a860c4b JO |
494 | static uint16_t current_hue = 0; |
495 | static uint16_t last_timer = 0; | |
496 | uint16_t hue; | |
497 | uint8_t i; | |
da887ea4 | 498 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) { |
3a860c4b JO |
499 | return; |
500 | } | |
501 | last_timer = timer_read(); | |
502 | for (i = 0; i < RGBLED_NUM; i++) { | |
503 | hue = (360 / RGBLED_NUM * i + current_hue) % 360; | |
e9f74875 | 504 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); |
3a860c4b JO |
505 | } |
506 | rgblight_set(); | |
507 | ||
508 | if (interval % 2) { | |
509 | current_hue = (current_hue + 1) % 360; | |
510 | } else { | |
511 | if (current_hue - 1 < 0) { | |
512 | current_hue = 359; | |
513 | } else { | |
514 | current_hue = current_hue - 1; | |
515 | } | |
516 | } | |
0a40654b YL |
517 | } |
518 | void rgblight_effect_snake(uint8_t interval) { | |
3a860c4b JO |
519 | static uint8_t pos = 0; |
520 | static uint16_t last_timer = 0; | |
521 | uint8_t i, j; | |
522 | int8_t k; | |
899c88cd | 523 | int8_t increment = 1; |
3a860c4b | 524 | if (interval % 2) { |
899c88cd | 525 | increment = -1; |
3a860c4b JO |
526 | } |
527 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) { | |
528 | return; | |
529 | } | |
530 | last_timer = timer_read(); | |
531 | for (i = 0; i < RGBLED_NUM; i++) { | |
532 | led[i].r = 0; | |
533 | led[i].g = 0; | |
534 | led[i].b = 0; | |
535 | for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) { | |
899c88cd | 536 | k = pos + j * increment; |
3a860c4b JO |
537 | if (k < 0) { |
538 | k = k + RGBLED_NUM; | |
539 | } | |
540 | if (i == k) { | |
e9f74875 | 541 | sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), (LED_TYPE *)&led[i]); |
3a860c4b JO |
542 | } |
543 | } | |
544 | } | |
545 | rgblight_set(); | |
899c88cd | 546 | if (increment == 1) { |
3a860c4b JO |
547 | if (pos - 1 < 0) { |
548 | pos = RGBLED_NUM - 1; | |
549 | } else { | |
550 | pos -= 1; | |
551 | } | |
552 | } else { | |
553 | pos = (pos + 1) % RGBLED_NUM; | |
554 | } | |
0a40654b | 555 | } |
0a40654b | 556 | void rgblight_effect_knight(uint8_t interval) { |
3a860c4b | 557 | static uint16_t last_timer = 0; |
3a860c4b JO |
558 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { |
559 | return; | |
560 | } | |
561 | last_timer = timer_read(); | |
4edfa97e DS |
562 | |
563 | static int8_t low_bound = 0; | |
564 | static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1; | |
565 | static int8_t increment = 1; | |
566 | uint8_t i, cur; | |
567 | ||
4580d3a7 | 568 | // Set all the LEDs to 0 |
94f8b758 | 569 | for (i = 0; i < RGBLED_NUM; i++) { |
4580d3a7 | 570 | led[i].r = 0; |
571 | led[i].g = 0; | |
572 | led[i].b = 0; | |
573 | } | |
574 | // Determine which LEDs should be lit up | |
575 | for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) { | |
94f8b758 | 576 | cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM; |
4edfa97e DS |
577 | |
578 | if (i >= low_bound && i <= high_bound) { | |
579 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]); | |
3a860c4b | 580 | } else { |
4edfa97e DS |
581 | led[cur].r = 0; |
582 | led[cur].g = 0; | |
583 | led[cur].b = 0; | |
3a860c4b JO |
584 | } |
585 | } | |
4edfa97e DS |
586 | rgblight_set(); |
587 | ||
4580d3a7 | 588 | // Move from low_bound to high_bound changing the direction we increment each |
589 | // time a boundary is hit. | |
4edfa97e DS |
590 | low_bound += increment; |
591 | high_bound += increment; | |
592 | ||
4580d3a7 | 593 | if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) { |
4edfa97e DS |
594 | increment = -increment; |
595 | } | |
3a860c4b JO |
596 | } |
597 | ||
cae269b0 JH |
598 | |
599 | void rgblight_effect_christmas(void) { | |
600 | static uint16_t current_offset = 0; | |
601 | static uint16_t last_timer = 0; | |
602 | uint16_t hue; | |
603 | uint8_t i; | |
0e548f8b | 604 | if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) { |
cae269b0 JH |
605 | return; |
606 | } | |
607 | last_timer = timer_read(); | |
608 | current_offset = (current_offset + 1) % 2; | |
609 | for (i = 0; i < RGBLED_NUM; i++) { | |
0e548f8b | 610 | hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120; |
cae269b0 JH |
611 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); |
612 | } | |
613 | rgblight_set(); | |
614 | } | |
615 | ||
3a860c4b | 616 | #endif |