aboutsummaryrefslogtreecommitdiff
path: root/quantum/rgblight.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/rgblight.c')
-rw-r--r--quantum/rgblight.c101
1 files changed, 51 insertions, 50 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index 221a16402..1901010bf 100644
--- a/quantum/rgblight.c
+++ b/quantum/rgblight.c
@@ -69,15 +69,12 @@ const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {100, 50, 20};
69 69
70rgblight_config_t rgblight_config; 70rgblight_config_t rgblight_config;
71rgblight_config_t inmem_config; 71rgblight_config_t inmem_config;
72#ifdef RGBW
73 struct cRGBW led[RGBLED_NUM];
74#else
75 struct cRGB led[RGBLED_NUM];
76#endif
77uint8_t rgblight_inited = 0;
78 72
73LED_TYPE led[RGBLED_NUM];
74uint8_t rgblight_inited = 0;
75bool rgblight_timer_enabled = false;
79 76
80void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) { 77void sethsv(uint16_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) {
81 uint8_t r = 0, g = 0, b = 0, base, color; 78 uint8_t r = 0, g = 0, b = 0, base, color;
82 79
83 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. 80 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
@@ -128,7 +125,7 @@ void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
128 setrgb(r, g, b, led1); 125 setrgb(r, g, b, led1);
129} 126}
130 127
131void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { 128void setrgb(uint8_t r, uint8_t g, uint8_t b, LED_TYPE *led1) {
132 (*led1).r = r; 129 (*led1).r = r;
133 (*led1).g = g; 130 (*led1).g = g;
134 (*led1).b = b; 131 (*led1).b = b;
@@ -145,9 +142,9 @@ void eeconfig_update_rgblight_default(void) {
145 dprintf("eeconfig_update_rgblight_default\n"); 142 dprintf("eeconfig_update_rgblight_default\n");
146 rgblight_config.enable = 1; 143 rgblight_config.enable = 1;
147 rgblight_config.mode = 1; 144 rgblight_config.mode = 1;
148 rgblight_config.hue = 200; 145 rgblight_config.hue = 0;
149 rgblight_config.sat = 204; 146 rgblight_config.sat = 255;
150 rgblight_config.val = 204; 147 rgblight_config.val = 255;
151 eeconfig_update_rgblight(rgblight_config.raw); 148 eeconfig_update_rgblight(rgblight_config.raw);
152} 149}
153void eeconfig_debug_rgblight(void) { 150void eeconfig_debug_rgblight(void) {
@@ -311,7 +308,7 @@ void rgblight_decrease_val(void) {
311void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { 308void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
312 inmem_config.raw = rgblight_config.raw; 309 inmem_config.raw = rgblight_config.raw;
313 if (rgblight_config.enable) { 310 if (rgblight_config.enable) {
314 struct cRGB tmp_led; 311 LED_TYPE tmp_led;
315 sethsv(hue, sat, val, &tmp_led); 312 sethsv(hue, sat, val, &tmp_led);
316 inmem_config.hue = hue; 313 inmem_config.hue = hue;
317 inmem_config.sat = sat; 314 inmem_config.sat = sat;
@@ -378,51 +375,55 @@ void rgblight_set(void) {
378 375
379// Animation timer -- AVR Timer3 376// Animation timer -- AVR Timer3
380void rgblight_timer_init(void) { 377void rgblight_timer_init(void) {
381 static uint8_t rgblight_timer_is_init = 0; 378 // static uint8_t rgblight_timer_is_init = 0;
382 if (rgblight_timer_is_init) { 379 // if (rgblight_timer_is_init) {
383 return; 380 // return;
384 } 381 // }
385 rgblight_timer_is_init = 1; 382 // rgblight_timer_is_init = 1;
386 /* Timer 3 setup */ 383 // /* Timer 3 setup */
387 TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP 384 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
388 | _BV(CS30); //Clock selelct: clk/1 385 // | _BV(CS30); // Clock selelct: clk/1
389 /* Set TOP value */ 386 // /* Set TOP value */
390 uint8_t sreg = SREG; 387 // uint8_t sreg = SREG;
391 cli(); 388 // cli();
392 OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff; 389 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
393 OCR3AL = RGBLED_TIMER_TOP & 0xff; 390 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
394 SREG = sreg; 391 // SREG = sreg;
392
393 rgblight_timer_enabled = true;
395} 394}
396void rgblight_timer_enable(void) { 395void rgblight_timer_enable(void) {
397 TIMSK3 |= _BV(OCIE3A); 396 rgblight_timer_enabled = true;
398 dprintf("TIMER3 enabled.\n"); 397 dprintf("TIMER3 enabled.\n");
399} 398}
400void rgblight_timer_disable(void) { 399void rgblight_timer_disable(void) {
401 TIMSK3 &= ~_BV(OCIE3A); 400 rgblight_timer_enabled = false;
402 dprintf("TIMER3 disabled.\n"); 401 dprintf("TIMER3 disabled.\n");
403} 402}
404void rgblight_timer_toggle(void) { 403void rgblight_timer_toggle(void) {
405 TIMSK3 ^= _BV(OCIE3A); 404 rgblight_timer_enabled ^= rgblight_timer_enabled;
406 dprintf("TIMER3 toggled.\n"); 405 dprintf("TIMER3 toggled.\n");
407} 406}
408 407
409ISR(TIMER3_COMPA_vect) { 408void rgblight_task(void) {
410 // mode = 1, static light, do nothing here 409 if (rgblight_timer_enabled) {
411 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { 410 // mode = 1, static light, do nothing here
412 // mode = 2 to 5, breathing mode 411 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
413 rgblight_effect_breathing(rgblight_config.mode - 2); 412 // mode = 2 to 5, breathing mode
414 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) { 413 rgblight_effect_breathing(rgblight_config.mode - 2);
415 // mode = 6 to 8, rainbow mood mod 414 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
416 rgblight_effect_rainbow_mood(rgblight_config.mode - 6); 415 // mode = 6 to 8, rainbow mood mod
417 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) { 416 rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
418 // mode = 9 to 14, rainbow swirl mode 417 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
419 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9); 418 // mode = 9 to 14, rainbow swirl mode
420 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) { 419 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
421 // mode = 15 to 20, snake mode 420 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
422 rgblight_effect_snake(rgblight_config.mode - 15); 421 // mode = 15 to 20, snake mode
423 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { 422 rgblight_effect_snake(rgblight_config.mode - 15);
424 // mode = 21 to 23, knight mode 423 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
425 rgblight_effect_knight(rgblight_config.mode - 21); 424 // mode = 21 to 23, knight mode
425 rgblight_effect_knight(rgblight_config.mode - 21);
426 }
426 } 427 }
427} 428}
428 429
@@ -461,7 +462,7 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) {
461 last_timer = timer_read(); 462 last_timer = timer_read();
462 for (i = 0; i < RGBLED_NUM; i++) { 463 for (i = 0; i < RGBLED_NUM; i++) {
463 hue = (360 / RGBLED_NUM * i + current_hue) % 360; 464 hue = (360 / RGBLED_NUM * i + current_hue) % 360;
464 sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]); 465 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
465 } 466 }
466 rgblight_set(); 467 rgblight_set();
467 468
@@ -498,7 +499,7 @@ void rgblight_effect_snake(uint8_t interval) {
498 k = k + RGBLED_NUM; 499 k = k + RGBLED_NUM;
499 } 500 }
500 if (i == k) { 501 if (i == k) {
501 sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]); 502 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]);
502 } 503 }
503 } 504 }
504 } 505 }
@@ -518,7 +519,7 @@ void rgblight_effect_knight(uint8_t interval) {
518 static uint16_t last_timer = 0; 519 static uint16_t last_timer = 0;
519 uint8_t i, j, cur; 520 uint8_t i, j, cur;
520 int8_t k; 521 int8_t k;
521 struct cRGB preled[RGBLED_NUM]; 522 LED_TYPE preled[RGBLED_NUM];
522 static int8_t increment = -1; 523 static int8_t increment = -1;
523 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { 524 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
524 return; 525 return;
@@ -537,7 +538,7 @@ void rgblight_effect_knight(uint8_t interval) {
537 k = RGBLED_NUM - 1; 538 k = RGBLED_NUM - 1;
538 } 539 }
539 if (i == k) { 540 if (i == k) {
540 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]); 541 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&preled[i]);
541 } 542 }
542 } 543 }
543 } 544 }