]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgblight.c
49420de3769551cdff7531cf96a7603421e52006
[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 <avr/eeprom.h>
17 #include <avr/interrupt.h>
18 #include <util/delay.h>
19 #include "progmem.h"
20 #include "timer.h"
21 #include "rgblight.h"
22 #include "debug.h"
23 #include "led_tables.h"
24
25
26 __attribute__ ((weak))
27 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
28 __attribute__ ((weak))
29 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
30 __attribute__ ((weak))
31 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
32 __attribute__ ((weak))
33 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
34 __attribute__ ((weak))
35 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {100, 50, 20};
36 __attribute__ ((weak))
37 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
38
39 rgblight_config_t rgblight_config;
40 rgblight_config_t inmem_config;
41
42 LED_TYPE led[RGBLED_NUM];
43 uint8_t rgblight_inited = 0;
44 bool rgblight_timer_enabled = false;
45
46 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
47   uint8_t r = 0, g = 0, b = 0, base, color;
48
49   if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
50     r = val;
51     g = val;
52     b = val;
53   } else {
54     base = ((255 - sat) * val) >> 8;
55     color = (val - base) * (hue % 60) / 60;
56
57     switch (hue / 60) {
58       case 0:
59         r = val;
60         g = base + color;
61         b = base;
62         break;
63       case 1:
64         r = val - color;
65         g = val;
66         b = base;
67         break;
68       case 2:
69         r = base;
70         g = val;
71         b = base + color;
72         break;
73       case 3:
74         r = base;
75         g = val - color;
76         b = val;
77         break;
78       case 4:
79         r = base + color;
80         g = base;
81         b = val;
82         break;
83       case 5:
84         r = val;
85         g = base;
86         b = val - color;
87         break;
88     }
89   }
90   r = pgm_read_byte(&CIE1931_CURVE[r]);
91   g = pgm_read_byte(&CIE1931_CURVE[g]);
92   b = pgm_read_byte(&CIE1931_CURVE[b]);
93
94   setrgb(r, g, b, led1);
95 }
96
97 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
98   (*led1).r = r;
99   (*led1).g = g;
100   (*led1).b = b;
101 }
102
103
104 uint32_t eeconfig_read_rgblight(void) {
105   return eeprom_read_dword(EECONFIG_RGBLIGHT);
106 }
107 void eeconfig_update_rgblight(uint32_t val) {
108   eeprom_update_dword(EECONFIG_RGBLIGHT, val);
109 }
110 void eeconfig_update_rgblight_default(void) {
111   dprintf("eeconfig_update_rgblight_default\n");
112   rgblight_config.enable = 1;
113   rgblight_config.mode = 1;
114   rgblight_config.hue = 0;
115   rgblight_config.sat = 255;
116   rgblight_config.val = 255;
117   eeconfig_update_rgblight(rgblight_config.raw);
118 }
119 void eeconfig_debug_rgblight(void) {
120   dprintf("rgblight_config eprom\n");
121   dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
122   dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
123   dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
124   dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
125   dprintf("rgblight_config.val = %d\n", rgblight_config.val);
126 }
127
128 void rgblight_init(void) {
129   debug_enable = 1; // Debug ON!
130   dprintf("rgblight_init called.\n");
131   rgblight_inited = 1;
132   dprintf("rgblight_init start!\n");
133   if (!eeconfig_is_enabled()) {
134     dprintf("rgblight_init eeconfig is not enabled.\n");
135     eeconfig_init();
136     eeconfig_update_rgblight_default();
137   }
138   rgblight_config.raw = eeconfig_read_rgblight();
139   if (!rgblight_config.mode) {
140     dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
141     eeconfig_update_rgblight_default();
142     rgblight_config.raw = eeconfig_read_rgblight();
143   }
144   eeconfig_debug_rgblight(); // display current eeprom values
145
146   #ifdef RGBLIGHT_ANIMATIONS
147     rgblight_timer_init(); // setup the timer
148   #endif
149
150   if (rgblight_config.enable) {
151     rgblight_mode(rgblight_config.mode);
152   }
153 }
154
155 void rgblight_update_dword(uint32_t dword) {
156   rgblight_config.raw = dword;
157   eeconfig_update_rgblight(rgblight_config.raw);
158   if (rgblight_config.enable)
159     rgblight_mode(rgblight_config.mode);
160   else {
161     #ifdef RGBLIGHT_ANIMATIONS
162       rgblight_timer_disable();
163     #endif
164       rgblight_set();
165   }
166 }
167
168 void rgblight_increase(void) {
169   uint8_t mode = 0;
170   if (rgblight_config.mode < RGBLIGHT_MODES) {
171     mode = rgblight_config.mode + 1;
172   }
173   rgblight_mode(mode);
174 }
175 void rgblight_decrease(void) {
176   uint8_t mode = 0;
177   // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
178   if (rgblight_config.mode > 1) {
179     mode = rgblight_config.mode - 1;
180   }
181   rgblight_mode(mode);
182 }
183 void rgblight_step(void) {
184   uint8_t mode = 0;
185   mode = rgblight_config.mode + 1;
186   if (mode > RGBLIGHT_MODES) {
187     mode = 1;
188   }
189   rgblight_mode(mode);
190 }
191 void rgblight_step_reverse(void) {
192   uint8_t mode = 0;
193   mode = rgblight_config.mode - 1;
194   if (mode < 1) {
195     mode = RGBLIGHT_MODES;
196   }
197   rgblight_mode(mode);
198 }
199
200 void rgblight_mode(uint8_t mode) {
201   if (!rgblight_config.enable) {
202     return;
203   }
204   if (mode < 1) {
205     rgblight_config.mode = 1;
206   } else if (mode > RGBLIGHT_MODES) {
207     rgblight_config.mode = RGBLIGHT_MODES;
208   } else {
209     rgblight_config.mode = mode;
210   }
211   eeconfig_update_rgblight(rgblight_config.raw);
212   xprintf("rgblight mode: %u\n", rgblight_config.mode);
213   if (rgblight_config.mode == 1) {
214     #ifdef RGBLIGHT_ANIMATIONS
215       rgblight_timer_disable();
216     #endif
217   } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) {
218     // MODE 2-5, breathing
219     // MODE 6-8, rainbow mood
220     // MODE 9-14, rainbow swirl
221     // MODE 15-20, snake
222     // MODE 21-23, knight
223
224     #ifdef RGBLIGHT_ANIMATIONS
225       rgblight_timer_enable();
226     #endif
227   } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
228     // MODE 25-34, static gradient
229
230     #ifdef RGBLIGHT_ANIMATIONS
231       rgblight_timer_disable();
232     #endif
233   }
234   rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
235 }
236
237 void rgblight_toggle(void) {
238   rgblight_config.enable ^= 1;
239   eeconfig_update_rgblight(rgblight_config.raw);
240   xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
241   if (rgblight_config.enable) {
242     rgblight_mode(rgblight_config.mode);
243   } else {
244     #ifdef RGBLIGHT_ANIMATIONS
245       rgblight_timer_disable();
246     #endif
247     _delay_ms(50);
248     rgblight_set();
249   }
250 }
251
252 void rgblight_enable(void) {
253   rgblight_config.enable = 1;
254   eeconfig_update_rgblight(rgblight_config.raw);
255   xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable);
256   rgblight_mode(rgblight_config.mode);
257 }
258
259
260 void rgblight_increase_hue(void) {
261   uint16_t hue;
262   hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
263   rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
264 }
265 void rgblight_decrease_hue(void) {
266   uint16_t hue;
267   if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
268     hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
269   } else {
270     hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
271   }
272   rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
273 }
274 void rgblight_increase_sat(void) {
275   uint8_t sat;
276   if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
277     sat = 255;
278   } else {
279     sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
280   }
281   rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
282 }
283 void rgblight_decrease_sat(void) {
284   uint8_t sat;
285   if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
286     sat = 0;
287   } else {
288     sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
289   }
290   rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
291 }
292 void rgblight_increase_val(void) {
293   uint8_t val;
294   if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
295     val = 255;
296   } else {
297     val = rgblight_config.val + RGBLIGHT_VAL_STEP;
298   }
299   rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
300 }
301 void rgblight_decrease_val(void) {
302   uint8_t val;
303   if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
304     val = 0;
305   } else {
306     val = rgblight_config.val - RGBLIGHT_VAL_STEP;
307   }
308   rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
309 }
310
311 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
312   inmem_config.raw = rgblight_config.raw;
313   if (rgblight_config.enable) {
314     LED_TYPE tmp_led;
315     sethsv(hue, sat, val, &tmp_led);
316     inmem_config.hue = hue;
317     inmem_config.sat = sat;
318     inmem_config.val = val;
319     // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
320     rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
321   }
322 }
323 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
324   if (rgblight_config.enable) {
325     if (rgblight_config.mode == 1) {
326       // same static color
327       rgblight_sethsv_noeeprom(hue, sat, val);
328     } else {
329       // all LEDs in same color
330       if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
331         // breathing mode, ignore the change of val, use in memory value instead
332         val = rgblight_config.val;
333       } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
334         // rainbow mood and rainbow swirl, ignore the change of hue
335         hue = rgblight_config.hue;
336       } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
337         // static gradient
338         uint16_t _hue;
339         int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
340         uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
341         for (uint8_t i = 0; i < RGBLED_NUM; i++) {
342           _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
343           dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
344           sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
345         }
346         rgblight_set();
347       }
348     }
349     rgblight_config.hue = hue;
350     rgblight_config.sat = sat;
351     rgblight_config.val = val;
352     eeconfig_update_rgblight(rgblight_config.raw);
353     xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
354   }
355 }
356
357 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
358   // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
359   for (uint8_t i = 0; i < RGBLED_NUM; i++) {
360     led[i].r = r;
361     led[i].g = g;
362     led[i].b = b;
363   }
364   rgblight_set();
365 }
366
367 #ifndef RGBLIGHT_CUSTOM_DRIVER
368 void rgblight_set(void) {
369   if (rgblight_config.enable) {
370     #ifdef RGBW
371       ws2812_setleds_rgbw(led, RGBLED_NUM);
372     #else
373       ws2812_setleds(led, RGBLED_NUM);
374     #endif
375   } else {
376     for (uint8_t i = 0; i < RGBLED_NUM; i++) {
377       led[i].r = 0;
378       led[i].g = 0;
379       led[i].b = 0;
380     }
381     #ifdef RGBW
382       ws2812_setleds_rgbw(led, RGBLED_NUM);
383     #else
384       ws2812_setleds(led, RGBLED_NUM);
385     #endif
386   }
387 }
388 #endif
389
390 #ifdef RGBLIGHT_ANIMATIONS
391
392 // Animation timer -- AVR Timer3
393 void rgblight_timer_init(void) {
394   // static uint8_t rgblight_timer_is_init = 0;
395   // if (rgblight_timer_is_init) {
396   //   return;
397   // }
398   // rgblight_timer_is_init = 1;
399   // /* Timer 3 setup */
400   // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
401   //       | _BV(CS30); // Clock selelct: clk/1
402   // /* Set TOP value */
403   // uint8_t sreg = SREG;
404   // cli();
405   // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
406   // OCR3AL = RGBLED_TIMER_TOP & 0xff;
407   // SREG = sreg;
408
409   rgblight_timer_enabled = true;
410 }
411 void rgblight_timer_enable(void) {
412   rgblight_timer_enabled = true;
413   dprintf("TIMER3 enabled.\n");
414 }
415 void rgblight_timer_disable(void) {
416   rgblight_timer_enabled = false;
417   dprintf("TIMER3 disabled.\n");
418 }
419 void rgblight_timer_toggle(void) {
420   rgblight_timer_enabled ^= rgblight_timer_enabled;
421   dprintf("TIMER3 toggled.\n");
422 }
423
424 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
425   rgblight_enable();
426   rgblight_mode(1);
427   rgblight_setrgb(r, g, b);
428 }
429
430 void rgblight_task(void) {
431   if (rgblight_timer_enabled) {
432     // mode = 1, static light, do nothing here
433     if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
434       // mode = 2 to 5, breathing mode
435       rgblight_effect_breathing(rgblight_config.mode - 2);
436     } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
437       // mode = 6 to 8, rainbow mood mod
438       rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
439     } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
440       // mode = 9 to 14, rainbow swirl mode
441       rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
442     } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
443       // mode = 15 to 20, snake mode
444       rgblight_effect_snake(rgblight_config.mode - 15);
445     } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
446       // mode = 21 to 23, knight mode
447       rgblight_effect_knight(rgblight_config.mode - 21);
448     } else if (rgblight_config.mode == 24) {
449       // mode = 24, christmas mode
450       rgblight_effect_christmas();
451     }
452   }
453 }
454
455 // Effects
456 void rgblight_effect_breathing(uint8_t interval) {
457   static uint8_t pos = 0;
458   static uint16_t last_timer = 0;
459
460   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
461     return;
462   }
463   last_timer = timer_read();
464
465   rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&LED_BREATHING_TABLE[pos]));
466   pos = (pos + 1) % 256;
467 }
468 void rgblight_effect_rainbow_mood(uint8_t interval) {
469   static uint16_t current_hue = 0;
470   static uint16_t last_timer = 0;
471
472   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
473     return;
474   }
475   last_timer = timer_read();
476   rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
477   current_hue = (current_hue + 1) % 360;
478 }
479 void rgblight_effect_rainbow_swirl(uint8_t interval) {
480   static uint16_t current_hue = 0;
481   static uint16_t last_timer = 0;
482   uint16_t hue;
483   uint8_t i;
484   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
485     return;
486   }
487   last_timer = timer_read();
488   for (i = 0; i < RGBLED_NUM; i++) {
489     hue = (360 / RGBLED_NUM * i + current_hue) % 360;
490     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
491   }
492   rgblight_set();
493
494   if (interval % 2) {
495     current_hue = (current_hue + 1) % 360;
496   } else {
497     if (current_hue - 1 < 0) {
498       current_hue = 359;
499     } else {
500       current_hue = current_hue - 1;
501     }
502   }
503 }
504 void rgblight_effect_snake(uint8_t interval) {
505   static uint8_t pos = 0;
506   static uint16_t last_timer = 0;
507   uint8_t i, j;
508   int8_t k;
509   int8_t increment = 1;
510   if (interval % 2) {
511     increment = -1;
512   }
513   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
514     return;
515   }
516   last_timer = timer_read();
517   for (i = 0; i < RGBLED_NUM; i++) {
518     led[i].r = 0;
519     led[i].g = 0;
520     led[i].b = 0;
521     for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
522       k = pos + j * increment;
523       if (k < 0) {
524         k = k + RGBLED_NUM;
525       }
526       if (i == k) {
527         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]);
528       }
529     }
530   }
531   rgblight_set();
532   if (increment == 1) {
533     if (pos - 1 < 0) {
534       pos = RGBLED_NUM - 1;
535     } else {
536       pos -= 1;
537     }
538   } else {
539     pos = (pos + 1) % RGBLED_NUM;
540   }
541 }
542 void rgblight_effect_knight(uint8_t interval) {
543   static int8_t pos = 0;
544   static uint16_t last_timer = 0;
545   uint8_t i, j, cur;
546   int8_t k;
547   LED_TYPE preled[RGBLED_NUM];
548   static int8_t increment = -1;
549   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
550     return;
551   }
552   last_timer = timer_read();
553   for (i = 0; i < RGBLED_NUM; i++) {
554     preled[i].r = 0;
555     preled[i].g = 0;
556     preled[i].b = 0;
557     for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
558       k = pos + j * increment;
559       if (k < 0) {
560         k = 0;
561       }
562       if (k >= RGBLED_NUM) {
563         k = RGBLED_NUM - 1;
564       }
565       if (i == k) {
566         sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&preled[i]);
567       }
568     }
569   }
570   if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
571     for (i = 0; i < RGBLED_NUM; i++) {
572       cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
573       led[i].r = preled[cur].r;
574       led[i].g = preled[cur].g;
575       led[i].b = preled[cur].b;
576     }
577   }
578   rgblight_set();
579   if (increment == 1) {
580     if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
581       pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
582       increment = -1;
583     } else {
584       pos -= 1;
585     }
586   } else {
587     if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
588       pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
589       increment = 1;
590     } else {
591       pos += 1;
592     }
593   }
594 }
595
596
597 void rgblight_effect_christmas(void) {
598   static uint16_t current_offset = 0;
599   static uint16_t last_timer = 0;
600   uint16_t hue;
601   uint8_t i;
602   if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
603     return;
604   }
605   last_timer = timer_read();
606   current_offset = (current_offset + 1) % 2;
607   for (i = 0; i < RGBLED_NUM; i++) {
608     hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
609     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
610   }
611   rgblight_set();
612 }
613
614 #endif