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