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>
24 // Lightness curve using the CIE 1931 lightness formula
25 //Generated by the python script provided in http://jared.geek.nz/2013/feb/linear-led-pwm
26 const uint8_t DIM_CURVE[] PROGMEM = {
27 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
28 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
29 2, 2, 2, 3, 3, 3, 3, 3, 3, 3,
30 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
31 5, 5, 6, 6, 6, 6, 6, 7, 7, 7,
32 7, 8, 8, 8, 8, 9, 9, 9, 10, 10,
33 10, 10, 11, 11, 11, 12, 12, 12, 13, 13,
34 13, 14, 14, 15, 15, 15, 16, 16, 17, 17,
35 17, 18, 18, 19, 19, 20, 20, 21, 21, 22,
36 22, 23, 23, 24, 24, 25, 25, 26, 26, 27,
37 28, 28, 29, 29, 30, 31, 31, 32, 32, 33,
38 34, 34, 35, 36, 37, 37, 38, 39, 39, 40,
39 41, 42, 43, 43, 44, 45, 46, 47, 47, 48,
40 49, 50, 51, 52, 53, 54, 54, 55, 56, 57,
41 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
42 68, 70, 71, 72, 73, 74, 75, 76, 77, 79,
43 80, 81, 82, 83, 85, 86, 87, 88, 90, 91,
44 92, 94, 95, 96, 98, 99, 100, 102, 103, 105,
45 106, 108, 109, 110, 112, 113, 115, 116, 118, 120,
46 121, 123, 124, 126, 128, 129, 131, 132, 134, 136,
47 138, 139, 141, 143, 145, 146, 148, 150, 152, 154,
48 155, 157, 159, 161, 163, 165, 167, 169, 171, 173,
49 175, 177, 179, 181, 183, 185, 187, 189, 191, 193,
50 196, 198, 200, 202, 204, 207, 209, 211, 214, 216,
51 218, 220, 223, 225, 228, 230, 232, 235, 237, 240,
52 242, 245, 247, 250, 252, 255,
55 const uint8_t RGBLED_BREATHING_TABLE[] PROGMEM = {
56 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9,
57 10, 11, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35,
58 37, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
59 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, 124,
60 127, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173,
61 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215,
62 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, 241, 243, 244,
63 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255,
64 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250, 250, 249, 248, 246,
65 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222, 220,
66 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179,
67 176, 173, 170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131,
68 128, 124, 121, 118, 115, 112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82,
69 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40,
70 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11,
71 10, 9, 7, 6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0
74 __attribute__ ((weak))
75 const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
76 __attribute__ ((weak))
77 const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
78 __attribute__ ((weak))
79 const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
80 __attribute__ ((weak))
81 const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
82 __attribute__ ((weak))
83 const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {100, 50, 20};
84 __attribute__ ((weak))
85 const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90};
87 rgblight_config_t rgblight_config;
88 rgblight_config_t inmem_config;
90 LED_TYPE led[RGBLED_NUM];
91 uint8_t rgblight_inited = 0;
92 bool rgblight_timer_enabled = false;
94 void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
95 uint8_t r = 0, g = 0, b = 0, base, color;
97 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
102 base = ((255 - sat) * val) >> 8;
103 color = (val - base) * (hue % 60) / 60;
138 r = pgm_read_byte(&DIM_CURVE[r]);
139 g = pgm_read_byte(&DIM_CURVE[g]);
140 b = pgm_read_byte(&DIM_CURVE[b]);
142 setrgb(r, g, b, led1);
145 void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
152 uint32_t eeconfig_read_rgblight(void) {
153 return eeprom_read_dword(EECONFIG_RGBLIGHT);
155 void eeconfig_update_rgblight(uint32_t val) {
156 eeprom_update_dword(EECONFIG_RGBLIGHT, val);
158 void eeconfig_update_rgblight_default(void) {
159 dprintf("eeconfig_update_rgblight_default\n");
160 rgblight_config.enable = 1;
161 rgblight_config.mode = 1;
162 rgblight_config.hue = 0;
163 rgblight_config.sat = 255;
164 rgblight_config.val = 255;
165 eeconfig_update_rgblight(rgblight_config.raw);
167 void eeconfig_debug_rgblight(void) {
168 dprintf("rgblight_config eprom\n");
169 dprintf("rgblight_config.enable = %d\n", rgblight_config.enable);
170 dprintf("rghlight_config.mode = %d\n", rgblight_config.mode);
171 dprintf("rgblight_config.hue = %d\n", rgblight_config.hue);
172 dprintf("rgblight_config.sat = %d\n", rgblight_config.sat);
173 dprintf("rgblight_config.val = %d\n", rgblight_config.val);
176 void rgblight_init(void) {
177 debug_enable = 1; // Debug ON!
178 dprintf("rgblight_init called.\n");
180 dprintf("rgblight_init start!\n");
181 if (!eeconfig_is_enabled()) {
182 dprintf("rgblight_init eeconfig is not enabled.\n");
184 eeconfig_update_rgblight_default();
186 rgblight_config.raw = eeconfig_read_rgblight();
187 if (!rgblight_config.mode) {
188 dprintf("rgblight_init rgblight_config.mode = 0. Write default values to EEPROM.\n");
189 eeconfig_update_rgblight_default();
190 rgblight_config.raw = eeconfig_read_rgblight();
192 eeconfig_debug_rgblight(); // display current eeprom values
194 #ifdef RGBLIGHT_ANIMATIONS
195 rgblight_timer_init(); // setup the timer
198 if (rgblight_config.enable) {
199 rgblight_mode(rgblight_config.mode);
203 void rgblight_update_dword(uint32_t dword) {
204 rgblight_config.raw = dword;
205 eeconfig_update_rgblight(rgblight_config.raw);
206 if (rgblight_config.enable)
207 rgblight_mode(rgblight_config.mode);
209 #ifdef RGBLIGHT_ANIMATIONS
210 rgblight_timer_disable();
216 void rgblight_increase(void) {
218 if (rgblight_config.mode < RGBLIGHT_MODES) {
219 mode = rgblight_config.mode + 1;
223 void rgblight_decrease(void) {
225 // Mode will never be < 1. If it ever is, eeprom needs to be initialized.
226 if (rgblight_config.mode > 1) {
227 mode = rgblight_config.mode - 1;
231 void rgblight_step(void) {
233 mode = rgblight_config.mode + 1;
234 if (mode > RGBLIGHT_MODES) {
239 void rgblight_step_reverse(void) {
241 mode = rgblight_config.mode - 1;
243 mode = RGBLIGHT_MODES;
248 void rgblight_mode(uint8_t mode) {
249 if (!rgblight_config.enable) {
253 rgblight_config.mode = 1;
254 } else if (mode > RGBLIGHT_MODES) {
255 rgblight_config.mode = RGBLIGHT_MODES;
257 rgblight_config.mode = mode;
259 eeconfig_update_rgblight(rgblight_config.raw);
260 xprintf("rgblight mode: %u\n", rgblight_config.mode);
261 if (rgblight_config.mode == 1) {
262 #ifdef RGBLIGHT_ANIMATIONS
263 rgblight_timer_disable();
265 } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) {
266 // MODE 2-5, breathing
267 // MODE 6-8, rainbow mood
268 // MODE 9-14, rainbow swirl
270 // MODE 21-23, knight
272 #ifdef RGBLIGHT_ANIMATIONS
273 rgblight_timer_enable();
275 } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
276 // MODE 25-34, static gradient
278 #ifdef RGBLIGHT_ANIMATIONS
279 rgblight_timer_disable();
282 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
285 void rgblight_toggle(void) {
286 rgblight_config.enable ^= 1;
287 eeconfig_update_rgblight(rgblight_config.raw);
288 xprintf("rgblight toggle: rgblight_config.enable = %u\n", rgblight_config.enable);
289 if (rgblight_config.enable) {
290 rgblight_mode(rgblight_config.mode);
292 #ifdef RGBLIGHT_ANIMATIONS
293 rgblight_timer_disable();
300 void rgblight_enable(void) {
301 rgblight_config.enable = 1;
302 eeconfig_update_rgblight(rgblight_config.raw);
303 xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable);
304 rgblight_mode(rgblight_config.mode);
308 void rgblight_increase_hue(void) {
310 hue = (rgblight_config.hue+RGBLIGHT_HUE_STEP) % 360;
311 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
313 void rgblight_decrease_hue(void) {
315 if (rgblight_config.hue-RGBLIGHT_HUE_STEP < 0) {
316 hue = (rgblight_config.hue + 360 - RGBLIGHT_HUE_STEP) % 360;
318 hue = (rgblight_config.hue - RGBLIGHT_HUE_STEP) % 360;
320 rgblight_sethsv(hue, rgblight_config.sat, rgblight_config.val);
322 void rgblight_increase_sat(void) {
324 if (rgblight_config.sat + RGBLIGHT_SAT_STEP > 255) {
327 sat = rgblight_config.sat + RGBLIGHT_SAT_STEP;
329 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
331 void rgblight_decrease_sat(void) {
333 if (rgblight_config.sat - RGBLIGHT_SAT_STEP < 0) {
336 sat = rgblight_config.sat - RGBLIGHT_SAT_STEP;
338 rgblight_sethsv(rgblight_config.hue, sat, rgblight_config.val);
340 void rgblight_increase_val(void) {
342 if (rgblight_config.val + RGBLIGHT_VAL_STEP > 255) {
345 val = rgblight_config.val + RGBLIGHT_VAL_STEP;
347 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
349 void rgblight_decrease_val(void) {
351 if (rgblight_config.val - RGBLIGHT_VAL_STEP < 0) {
354 val = rgblight_config.val - RGBLIGHT_VAL_STEP;
356 rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, val);
359 void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
360 inmem_config.raw = rgblight_config.raw;
361 if (rgblight_config.enable) {
363 sethsv(hue, sat, val, &tmp_led);
364 inmem_config.hue = hue;
365 inmem_config.sat = sat;
366 inmem_config.val = val;
367 // dprintf("rgblight set hue [MEMORY]: %u,%u,%u\n", inmem_config.hue, inmem_config.sat, inmem_config.val);
368 rgblight_setrgb(tmp_led.r, tmp_led.g, tmp_led.b);
371 void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) {
372 if (rgblight_config.enable) {
373 if (rgblight_config.mode == 1) {
375 rgblight_sethsv_noeeprom(hue, sat, val);
377 // all LEDs in same color
378 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
379 // breathing mode, ignore the change of val, use in memory value instead
380 val = rgblight_config.val;
381 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) {
382 // rainbow mood and rainbow swirl, ignore the change of hue
383 hue = rgblight_config.hue;
384 } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) {
387 int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1;
388 uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]);
389 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
390 _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360;
391 dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range);
392 sethsv(_hue, sat, val, (LED_TYPE *)&led[i]);
397 rgblight_config.hue = hue;
398 rgblight_config.sat = sat;
399 rgblight_config.val = val;
400 eeconfig_update_rgblight(rgblight_config.raw);
401 xprintf("rgblight set hsv [EEPROM]: %u,%u,%u\n", rgblight_config.hue, rgblight_config.sat, rgblight_config.val);
405 void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
406 // dprintf("rgblight set rgb: %u,%u,%u\n", r,g,b);
407 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
415 __attribute__ ((weak))
416 void rgblight_set(void) {
417 if (rgblight_config.enable) {
419 ws2812_setleds_rgbw(led, RGBLED_NUM);
421 ws2812_setleds(led, RGBLED_NUM);
424 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
430 ws2812_setleds_rgbw(led, RGBLED_NUM);
432 ws2812_setleds(led, RGBLED_NUM);
437 #ifdef RGBLIGHT_ANIMATIONS
439 // Animation timer -- AVR Timer3
440 void rgblight_timer_init(void) {
441 // static uint8_t rgblight_timer_is_init = 0;
442 // if (rgblight_timer_is_init) {
445 // rgblight_timer_is_init = 1;
446 // /* Timer 3 setup */
447 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
448 // | _BV(CS30); // Clock selelct: clk/1
449 // /* Set TOP value */
450 // uint8_t sreg = SREG;
452 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
453 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
456 rgblight_timer_enabled = true;
458 void rgblight_timer_enable(void) {
459 rgblight_timer_enabled = true;
460 dprintf("TIMER3 enabled.\n");
462 void rgblight_timer_disable(void) {
463 rgblight_timer_enabled = false;
464 dprintf("TIMER3 disabled.\n");
466 void rgblight_timer_toggle(void) {
467 rgblight_timer_enabled ^= rgblight_timer_enabled;
468 dprintf("TIMER3 toggled.\n");
471 void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) {
474 rgblight_setrgb(r, g, b);
477 void rgblight_task(void) {
478 if (rgblight_timer_enabled) {
479 // mode = 1, static light, do nothing here
480 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
481 // mode = 2 to 5, breathing mode
482 rgblight_effect_breathing(rgblight_config.mode - 2);
483 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
484 // mode = 6 to 8, rainbow mood mod
485 rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
486 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
487 // mode = 9 to 14, rainbow swirl mode
488 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
489 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
490 // mode = 15 to 20, snake mode
491 rgblight_effect_snake(rgblight_config.mode - 15);
492 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
493 // mode = 21 to 23, knight mode
494 rgblight_effect_knight(rgblight_config.mode - 21);
495 } else if (rgblight_config.mode == 24) {
496 // mode = 24, christmas mode
497 rgblight_effect_christmas();
503 void rgblight_effect_breathing(uint8_t interval) {
504 static uint8_t pos = 0;
505 static uint16_t last_timer = 0;
507 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) {
510 last_timer = timer_read();
512 rgblight_sethsv_noeeprom(rgblight_config.hue, rgblight_config.sat, pgm_read_byte(&RGBLED_BREATHING_TABLE[pos]));
513 pos = (pos + 1) % 256;
515 void rgblight_effect_rainbow_mood(uint8_t interval) {
516 static uint16_t current_hue = 0;
517 static uint16_t last_timer = 0;
519 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) {
522 last_timer = timer_read();
523 rgblight_sethsv_noeeprom(current_hue, rgblight_config.sat, rgblight_config.val);
524 current_hue = (current_hue + 1) % 360;
526 void rgblight_effect_rainbow_swirl(uint8_t interval) {
527 static uint16_t current_hue = 0;
528 static uint16_t last_timer = 0;
531 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval / 2])) {
534 last_timer = timer_read();
535 for (i = 0; i < RGBLED_NUM; i++) {
536 hue = (360 / RGBLED_NUM * i + current_hue) % 360;
537 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
542 current_hue = (current_hue + 1) % 360;
544 if (current_hue - 1 < 0) {
547 current_hue = current_hue - 1;
551 void rgblight_effect_snake(uint8_t interval) {
552 static uint8_t pos = 0;
553 static uint16_t last_timer = 0;
556 int8_t increment = 1;
560 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) {
563 last_timer = timer_read();
564 for (i = 0; i < RGBLED_NUM; i++) {
568 for (j = 0; j < RGBLIGHT_EFFECT_SNAKE_LENGTH; j++) {
569 k = pos + j * increment;
574 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]);
579 if (increment == 1) {
581 pos = RGBLED_NUM - 1;
586 pos = (pos + 1) % RGBLED_NUM;
589 void rgblight_effect_knight(uint8_t interval) {
590 static int8_t pos = 0;
591 static uint16_t last_timer = 0;
594 LED_TYPE preled[RGBLED_NUM];
595 static int8_t increment = -1;
596 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
599 last_timer = timer_read();
600 for (i = 0; i < RGBLED_NUM; i++) {
604 for (j = 0; j < RGBLIGHT_EFFECT_KNIGHT_LENGTH; j++) {
605 k = pos + j * increment;
609 if (k >= RGBLED_NUM) {
613 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&preled[i]);
617 if (RGBLIGHT_EFFECT_KNIGHT_OFFSET) {
618 for (i = 0; i < RGBLED_NUM; i++) {
619 cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % RGBLED_NUM;
620 led[i].r = preled[cur].r;
621 led[i].g = preled[cur].g;
622 led[i].b = preled[cur].b;
626 if (increment == 1) {
627 if (pos - 1 < 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
628 pos = 0 - RGBLIGHT_EFFECT_KNIGHT_LENGTH;
634 if (pos + 1 > RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH) {
635 pos = RGBLED_NUM + RGBLIGHT_EFFECT_KNIGHT_LENGTH - 1;
644 void rgblight_effect_christmas(void) {
645 static uint16_t current_offset = 0;
646 static uint16_t last_timer = 0;
649 if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) {
652 last_timer = timer_read();
653 current_offset = (current_offset + 1) % 2;
654 for (i = 0; i < RGBLED_NUM; i++) {
655 hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120;
656 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);