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