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