1 /* Copyright 2016-2017 Yang Liu
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.
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.
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/>.
17 #include <avr/eeprom.h>
18 #include <avr/interrupt.h>
19 #include <util/delay.h>
24 #include "led_tables.h"
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 = {127, 63, 31};
36 __attribute__ ((weak))
37 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
39 rgblight_config_t rgblight_config;
40 rgblight_config_t inmem_config;
42 LED_TYPE led[RGBLED_NUM];
43 uint8_t rgblight_inited = 0;
44 bool rgblight_timer_enabled = false;
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;
49 #ifdef RGBLIGHT_LIMIT_VAL
50 if (val > RGBLIGHT_LIMIT_VAL) {
51 val=RGBLIGHT_LIMIT_VAL; // limit the val
55 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
60 base = ((255 - sat) * val) >> 8;
61 color = (val - base) * (hue % 60) / 60;
96 r = pgm_read_byte(&CIE1931_CURVE[r]);
97 g = pgm_read_byte(&CIE1931_CURVE[g]);
98 b = pgm_read_byte(&CIE1931_CURVE[b]);
100 setrgb(r, g, b, led1);
103 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
110 uint32_t eeconfig_read_rgblight(void) {
111 return eeprom_read_dword(EECONFIG_RGBLIGHT);
113 void eeconfig_update_rgblight(uint32_t val) {
114 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
116 void eeconfig_update_rgblight_default(void) {
117 dprintf("eeconfig_update_rgblight_default\n");
118 rgblight_config.enable = 1;
119 rgblight_config.mode = 1;
120 rgblight_config.hue = 0;
121 rgblight_config.sat = 255;
122 rgblight_config.val = 255;
123 eeconfig_update_rgblight(rgblight_config.raw);
125 void eeconfig_debug_rgblight(void) {
126 dprintf("rgblight_config eprom\n");
127 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
128 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
129 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
130 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
131 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
134 void rgblight_init(void) {
135 debug_enable = 1; // Debug ON!
136 dprintf("rgblight_init called.\n");
138 dprintf("rgblight_init start!\n");
139 if (!eeconfig_is_enabled()) {
140 dprintf("rgblight_init eeconfig is not enabled.\n");
142 eeconfig_update_rgblight_default();
144 rgblight_config.raw = eeconfig_read_rgblight();
145 if (!rgblight_config.mode) {
146 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
147 eeconfig_update_rgblight_default();
148 rgblight_config.raw = eeconfig_read_rgblight();
150 eeconfig_debug_rgblight(); // display current eeprom values
152 #ifdef RGBLIGHT_ANIMATIONS
153 rgblight_timer_init(); // setup the timer
156 if (rgblight_config.enable) {
157 rgblight_mode(rgblight_config.mode);
161 void rgblight_update_dword(uint32_t dword) {
162 rgblight_config.raw = dword;
163 eeconfig_update_rgblight(rgblight_config.raw);
164 if (rgblight_config.enable)
165 rgblight_mode(rgblight_config.mode);
167 #ifdef RGBLIGHT_ANIMATIONS
168 rgblight_timer_disable();
174 void rgblight_increase(void) {
176 if (rgblight_config.mode < RGBLIGHT_MODES) {
177 mode = rgblight_config.mode + 1;
181 void rgblight_decrease(void) {
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;
189 void rgblight_step(void) {
191 mode = rgblight_config.mode + 1;
192 if (mode > RGBLIGHT_MODES) {
197 void rgblight_step_reverse(void) {
199 mode = rgblight_config.mode - 1;
201 mode = RGBLIGHT_MODES;
206 uint32_t rgblight_get_mode(void) {
207 if (!rgblight_config.enable) {
211 return rgblight_config.mode;
214 void rgblight_mode(uint8_t mode) {
215 if (!rgblight_config.enable) {
219 rgblight_config.mode = 1;
220 } else if (mode > RGBLIGHT_MODES) {
221 rgblight_config.mode = RGBLIGHT_MODES;
223 rgblight_config.mode = mode;
225 eeconfig_update_rgblight(rgblight_config.raw);
226 xprintf("rgblight mode: %u\n", rgblight_config.mode);
227 if (rgblight_config.mode == 1) {
228 #ifdef RGBLIGHT_ANIMATIONS
229 rgblight_timer_disable();
231 } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) {
232 // MODE 2-5, breathing
233 // MODE 6-8, rainbow mood
234 // MODE 9-14, rainbow swirl
236 // MODE 21-23, knight
238 // MODE 25-34, static rainbow
240 #ifdef RGBLIGHT_ANIMATIONS
241 rgblight_timer_enable();
243 } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
244 // MODE 25-34, static gradient
246 #ifdef RGBLIGHT_ANIMATIONS
247 rgblight_timer_disable();
250 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
253 void rgblight_toggle(void) {
254 xprintf("rgblight toggle: rgblight_config.enable = %u\n", !rgblight_config.enable);
255 if (rgblight_config.enable) {
263 void rgblight_enable(void) {
264 rgblight_config.enable = 1;
265 eeconfig_update_rgblight(rgblight_config.raw);
266 xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable);
267 rgblight_mode(rgblight_config.mode);
270 void rgblight_disable(void) {
271 rgblight_config.enable = 0;
272 eeconfig_update_rgblight(rgblight_config.raw);
273 xprintf("rgblight disable: rgblight_config.enable = %u\n", rgblight_config.enable);
274 #ifdef RGBLIGHT_ANIMATIONS
275 rgblight_timer_disable();
282 void rgblight_increase_hue(void) {
284 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
285 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
287 void rgblight_decrease_hue(void) {
289 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
290 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
292 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
294 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
296 void rgblight_increase_sat(void) {
298 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
301 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
303 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
305 void rgblight_decrease_sat(void) {
307 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
310 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
312 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
314 void rgblight_increase_val(void) {
316 if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
319 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
321 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
323 void rgblight_decrease_val(void) {
325 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
328 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
330 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
333 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
334 inmem_config.raw = rgblight_config.raw;
335 if (rgblight_config.enable) {
337 sethsv(hue, sat, val, &tmp_led);
338 inmem_config.hue = hue;
339 inmem_config.sat = sat;
340 inmem_config.val = val;
341 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
342 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
345 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
346 if (rgblight_config.enable) {
347 if (rgblight_config.mode == 1) {
349 rgblight_sethsv_noeeprom(hue, sat, val);
351 // all LEDs in same color
352 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
353 // breathing mode, ignore the change of val, use in memory value instead
354 val = rgblight_config.val;
355 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
356 // rainbow mood and rainbow swirl, ignore the change of hue
357 hue = rgblight_config.hue;
358 } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
361 int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
362 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
363 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
364 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
365 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
366 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
371 rgblight_config.hue = hue;
372 rgblight_config.sat = sat;
373 rgblight_config.val = val;
374 eeconfig_update_rgblight(rgblight_config.raw);
375 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
379 uint16_t rgblight_get_hue(void) {
380 return rgblight_config.hue;
383 uint8_t rgblight_get_sat(void) {
384 return rgblight_config.sat;
387 uint8_t rgblight_get_val(void) {
388 return rgblight_config.val;
391 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
392 if (!rgblight_config.enable) { return; }
394 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
402 void rgblight_setrgb_at(uint8_t r, uint8_t g, uint8_t b, uint8_t index) {
403 if (!rgblight_config.enable || index >= RGBLED_NUM) { return; }
411 void rgblight_sethsv_at(uint16_t hue, uint8_t sat, uint8_t val, uint8_t index) {
412 if (!rgblight_config.enable) { return; }
415 sethsv(hue, sat, val, &tmp_led);
416 rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index);
419 #ifndef RGBLIGHT_CUSTOM_DRIVER
420 void rgblight_set(void) {
421 if (rgblight_config.enable) {
423 ws2812_setleds_rgbw(led, RGBLED_NUM);
425 ws2812_setleds(led, RGBLED_NUM);
428 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
434 ws2812_setleds_rgbw(led, RGBLED_NUM);
436 ws2812_setleds(led, RGBLED_NUM);
442 #ifdef RGBLIGHT_ANIMATIONS
444 // Animation timer -- AVR Timer3
445 void rgblight_timer_init(void) {
446 // static uint8_t rgblight_timer_is_init = 0;
447 // if (rgblight_timer_is_init) {
450 // rgblight_timer_is_init = 1;
451 // /* Timer 3 setup */
452 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
453 // | _BV(CS30); // Clock selelct: clk/1
454 // /* Set TOP value */
455 // uint8_t sreg = SREG;
457 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
458 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
461 rgblight_timer_enabled = true;
463 void rgblight_timer_enable(void) {
464 rgblight_timer_enabled = true;
465 dprintf("TIMER3 enabled.\n");
467 void rgblight_timer_disable(void) {
468 rgblight_timer_enabled = false;
469 dprintf("TIMER3 disabled.\n");
471 void rgblight_timer_toggle(void) {
472 rgblight_timer_enabled ^= rgblight_timer_enabled;
473 dprintf("TIMER3 toggled.\n");
476 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
479 rgblight_setrgb(r, g, b);
482 void rgblight_task(void) {
483 if (rgblight_timer_enabled) {
484 // mode = 1, static light, do nothing here
485 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
486 // mode = 2 to 5, breathing mode
487 rgblight_effect_breathing(rgblight_config.mode - 2);
488 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
489 // mode = 6 to 8, rainbow mood mod
490 rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
491 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
492 // mode = 9 to 14, rainbow swirl mode
493 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
494 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
495 // mode = 15 to 20, snake mode
496 rgblight_effect_snake(rgblight_config.mode - 15);
497 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
498 // mode = 21 to 23, knight mode
499 rgblight_effect_knight(rgblight_config.mode - 21);
500 } else if (rgblight_config.mode == 24) {
501 // mode = 24, christmas mode
502 rgblight_effect_christmas();
508 void rgblight_effect_breathing(uint8_t interval) {
509 static uint8_t pos = 0;
510 static uint16_t last_timer = 0;
513 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
516 last_timer = timer_read();
519 // http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/
520 val = (exp(sin((pos/255.0)*M_PI)) - RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)*(RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E));
521 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, val);
522 pos = (pos + 1) % 256;
524 void rgblight_effect_rainbow_mood(uint8_t interval) {
525 static uint16_t current_hue = 0;
526 static uint16_t last_timer = 0;
528 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
531 last_timer = timer_read();
532 rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
533 current_hue = (current_hue + 1) % 360;
535 void rgblight_effect_rainbow_swirl(uint8_t interval) {
536 static uint16_t current_hue = 0;
537 static uint16_t last_timer = 0;
540 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
543 last_timer = timer_read();
544 for (i = 0; i < RGBLED_NUM; i++) {
545 hue = (360 / RGBLED_NUM * i + current_hue) % 360;
546 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
551 current_hue = (current_hue + 1) % 360;
553 if (current_hue - 1 < 0) {
556 current_hue = current_hue - 1;
560 void rgblight_effect_snake(uint8_t interval) {
561 static uint8_t pos = 0;
562 static uint16_t last_timer = 0;
565 int8_t increment = 1;
569 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
572 last_timer = timer_read();
573 for (i = 0; i < RGBLED_NUM; i++) {
577 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
578 k = pos + j * increment;
583 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]);
588 if (increment == 1) {
590 pos = RGBLED_NUM - 1;
595 pos = (pos + 1) % RGBLED_NUM;
598 void rgblight_effect_knight(uint8_t interval) {
599 static uint16_t last_timer = 0;
600 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
603 last_timer = timer_read();
605 static int8_t low_bound = 0;
606 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
607 static int8_t increment = 1;
610 // Set all the LEDs to 0
611 for (i = 0; i < RGBLED_NUM; i++) {
616 // Determine which LEDs should be lit up
617 for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) {
618 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
620 if (i >= low_bound && i <= high_bound) {
621 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
630 // Move from low_bound to high_bound changing the direction we increment each
631 // time a boundary is hit.
632 low_bound += increment;
633 high_bound += increment;
635 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
636 increment = -increment;
641 void rgblight_effect_christmas(void) {
642 static uint16_t current_offset = 0;
643 static uint16_t last_timer = 0;
646 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
649 last_timer = timer_read();
650 current_offset = (current_offset + 1) % 2;
651 for (i = 0; i < RGBLED_NUM; i++) {
652 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
653 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);