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