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