]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgblight.c
Improve RGB Light code (#4326)
[qmk_firmware.git] / quantum / rgblight.c
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  */
16 #include <math.h>
17 #include <string.h>
18 #ifdef __AVR__
19   #include <avr/eeprom.h>
20   #include <avr/interrupt.h>
21 #endif
22 #ifdef STM32_EEPROM_ENABLE
23   #include "hal.h"
24   #include "eeprom.h"
25   #include "eeprom_stm32.h"
26 #endif
27 #include "wait.h"
28 #include "progmem.h"
29 #include "timer.h"
30 #include "rgblight.h"
31 #include "debug.h"
32 #include "led_tables.h"
33
34 #ifndef RGBLIGHT_LIMIT_VAL
35 #define RGBLIGHT_LIMIT_VAL 255
36 #endif
37
38 #define _RGBM_SINGLE_STATIC(sym)   RGBLIGHT_MODE_ ## sym,
39 #define _RGBM_SINGLE_DYNAMIC(sym)
40 #define _RGBM_MULTI_STATIC(sym)    RGBLIGHT_MODE_ ## sym,
41 #define _RGBM_MULTI_DYNAMIC(sym)
42 #define _RGBM_TMP_STATIC(sym)      RGBLIGHT_MODE_ ## sym,
43 #define _RGBM_TMP_DYNAMIC(sym)
44 static uint8_t static_effect_table [] = {
45 #include "rgblight.h"
46 };
47
48 static inline int is_static_effect(uint8_t mode) {
49     return memchr(static_effect_table, mode, sizeof(static_effect_table)) != NULL;
50 }
51
52 #define MIN(a,b) (((a)<(b))?(a):(b))
53 #define MAX(a,b) (((a)>(b))?(a):(b))
54
55 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
56 __attribute__ ((weak))
57 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
58 #endif
59
60 rgblight_config_t rgblight_config;
61 bool is_rgblight_initialized = false;
62
63 LED_TYPE led[RGBLED_NUM];
64 bool rgblight_timer_enabled = false;
65
66 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
67   uint8_t r = 0, g = 0, b = 0, base, color;
68
69   if (val > RGBLIGHT_LIMIT_VAL) {
70       val=RGBLIGHT_LIMIT_VAL; // limit the val
71   }
72
73   if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
74     r = val;
75     g = val;
76     b = val;
77   } else {
78     base = ((255 - sat) * val) >> 8;
79     color = (val - base) * (hue % 60) / 60;
80
81     switch (hue / 60) {
82       case 0:
83         r = val;
84         g = base + color;
85         b = base;
86         break;
87       case 1:
88         r = val - color;
89         g = val;
90         b = base;
91         break;
92       case 2:
93         r = base;
94         g = val;
95         b = base + color;
96         break;
97       case 3:
98         r = base;
99         g = val - color;
100         b = val;
101         break;
102       case 4:
103         r = base + color;
104         g = base;
105         b = val;
106         break;
107       case 5:
108         r = val;
109         g = base;
110         b = val - color;
111         break;
112     }
113   }
114   r = pgm_read_byte(&CIE1931_CURVE[r]);
115   g = pgm_read_byte(&CIE1931_CURVE[g]);
116   b = pgm_read_byte(&CIE1931_CURVE[b]);
117
118   setrgb(r, g, b, led1);
119 }
120
121 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
122   (*led1).r = r;
123   (*led1).g = g;
124   (*led1).b = b;
125 }
126
127 void rgblight_check_config(void) {
128   /* Add some out of bound checks for RGB light config */
129
130   if (rgblight_config.mode < RGBLIGHT_MODE_STATIC_LIGHT) {
131     rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
132   }
133   else if (rgblight_config.mode > RGBLIGHT_MODES) {
134     rgblight_config.mode = RGBLIGHT_MODES;
135   }
136
137   if (rgblight_config.hue < 0) {
138     rgblight_config.hue = 0;
139   } else if (rgblight_config.hue > 360) {
140     rgblight_config.hue %= 360;
141   }
142
143   if (rgblight_config.sat < 0) {
144     rgblight_config.sat = 0;
145   } else if (rgblight_config.sat > 255) {
146     rgblight_config.sat = 255;
147   }
148
149   if (rgblight_config.val < 0) {
150     rgblight_config.val = 0;
151   } else if (rgblight_config.val > RGBLIGHT_LIMIT_VAL) {
152     rgblight_config.val = RGBLIGHT_LIMIT_VAL;
153   }
154
155 }
156
157 uint32_t eeconfig_read_rgblight(void) {
158   #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
159     return eeprom_read_dword(EECONFIG_RGBLIGHT);
160   #else
161     return 0;
162   #endif
163 }
164
165 void eeconfig_update_rgblight(uint32_t val) {
166   #if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
167     rgblight_check_config();
168     eeprom_update_dword(EECONFIG_RGBLIGHT, val);
169   #endif
170 }
171
172 void eeconfig_update_rgblight_default(void) {
173   //dprintf("eeconfig_update_rgblight_default\n");
174   rgblight_config.enable = 1;
175   rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
176   rgblight_config.hue = 0;
177   rgblight_config.sat = 255;
178   rgblight_config.val = RGBLIGHT_LIMIT_VAL;
179   rgblight_config.speed = 0;
180   eeconfig_update_rgblight(rgblight_config.raw);
181 }
182
183 void eeconfig_debug_rgblight(void) {
184   dprintf("rgblight_config eprom\n");
185   dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
186   dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
187   dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
188   dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
189   dprintf("rgblight_config.val = %d\n", rgblight_config.val);
190   dprintf("rgblight_config.speed = %d\n", rgblight_config.speed);
191 }
192
193 void rgblight_init(void) {
194   /* if already initialized, don't do it again.
195      If you must do it again, extern this and set to false, first.
196      This is a dirty, dirty hack until proper hooks can be added for keyboard startup. */
197   if (is_rgblight_initialized) { return; }
198
199   debug_enable = 1; // Debug ON!
200   dprintf("rgblight_init called.\n");
201   dprintf("rgblight_init start!\n");
202   if (!eeconfig_is_enabled()) {
203     dprintf("rgblight_init eeconfig is not enabled.\n");
204     eeconfig_init();
205     eeconfig_update_rgblight_default();
206   }
207   rgblight_config.raw = eeconfig_read_rgblight();
208   if (!rgblight_config.mode) {
209     dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
210     eeconfig_update_rgblight_default();
211     rgblight_config.raw = eeconfig_read_rgblight();
212   }
213   rgblight_check_config();
214
215   eeconfig_debug_rgblight(); // display current eeprom values
216
217 #ifdef RGBLIGHT_USE_TIMER
218     rgblight_timer_init(); // setup the timer
219 #endif
220
221   if (rgblight_config.enable) {
222     rgblight_mode_noeeprom(rgblight_config.mode);
223   }
224
225   is_rgblight_initialized = true;
226
227 }
228
229 void rgblight_update_dword(uint32_t dword) {
230   rgblight_config.raw = dword;
231   eeconfig_update_rgblight(rgblight_config.raw);
232   if (rgblight_config.enable)
233     rgblight_mode(rgblight_config.mode);
234   else {
235 #ifdef RGBLIGHT_USE_TIMER
236       rgblight_timer_disable();
237 #endif
238       rgblight_set();
239   }
240 }
241
242 void rgblight_increase(void) {
243   uint8_t mode = 0;
244   if (rgblight_config.mode < RGBLIGHT_MODES) {
245     mode = rgblight_config.mode + 1;
246   }
247   rgblight_mode(mode);
248 }
249 void rgblight_decrease(void) {
250   uint8_t mode = 0;
251   // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
252   if (rgblight_config.mode > RGBLIGHT_MODE_STATIC_LIGHT) {
253     mode = rgblight_config.mode - 1;
254   }
255   rgblight_mode(mode);
256 }
257 void rgblight_step_helper(bool write_to_eeprom) {
258   uint8_t mode = 0;
259   mode = rgblight_config.mode + 1;
260   if (mode > RGBLIGHT_MODES) {
261     mode = 1;
262   }
263   rgblight_mode_eeprom_helper(mode, write_to_eeprom);
264 }
265 void rgblight_step_noeeprom(void) {
266   rgblight_step_helper(false);
267 }
268 void rgblight_step(void) {
269   rgblight_step_helper(true);
270 }
271 void rgblight_step_reverse_helper(bool write_to_eeprom) {
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);
278 }
279 void rgblight_step_reverse_noeeprom(void) {
280   rgblight_step_reverse_helper(false);
281 }
282 void rgblight_step_reverse(void) {
283   rgblight_step_reverse_helper(true);
284 }
285
286 uint8_t rgblight_get_mode(void) {
287   if (!rgblight_config.enable) {
288     return false;
289   }
290
291   return rgblight_config.mode;
292 }
293
294 void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) {
295   if (!rgblight_config.enable) {
296     return;
297   }
298   if (mode < RGBLIGHT_MODE_STATIC_LIGHT) {
299     rgblight_config.mode = RGBLIGHT_MODE_STATIC_LIGHT;
300   } else if (mode > RGBLIGHT_MODES) {
301     rgblight_config.mode = RGBLIGHT_MODES;
302   } else {
303     rgblight_config.mode = mode;
304   }
305   if (write_to_eeprom) {
306     eeconfig_update_rgblight(rgblight_config.raw);
307     xprintf("rgblight mode [EEPROM]: %u\n", rgblight_config.mode);
308   } else {
309     xprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode);
310   }
311   if( is_static_effect(rgblight_config.mode) ) {
312 #ifdef RGBLIGHT_USE_TIMER
313       rgblight_timer_disable();
314 #endif
315   } else {
316 #ifdef RGBLIGHT_USE_TIMER
317       rgblight_timer_enable();
318 #endif
319   }
320   rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
321 }
322
323 void rgblight_mode(uint8_t mode) {
324   rgblight_mode_eeprom_helper(mode, true);
325 }
326
327 void rgblight_mode_noeeprom(uint8_t mode) {
328   rgblight_mode_eeprom_helper(mode, false);
329 }
330
331
332 void rgblight_toggle(void) {
333   xprintf("rgblight toggle [EEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
334   if (rgblight_config.enable) {
335     rgblight_disable();
336   }
337   else {
338     rgblight_enable();
339   }
340 }
341
342 void rgblight_toggle_noeeprom(void) {
343   xprintf("rgblight toggle [NOEEPROM]: rgblight_config.enable = %u\n", !rgblight_config.enable);
344   if (rgblight_config.enable) {
345     rgblight_disable_noeeprom();
346   }
347   else {
348     rgblight_enable_noeeprom();
349   }
350 }
351
352 void rgblight_enable(void) {
353   rgblight_config.enable = 1;
354   // No need to update EEPROM here. rgblight_mode() will do that, actually
355   //eeconfig_update_rgblight(rgblight_config.raw);
356   xprintf("rgblight enable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
357   rgblight_mode(rgblight_config.mode);
358 }
359
360 void rgblight_enable_noeeprom(void) {
361   rgblight_config.enable = 1;
362   xprintf("rgblight enable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
363   rgblight_mode_noeeprom(rgblight_config.mode);
364 }
365
366 void rgblight_disable(void) {
367   rgblight_config.enable = 0;
368   eeconfig_update_rgblight(rgblight_config.raw);
369   xprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
370 #ifdef RGBLIGHT_USE_TIMER
371       rgblight_timer_disable();
372 #endif
373   wait_ms(50);
374   rgblight_set();
375 }
376
377 void rgblight_disable_noeeprom(void) {
378   rgblight_config.enable = 0;
379   xprintf("rgblight disable [noEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable);
380 #ifdef RGBLIGHT_USE_TIMER
381     rgblight_timer_disable();
382 #endif
383   wait_ms(50);
384   rgblight_set();
385 }
386
387
388 // Deals with the messy details of incrementing an integer
389 static uint8_t increment( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
390     int16_t new_value = value;
391     new_value += step;
392     return MIN( MAX( new_value, min ), max );
393 }
394
395 static uint8_t decrement( uint8_t value, uint8_t step, uint8_t min, uint8_t max ) {
396     int16_t new_value = value;
397     new_value -= step;
398     return MIN( MAX( new_value, min ), max );
399 }
400
401 void rgblight_increase_hue_helper(bool write_to_eeprom) {
402   uint16_t hue;
403   hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
404   rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
405 }
406 void rgblight_increase_hue_noeeprom(void) {
407   rgblight_increase_hue_helper(false);
408 }
409 void rgblight_increase_hue(void) {
410   rgblight_increase_hue_helper(true);
411 }
412 void rgblight_decrease_hue_helper(bool write_to_eeprom) {
413   uint16_t hue;
414   if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
415     hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
416   } else {
417     hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
418   }
419   rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom);
420 }
421 void rgblight_decrease_hue_noeeprom(void) {
422   rgblight_decrease_hue_helper(false);
423 }
424 void rgblight_decrease_hue(void) {
425   rgblight_decrease_hue_helper(true);
426 }
427 void rgblight_increase_sat_helper(bool write_to_eeprom) {
428   uint8_t sat;
429   if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
430     sat = 255;
431   } else {
432     sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
433   }
434   rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
435 }
436 void rgblight_increase_sat_noeeprom(void) {
437   rgblight_increase_sat_helper(false);
438 }
439 void rgblight_increase_sat(void) {
440   rgblight_increase_sat_helper(true);
441 }
442 void rgblight_decrease_sat_helper(bool write_to_eeprom) {
443   uint8_t sat;
444   if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
445     sat = 0;
446   } else {
447     sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
448   }
449   rgblight_sethsv_eeprom_helper(rgblight_config.hue, sat, rgblight_config.val, write_to_eeprom);
450 }
451 void rgblight_decrease_sat_noeeprom(void) {
452   rgblight_decrease_sat_helper(false);
453 }
454 void rgblight_decrease_sat(void) {
455   rgblight_decrease_sat_helper(true);
456 }
457 void rgblight_increase_val_helper(bool write_to_eeprom) {
458   uint8_t val;
459   if (rgblight_config.val + RGBLIGHT_VAL_STEP > RGBLIGHT_LIMIT_VAL) {
460     val = RGBLIGHT_LIMIT_VAL;
461   } else {
462     val = rgblight_config.val + RGBLIGHT_VAL_STEP;
463   }
464   rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
465 }
466 void rgblight_increase_val_noeeprom(void) {
467   rgblight_increase_val_helper(false);
468 }
469 void rgblight_increase_val(void) {
470   rgblight_increase_val_helper(true);
471 }
472 void rgblight_decrease_val_helper(bool write_to_eeprom) {
473   uint8_t val;
474   if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
475     val = 0;
476   } else {
477     val = rgblight_config.val - RGBLIGHT_VAL_STEP;
478   }
479   rgblight_sethsv_eeprom_helper(rgblight_config.hue, rgblight_config.sat, val, write_to_eeprom);
480 }
481 void rgblight_decrease_val_noeeprom(void) {
482   rgblight_decrease_val_helper(false);
483 }
484 void rgblight_decrease_val(void) {
485   rgblight_decrease_val_helper(true);
486 }
487 void rgblight_increase_speed(void) {
488     rgblight_config.speed = increment( rgblight_config.speed, 1, 0, 3 );
489     eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
490 }
491
492 void rgblight_decrease_speed(void) {
493     rgblight_config.speed = decrement( rgblight_config.speed, 1, 0, 3 );
494     eeconfig_update_rgblight(rgblight_config.raw);//EECONFIG needs to be increased to support this
495 }
496
497 void rgblight_sethsv_noeeprom_old(uint16_t hue, uint8_t sat, uint8_t val) {
498   if (rgblight_config.enable) {
499     LED_TYPE tmp_led;
500     sethsv(hue, sat, val, &tmp_led);
501     // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
502     rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
503   }
504 }
505
506 void rgblight_sethsv_eeprom_helper(uint16_t hue, uint8_t sat, uint8_t val, bool write_to_eeprom) {
507   if (rgblight_config.enable) {
508     if (rgblight_config.mode == RGBLIGHT_MODE_STATIC_LIGHT) {
509       // same static color
510       LED_TYPE tmp_led;
511       sethsv(hue, sat, val, &tmp_led);
512       rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
513     } else {
514       // all LEDs in same color
515       if ( 1 == 0 ) { //dummy
516       }
517 #ifdef RGBLIGHT_EFFECT_BREATHING
518       else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING &&
519           rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
520         // breathing mode, ignore the change of val, use in memory value instead
521         val = rgblight_config.val;
522       }
523 #endif
524 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
525       else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
526                   rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
527         // rainbow mood, ignore the change of hue
528         hue = rgblight_config.hue;
529       }
530 #endif
531 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
532       else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
533                rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
534         // rainbow swirl, ignore the change of hue
535         hue = rgblight_config.hue;
536       }
537 #endif
538 #ifdef RGBLIGHT_EFFECT_STATIC_GRADIENT
539       else if (rgblight_config.mode >= RGBLIGHT_MODE_STATIC_GRADIENT &&
540                rgblight_config.mode <= RGBLIGHT_MODE_STATIC_GRADIENT_end) {
541         // static gradient
542         uint16_t _hue;
543         int8_t direction = ((rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) % 2) ? -1 : 1;
544         uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - RGBLIGHT_MODE_STATIC_GRADIENT) / 2]);
545         for (uint8_t i = 0; i < RGBLED_NUM; i++) {
546           _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
547           dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
548           sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
549         }
550         rgblight_set();
551       }
552 #endif
553     }
554     rgblight_config.hue = hue;
555     rgblight_config.sat = sat;
556     rgblight_config.val = val;
557     if (write_to_eeprom) {
558       eeconfig_update_rgblight(rgblight_config.raw);
559       xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
560     } else {
561       xprintf("rgblight set hsv [NOEEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
562     }
563   }
564 }
565
566 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
567   rgblight_sethsv_eeprom_helper(hue, sat, val, true);
568 }
569
570 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
571   rgblight_sethsv_eeprom_helper(hue, sat, val, false);
572 }
573
574 uint16_t rgblight_get_hue(void) {
575   return rgblight_config.hue;
576 }
577
578 uint8_t rgblight_get_sat(void) {
579   return rgblight_config.sat;
580 }
581
582 uint8_t rgblight_get_val(void) {
583   return rgblight_config.val;
584 }
585
586 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
587   if (!rgblight_config.enable) { return; }
588
589   for (uint8_t i = 0; i < RGBLED_NUM; i++) {
590     led[i].r = r;
591     led[i].g = g;
592     led[i].b = b;
593   }
594   rgblight_set();
595 }
596
597 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
598   if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
599
600   led[index].r = r;
601   led[index].g = g;
602   led[index].b = b;
603   rgblight_set();
604 }
605
606 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
607   if (!rgblight_config.enable) { return; }
608
609   LED_TYPE tmp_led;
610   sethsv(hue, sat, val, &tmp_led);
611   rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
612 }
613
614 #ifndef RGBLIGHT_CUSTOM_DRIVER
615 void rgblight_set(void) {
616   if (rgblight_config.enable) {
617     #ifdef RGBW
618       ws2812_setleds_rgbw(led, RGBLED_NUM);
619     #else
620       ws2812_setleds(led, RGBLED_NUM);
621     #endif
622   } else {
623     for (uint8_t i = 0; i < RGBLED_NUM; i++) {
624       led[i].r = 0;
625       led[i].g = 0;
626       led[i].b = 0;
627     }
628     #ifdef RGBW
629       ws2812_setleds_rgbw(led, RGBLED_NUM);
630     #else
631       ws2812_setleds(led, RGBLED_NUM);
632     #endif
633   }
634 }
635 #endif
636
637 #ifdef RGBLIGHT_USE_TIMER
638
639 // Animation timer -- AVR Timer3
640 void rgblight_timer_init(void) {
641   // static uint8_t rgblight_timer_is_init = 0;
642   // if (rgblight_timer_is_init) {
643   //   return;
644   // }
645   // rgblight_timer_is_init = 1;
646   // /* Timer 3 setup */
647   // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
648   //       | _BV(CS30); // Clock selelct: clk/1
649   // /* Set TOP value */
650   // uint8_t sreg = SREG;
651   // cli();
652   // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
653   // OCR3AL = RGBLED_TIMER_TOP & 0xff;
654   // SREG = sreg;
655
656   rgblight_timer_enabled = true;
657 }
658 void rgblight_timer_enable(void) {
659   rgblight_timer_enabled = true;
660   dprintf("TIMER3 enabled.\n");
661 }
662 void rgblight_timer_disable(void) {
663   rgblight_timer_enabled = false;
664   dprintf("TIMER3 disabled.\n");
665 }
666 void rgblight_timer_toggle(void) {
667   rgblight_timer_enabled ^= rgblight_timer_enabled;
668   dprintf("TIMER3 toggled.\n");
669 }
670
671 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
672   rgblight_enable();
673   rgblight_mode(RGBLIGHT_MODE_STATIC_LIGHT);
674   rgblight_setrgb(r, g, b);
675 }
676
677 void rgblight_task(void) {
678   if (rgblight_timer_enabled) {
679     // static light mode, do nothing here
680     if ( 1 == 0 ) { //dummy
681     }
682 #ifdef RGBLIGHT_EFFECT_BREATHING
683     else if (rgblight_config.mode >= RGBLIGHT_MODE_BREATHING  &&
684         rgblight_config.mode <= RGBLIGHT_MODE_BREATHING_end) {
685       // breathing mode
686       rgblight_effect_breathing(rgblight_config.mode - RGBLIGHT_MODE_BREATHING );
687     }
688 #endif
689 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
690     else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_MOOD &&
691                rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_MOOD_end) {
692       // rainbow mood mode
693       rgblight_effect_rainbow_mood(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_MOOD);
694     }
695 #endif
696 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
697     else if (rgblight_config.mode >= RGBLIGHT_MODE_RAINBOW_SWIRL &&
698                rgblight_config.mode <= RGBLIGHT_MODE_RAINBOW_SWIRL_end) {
699       // rainbow swirl mode
700       rgblight_effect_rainbow_swirl(rgblight_config.mode - RGBLIGHT_MODE_RAINBOW_SWIRL);
701     }
702 #endif
703 #ifdef RGBLIGHT_EFFECT_SNAKE
704     else if (rgblight_config.mode >= RGBLIGHT_MODE_SNAKE &&
705                rgblight_config.mode <= RGBLIGHT_MODE_SNAKE_end) {
706       // snake mode
707       rgblight_effect_snake(rgblight_config.mode - RGBLIGHT_MODE_SNAKE);
708     }
709 #endif
710 #ifdef RGBLIGHT_EFFECT_KNIGHT
711     else if (rgblight_config.mode >= RGBLIGHT_MODE_KNIGHT &&
712                rgblight_config.mode <= RGBLIGHT_MODE_KNIGHT_end) {
713       // knight mode
714       rgblight_effect_knight(rgblight_config.mode - RGBLIGHT_MODE_KNIGHT);
715     }
716 #endif
717 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
718     else if (rgblight_config.mode == RGBLIGHT_MODE_CHRISTMAS) {
719       // christmas mode
720       rgblight_effect_christmas();
721     }
722 #endif
723 #ifdef RGBLIGHT_EFFECT_RGB_TEST
724     else if (rgblight_config.mode == RGBLIGHT_MODE_RGB_TEST) {
725       // RGB test mode
726       rgblight_effect_rgbtest();
727     }
728 #endif
729 #ifdef RGBLIGHT_EFFECT_ALTERNATING
730     else if (rgblight_config.mode == RGBLIGHT_MODE_ALTERNATING){
731       rgblight_effect_alternating();
732     }
733 #endif
734   }
735 }
736
737 #endif /* RGBLIGHT_USE_TIMER */
738
739 // Effects
740 #ifdef RGBLIGHT_EFFECT_BREATHING
741 __attribute__ ((weak))
742 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
743
744 void rgblight_effect_breathing(uint8_t interval) {
745   static uint8_t pos = 0;
746   static uint16_t last_timer = 0;
747   float val;
748
749   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
750     return;
751   }
752   last_timer = timer_read();
753
754   // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
755   val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
756   rgblight_sethsv_noeeprom_old(rgblight_config.hue, rgblight_config.sat, val);
757   pos = (pos + 1) % 256;
758 }
759 #endif
760
761 #ifdef RGBLIGHT_EFFECT_RAINBOW_MOOD
762 __attribute__ ((weak))
763 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
764
765 void rgblight_effect_rainbow_mood(uint8_t interval) {
766   static uint16_t current_hue = 0;
767   static uint16_t last_timer = 0;
768
769   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
770     return;
771   }
772   last_timer = timer_read();
773   rgblight_sethsv_noeeprom_old(current_hue, rgblight_config.sat, rgblight_config.val);
774   current_hue = (current_hue + 1) % 360;
775 }
776 #endif
777
778 #ifdef RGBLIGHT_EFFECT_RAINBOW_SWIRL
779 #ifndef RGBLIGHT_RAINBOW_SWIRL_RANGE
780   #define RGBLIGHT_RAINBOW_SWIRL_RANGE 360
781 #endif
782
783 __attribute__ ((weak))
784 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
785
786 void rgblight_effect_rainbow_swirl(uint8_t interval) {
787   static uint16_t current_hue = 0;
788   static uint16_t last_timer = 0;
789   uint16_t hue;
790   uint8_t i;
791   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
792     return;
793   }
794   last_timer = timer_read();
795   for (i = 0; i < RGBLED_NUM; i++) {
796     hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / RGBLED_NUM * i + current_hue) % 360;
797     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
798   }
799   rgblight_set();
800
801   if (interval % 2) {
802     current_hue = (current_hue + 1) % 360;
803   } else {
804     if (current_hue - 1 < 0) {
805       current_hue = 359;
806     } else {
807       current_hue = current_hue - 1;
808     }
809   }
810 }
811 #endif
812
813 #ifdef RGBLIGHT_EFFECT_SNAKE
814 __attribute__ ((weak))
815 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
816
817 void rgblight_effect_snake(uint8_t interval) {
818   static uint8_t pos = 0;
819   static uint16_t last_timer = 0;
820   uint8_t i, j;
821   int8_t k;
822   int8_t increment = 1;
823   if (interval % 2) {
824     increment = -1;
825   }
826   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
827     return;
828   }
829   last_timer = timer_read();
830   for (i = 0; i < RGBLED_NUM; i++) {
831     led[i].r = 0;
832     led[i].g = 0;
833     led[i].b = 0;
834     for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
835       k = pos + j * increment;
836       if (k < 0) {
837         k = k + RGBLED_NUM;
838       }
839       if (i == k) {
840         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]);
841       }
842     }
843   }
844   rgblight_set();
845   if (increment == 1) {
846     if (pos - 1 < 0) {
847       pos = RGBLED_NUM - 1;
848     } else {
849       pos -= 1;
850     }
851   } else {
852     pos = (pos + 1) % RGBLED_NUM;
853   }
854 }
855 #endif
856
857 #ifdef RGBLIGHT_EFFECT_KNIGHT
858 __attribute__ ((weak))
859 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
860
861 void rgblight_effect_knight(uint8_t interval) {
862   static uint16_t last_timer = 0;
863   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
864     return;
865   }
866   last_timer = timer_read();
867
868   static int8_t low_bound = 0;
869   static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
870   static int8_t increment = 1;
871   uint8_t i, cur;
872
873   // Set all the LEDs to 0
874   for (i = 0; i < RGBLED_NUM; i++) {
875     led[i].r = 0;
876     led[i].g = 0;
877     led[i].b = 0;
878   }
879   // Determine which LEDs should be lit up
880   for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
881     cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
882
883     if (i >= low_bound && i <= high_bound) {
884       sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
885     } else {
886       led[cur].r = 0;
887       led[cur].g = 0;
888       led[cur].b = 0;
889     }
890   }
891   rgblight_set();
892
893   // Move from low_bound to high_bound changing the direction we increment each
894   // time a boundary is hit.
895   low_bound += increment;
896   high_bound += increment;
897
898   if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
899     increment = -increment;
900   }
901 }
902 #endif
903
904 #ifdef RGBLIGHT_EFFECT_CHRISTMAS
905 void rgblight_effect_christmas(void) {
906   static uint16_t current_offset = 0;
907   static uint16_t last_timer = 0;
908   uint16_t hue;
909   uint8_t i;
910   if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
911     return;
912   }
913   last_timer = timer_read();
914   current_offset = (current_offset + 1) % 2;
915   for (i = 0; i < RGBLED_NUM; i++) {
916     hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
917     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
918   }
919   rgblight_set();
920 }
921 #endif
922
923 #ifdef RGBLIGHT_EFFECT_RGB_TEST
924 __attribute__ ((weak))
925 const uint16_t RGBLED_RGBTEST_INTERVALS[] PROGMEM = {1024};
926
927 void rgblight_effect_rgbtest(void) {
928   static uint8_t pos = 0;
929   static uint16_t last_timer = 0;
930   static uint8_t maxval = 0;
931   uint8_t g; uint8_t r; uint8_t b;
932
933   if (timer_elapsed(last_timer) < pgm_read_word(&RGBLED_RGBTEST_INTERVALS[0])) {
934     return;
935   }
936
937   if( maxval == 0 ) {
938       LED_TYPE tmp_led;
939       sethsv(0, 255, RGBLIGHT_LIMIT_VAL, &tmp_led);
940       maxval = tmp_led.r;
941   }
942   last_timer = timer_read();
943   g = r = b = 0;
944   switch( pos ) {
945     case 0: r = maxval; break;
946     case 1: g = maxval; break;
947     case 2: b = maxval; break;
948   }
949   rgblight_setrgb(r, g, b);
950   pos = (pos + 1) % 3;
951 }
952 #endif
953
954 #ifdef RGBLIGHT_EFFECT_ALTERNATING
955 void rgblight_effect_alternating(void){
956   static uint16_t last_timer = 0;
957   static uint16_t pos = 0;
958   if (timer_elapsed(last_timer) < 500) {
959     return;
960   }
961   last_timer = timer_read();
962
963   for(int i = 0; i<RGBLED_NUM; i++){
964       if(i<RGBLED_NUM/2 && pos){
965           sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
966       }else if (i>=RGBLED_NUM/2 && !pos){
967           sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
968       }else{
969           sethsv(rgblight_config.hue, rgblight_config.sat, 0, (LED_TYPE *)&led[i]);
970       }
971   }
972   rgblight_set();
973   pos = (pos + 1) % 2;
974 }
975 #endif