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/>.
16 #include <avr/eeprom.h>
17 #include <avr/interrupt.h>
18 #include <util/delay.h>
23 #include "led_tables.h"
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};
38 rgblight_config_t rgblight_config;
39 rgblight_config_t inmem_config;
41 LED_TYPE led[RGBLED_NUM];
42 uint8_t rgblight_inited = 0;
43 bool rgblight_timer_enabled = false;
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;
48 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
53 base = ((255 - sat) * val) >> 8;
54 color = (val - base) * (hue % 60) / 60;
89 r = pgm_read_byte(&CIE1931_CURVE[r]);
90 g = pgm_read_byte(&CIE1931_CURVE[g]);
91 b = pgm_read_byte(&CIE1931_CURVE[b]);
93 setrgb(r, g, b, led1);
96 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
103 uint32_t eeconfig_read_rgblight(void) {
104 return eeprom_read_dword(EECONFIG_RGBLIGHT);
106 void eeconfig_update_rgblight(uint32_t val) {
107 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
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);
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);
127 void rgblight_init(void) {
128 debug_enable = 1; // Debug ON!
129 dprintf("rgblight_init called.\n");
131 dprintf("rgblight_init start!\n");
132 if (!eeconfig_is_enabled()) {
133 dprintf("rgblight_init eeconfig is not enabled.\n");
135 eeconfig_update_rgblight_default();
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();
143 eeconfig_debug_rgblight(); // display current eeprom values
145 #ifdef RGBLIGHT_ANIMATIONS
146 rgblight_timer_init(); // setup the timer
149 if (rgblight_config.enable) {
150 rgblight_mode(rgblight_config.mode);
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);
160 #ifdef RGBLIGHT_ANIMATIONS
161 rgblight_timer_disable();
167 void rgblight_increase(void) {
169 if (rgblight_config.mode < RGBLIGHT_MODES) {
170 mode = rgblight_config.mode + 1;
174 void rgblight_decrease(void) {
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;
182 void rgblight_step(void) {
184 mode = rgblight_config.mode + 1;
185 if (mode > RGBLIGHT_MODES) {
190 void rgblight_step_reverse(void) {
192 mode = rgblight_config.mode - 1;
194 mode = RGBLIGHT_MODES;
199 uint32_t rgblight_get_mode(void) {
200 if (!rgblight_config.enable) {
204 return rgblight_config.mode;
207 void rgblight_mode(uint8_t mode) {
208 if (!rgblight_config.enable) {
212 rgblight_config.mode = 1;
213 } else if (mode > RGBLIGHT_MODES) {
214 rgblight_config.mode = RGBLIGHT_MODES;
216 rgblight_config.mode = mode;
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();
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
229 // MODE 21-23, knight
231 // MODE 25-34, static rainbow
233 #ifdef RGBLIGHT_ANIMATIONS
234 rgblight_timer_enable();
236 } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
237 // MODE 25-34, static gradient
239 #ifdef RGBLIGHT_ANIMATIONS
240 rgblight_timer_disable();
243 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
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);
253 #ifdef RGBLIGHT_ANIMATIONS
254 rgblight_timer_disable();
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);
269 void rgblight_increase_hue(void) {
271 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
272 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
274 void rgblight_decrease_hue(void) {
276 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
277 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
279 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
281 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
283 void rgblight_increase_sat(void) {
285 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
288 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
290 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
292 void rgblight_decrease_sat(void) {
294 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
297 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
299 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
301 void rgblight_increase_val(void) {
303 if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
306 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
308 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
310 void rgblight_decrease_val(void) {
312 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
315 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
317 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
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) {
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);
332 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
333 if (rgblight_config.enable) {
334 if (rgblight_config.mode == 1) {
336 rgblight_sethsv_noeeprom(hue, sat, val);
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) {
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]);
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);
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++) {
376 #ifndef RGBLIGHT_CUSTOM_DRIVER
377 void rgblight_set(void) {
378 if (rgblight_config.enable) {
380 ws2812_setleds_rgbw(led, RGBLED_NUM);
382 ws2812_setleds(led, RGBLED_NUM);
385 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
391 ws2812_setleds_rgbw(led, RGBLED_NUM);
393 ws2812_setleds(led, RGBLED_NUM);
399 #ifdef RGBLIGHT_ANIMATIONS
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) {
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;
414 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
415 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
418 rgblight_timer_enabled = true;
420 void rgblight_timer_enable(void) {
421 rgblight_timer_enabled = true;
422 dprintf("TIMER3 enabled.\n");
424 void rgblight_timer_disable(void) {
425 rgblight_timer_enabled = false;
426 dprintf("TIMER3 disabled.\n");
428 void rgblight_timer_toggle(void) {
429 rgblight_timer_enabled ^= rgblight_timer_enabled;
430 dprintf("TIMER3 toggled.\n");
433 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
436 rgblight_setrgb(r, g, b);
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();
465 void rgblight_effect_breathing(uint8_t interval) {
466 static uint8_t pos = 0;
467 static uint16_t last_timer = 0;
469 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
472 last_timer = timer_read();
474 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&LED_BREATHING_TABLE[pos]));
475 pos = (pos + 1) % 256;
477 void rgblight_effect_rainbow_mood(uint8_t interval) {
478 static uint16_t current_hue = 0;
479 static uint16_t last_timer = 0;
481 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
484 last_timer = timer_read();
485 rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
486 current_hue = (current_hue + 1) % 360;
488 void rgblight_effect_rainbow_swirl(uint8_t interval) {
489 static uint16_t current_hue = 0;
490 static uint16_t last_timer = 0;
493 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_SWIRL_INTERVALS[interval / 2])) {
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]);
504 current_hue = (current_hue + 1) % 360;
506 if (current_hue - 1 < 0) {
509 current_hue = current_hue - 1;
513 void rgblight_effect_snake(uint8_t interval) {
514 static uint8_t pos = 0;
515 static uint16_t last_timer = 0;
518 int8_t increment = 1;
522 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
525 last_timer = timer_read();
526 for (i = 0; i < RGBLED_NUM; i++) {
530 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
531 k = pos + j * increment;
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]);
541 if (increment == 1) {
543 pos = RGBLED_NUM - 1;
548 pos = (pos + 1) % RGBLED_NUM;
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])) {
556 last_timer = timer_read();
558 static int8_t low_bound = 0;
559 static int8_t high_bound = RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
560 static int8_t increment = 1;
563 // Set all the LEDs to 0
564 for (i = 0; i < RGBLED_NUM; i++) {
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;
573 if (i >= low_bound && i <= high_bound) {
574 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]);
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;
588 if (high_bound <= 0 || low_bound >= RGBLIGHT_EFFECT_KNIGHT_LED_NUM - 1) {
589 increment = -increment;
594 void rgblight_effect_christmas(void) {
595 static uint16_t current_offset = 0;
596 static uint16_t last_timer = 0;
599 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
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]);