]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/rgblight.c
Revert "Remove the "lib/%" rule."
[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 __attribute__ ((weak))
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
398 #ifdef RGBLIGHT_ANIMATIONS
399
400 // Animation timer -- AVR Timer3
401 void rgblight_timer_init(void) {
402   // static uint8_t rgblight_timer_is_init = 0;
403   // if (rgblight_timer_is_init) {
404   //   return;
405   // }
406   // rgblight_timer_is_init = 1;
407   // /* Timer 3 setup */
408   // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
409   //       | _BV(CS30); // Clock selelct: clk/1
410   // /* Set TOP value */
411   // uint8_t sreg = SREG;
412   // cli();
413   // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
414   // OCR3AL = RGBLED_TIMER_TOP & 0xff;
415   // SREG = sreg;
416
417   rgblight_timer_enabled = true;
418 }
419 void rgblight_timer_enable(void) {
420   rgblight_timer_enabled = true;
421   dprintf("TIMER3 enabled.\n");
422 }
423 void rgblight_timer_disable(void) {
424   rgblight_timer_enabled = false;
425   dprintf("TIMER3 disabled.\n");
426 }
427 void rgblight_timer_toggle(void) {
428   rgblight_timer_enabled ^= rgblight_timer_enabled;
429   dprintf("TIMER3 toggled.\n");
430 }
431
432 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
433   rgblight_enable();
434   rgblight_mode(1);
435   rgblight_setrgb(r, g, b);
436 }
437
438 void rgblight_task(void) {
439   if (rgblight_timer_enabled) {
440     // mode = 1, static light, do nothing here
441     if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
442       // mode = 2 to 5, breathing mode
443       rgblight_effect_breathing(rgblight_config.mode - 2);
444     } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
445       // mode = 6 to 8, rainbow mood mod
446       rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
447     } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
448       // mode = 9 to 14, rainbow swirl mode
449       rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
450     } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
451       // mode = 15 to 20, snake mode
452       rgblight_effect_snake(rgblight_config.mode - 15);
453     } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
454       // mode = 21 to 23, knight mode
455       rgblight_effect_knight(rgblight_config.mode - 21);
456     } else if (rgblight_config.mode == 24) {
457       // mode = 24, christmas mode
458       rgblight_effect_christmas();
459     }
460   }
461 }
462
463 // Effects
464 void rgblight_effect_breathing(uint8_t interval) {
465   static uint8_t pos = 0;
466   static uint16_t last_timer = 0;
467
468   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
469     return;
470   }
471   last_timer = timer_read();
472
473   rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&LED_BREATHING_TABLE[pos]));
474   pos = (pos + 1) % 256;
475 }
476 void rgblight_effect_rainbow_mood(uint8_t interval) {
477   static uint16_t current_hue = 0;
478   static uint16_t last_timer = 0;
479
480   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
481     return;
482   }
483   last_timer = timer_read();
484   rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
485   current_hue = (current_hue + 1) % 360;
486 }
487 void rgblight_effect_rainbow_swirl(uint8_t interval) {
488   static uint16_t current_hue = 0;
489   static uint16_t last_timer = 0;
490   uint16_t hue;
491   uint8_t i;
492   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
493     return;
494   }
495   last_timer = timer_read();
496   for (i = 0; i < RGBLED_NUM; i++) {
497     hue = (360 / RGBLED_NUM * i + current_hue) % 360;
498     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
499   }
500   rgblight_set();
501
502   if (interval % 2) {
503     current_hue = (current_hue + 1) % 360;
504   } else {
505     if (current_hue - 1 < 0) {
506       current_hue = 359;
507     } else {
508       current_hue = current_hue - 1;
509     }
510   }
511 }
512 void rgblight_effect_snake(uint8_t interval) {
513   static uint8_t pos = 0;
514   static uint16_t last_timer = 0;
515   uint8_t i, j;
516   int8_t k;
517   int8_t increment = 1;
518   if (interval % 2) {
519     increment = -1;
520   }
521   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
522     return;
523   }
524   last_timer = timer_read();
525   for (i = 0; i < RGBLED_NUM; i++) {
526     led[i].r = 0;
527     led[i].g = 0;
528     led[i].b = 0;
529     for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
530       k = pos + j * increment;
531       if (k < 0) {
532         k = k + RGBLED_NUM;
533       }
534       if (i == k) {
535         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]);
536       }
537     }
538   }
539   rgblight_set();
540   if (increment == 1) {
541     if (pos - 1 < 0) {
542       pos = RGBLED_NUM - 1;
543     } else {
544       pos -= 1;
545     }
546   } else {
547     pos = (pos + 1) % RGBLED_NUM;
548   }
549 }
550 void rgblight_effect_knight(uint8_t interval) {
551   static uint16_t last_timer = 0;
552   if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
553     return;
554   }
555   last_timer = timer_read();
556
557   static int8_t low_bound = 0;
558   static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
559   static int8_t increment = 1;
560   uint8_t i, cur;
561
562   // Set all the LEDs to 0
563   for (i = 0; i < RGBLED_NUM; i++) {
564     led[i].r = 0;
565     led[i].g = 0;
566     led[i].b = 0;
567   }
568   // Determine which LEDs should be lit up
569   for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
570     cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
571
572     if (i >= low_bound && i <= high_bound) {
573       sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
574     } else {
575       led[cur].r = 0;
576       led[cur].g = 0;
577       led[cur].b = 0;
578     }
579   }
580   rgblight_set();
581
582   // Move from low_bound to high_bound changing the direction we increment each
583   // time a boundary is hit.
584   low_bound += increment;
585   high_bound += increment;
586
587   if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
588     increment = -increment;
589   }
590 }
591
592
593 void rgblight_effect_christmas(void) {
594   static uint16_t current_offset = 0;
595   static uint16_t last_timer = 0;
596   uint16_t hue;
597   uint8_t i;
598   if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
599     return;
600   }
601   last_timer = timer_read();
602   current_offset = (current_offset + 1) % 2;
603   for (i = 0; i < RGBLED_NUM; i++) {
604     hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
605     sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
606   }
607   rgblight_set();
608 }
609
610 #endif