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