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