aboutsummaryrefslogtreecommitdiff
path: root/quantum/rgblight.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/rgblight.c')
-rw-r--r--quantum/rgblight.c126
1 files changed, 73 insertions, 53 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c
index d550c5866..00620da58 100644
--- a/quantum/rgblight.c
+++ b/quantum/rgblight.c
@@ -69,11 +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;
72struct cRGB led[RGBLED_NUM];
73uint8_t rgblight_inited = 0;
74 72
73LED_TYPE led[RGBLED_NUM];
74uint8_t rgblight_inited = 0;
75bool rgblight_timer_enabled = false;
75 76
76void 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) {
77 uint8_t r = 0, g = 0, b = 0, base, color; 78 uint8_t r = 0, g = 0, b = 0, base, color;
78 79
79 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. 80 if (sat == 0) { // Acromatic color (gray). Hue doesn't mind.
@@ -124,7 +125,7 @@ void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) {
124 setrgb(r, g, b, led1); 125 setrgb(r, g, b, led1);
125} 126}
126 127
127void 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) {
128 (*led1).r = r; 129 (*led1).r = r;
129 (*led1).g = g; 130 (*led1).g = g;
130 (*led1).b = b; 131 (*led1).b = b;
@@ -141,9 +142,9 @@ void eeconfig_update_rgblight_default(void) {
141 dprintf("eeconfig_update_rgblight_default\n"); 142 dprintf("eeconfig_update_rgblight_default\n");
142 rgblight_config.enable = 1; 143 rgblight_config.enable = 1;
143 rgblight_config.mode = 1; 144 rgblight_config.mode = 1;
144 rgblight_config.hue = 200; 145 rgblight_config.hue = 0;
145 rgblight_config.sat = 204; 146 rgblight_config.sat = 255;
146 rgblight_config.val = 204; 147 rgblight_config.val = 255;
147 eeconfig_update_rgblight(rgblight_config.raw); 148 eeconfig_update_rgblight(rgblight_config.raw);
148} 149}
149void eeconfig_debug_rgblight(void) { 150void eeconfig_debug_rgblight(void) {
@@ -173,7 +174,7 @@ void rgblight_init(void) {
173 } 174 }
174 eeconfig_debug_rgblight(); // display current eeprom values 175 eeconfig_debug_rgblight(); // display current eeprom values
175 176
176 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 177 #ifdef RGBLIGHT_ANIMATIONS
177 rgblight_timer_init(); // setup the timer 178 rgblight_timer_init(); // setup the timer
178 #endif 179 #endif
179 180
@@ -220,7 +221,7 @@ void rgblight_mode(uint8_t mode) {
220 eeconfig_update_rgblight(rgblight_config.raw); 221 eeconfig_update_rgblight(rgblight_config.raw);
221 xprintf("rgblight mode: %u\n", rgblight_config.mode); 222 xprintf("rgblight mode: %u\n", rgblight_config.mode);
222 if (rgblight_config.mode == 1) { 223 if (rgblight_config.mode == 1) {
223 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 224 #ifdef RGBLIGHT_ANIMATIONS
224 rgblight_timer_disable(); 225 rgblight_timer_disable();
225 #endif 226 #endif
226 } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) { 227 } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) {
@@ -230,7 +231,7 @@ void rgblight_mode(uint8_t mode) {
230 // MODE 15-20, snake 231 // MODE 15-20, snake
231 // MODE 21-23, knight 232 // MODE 21-23, knight
232 233
233 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 234 #ifdef RGBLIGHT_ANIMATIONS
234 rgblight_timer_enable(); 235 rgblight_timer_enable();
235 #endif 236 #endif
236 } 237 }
@@ -244,7 +245,7 @@ void rgblight_toggle(void) {
244 if (rgblight_config.enable) { 245 if (rgblight_config.enable) {
245 rgblight_mode(rgblight_config.mode); 246 rgblight_mode(rgblight_config.mode);
246 } else { 247 } else {
247 #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 248 #ifdef RGBLIGHT_ANIMATIONS
248 rgblight_timer_disable(); 249 rgblight_timer_disable();
249 #endif 250 #endif
250 _delay_ms(50); 251 _delay_ms(50);
@@ -252,6 +253,13 @@ void rgblight_toggle(void) {
252 } 253 }
253} 254}
254 255
256void rgblight_enable(void) {
257 rgblight_config.enable = 1;
258 eeconfig_update_rgblight(rgblight_config.raw);
259 xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable);
260 rgblight_mode(rgblight_config.mode);
261}
262
255 263
256void rgblight_increase_hue(void) { 264void rgblight_increase_hue(void) {
257 uint16_t hue; 265 uint16_t hue;
@@ -307,7 +315,7 @@ void rgblight_decrease_val(void) {
307void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { 315void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) {
308 inmem_config.raw = rgblight_config.raw; 316 inmem_config.raw = rgblight_config.raw;
309 if (rgblight_config.enable) { 317 if (rgblight_config.enable) {
310 struct cRGB tmp_led; 318 LED_TYPE tmp_led;
311 sethsv(hue, sat, val, &tmp_led); 319 sethsv(hue, sat, val, &tmp_led);
312 inmem_config.hue = hue; 320 inmem_config.hue = hue;
313 inmem_config.sat = sat; 321 inmem_config.sat = sat;
@@ -351,66 +359,78 @@ void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) {
351 359
352void rgblight_set(void) { 360void rgblight_set(void) {
353 if (rgblight_config.enable) { 361 if (rgblight_config.enable) {
354 ws2812_setleds(led, RGBLED_NUM); 362 #ifdef RGBW
363 ws2812_setleds_rgbw(led, RGBLED_NUM);
364 #else
365 ws2812_setleds(led, RGBLED_NUM);
366 #endif
355 } else { 367 } else {
356 for (uint8_t i = 0; i < RGBLED_NUM; i++) { 368 for (uint8_t i = 0; i < RGBLED_NUM; i++) {
357 led[i].r = 0; 369 led[i].r = 0;
358 led[i].g = 0; 370 led[i].g = 0;
359 led[i].b = 0; 371 led[i].b = 0;
360 } 372 }
361 ws2812_setleds(led, RGBLED_NUM); 373 #ifdef RGBW
374 ws2812_setleds_rgbw(led, RGBLED_NUM);
375 #else
376 ws2812_setleds(led, RGBLED_NUM);
377 #endif
362 } 378 }
363} 379}
364 380
365#if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) 381#ifdef RGBLIGHT_ANIMATIONS
366 382
367// Animation timer -- AVR Timer3 383// Animation timer -- AVR Timer3
368void rgblight_timer_init(void) { 384void rgblight_timer_init(void) {
369 static uint8_t rgblight_timer_is_init = 0; 385 // static uint8_t rgblight_timer_is_init = 0;
370 if (rgblight_timer_is_init) { 386 // if (rgblight_timer_is_init) {
371 return; 387 // return;
372 } 388 // }
373 rgblight_timer_is_init = 1; 389 // rgblight_timer_is_init = 1;
374 /* Timer 3 setup */ 390 // /* Timer 3 setup */
375 TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP 391 // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP
376 | _BV(CS30); //Clock selelct: clk/1 392 // | _BV(CS30); // Clock selelct: clk/1
377 /* Set TOP value */ 393 // /* Set TOP value */
378 uint8_t sreg = SREG; 394 // uint8_t sreg = SREG;
379 cli(); 395 // cli();
380 OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff; 396 // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff;
381 OCR3AL = RGBLED_TIMER_TOP & 0xff; 397 // OCR3AL = RGBLED_TIMER_TOP & 0xff;
382 SREG = sreg; 398 // SREG = sreg;
399
400 rgblight_timer_enabled = true;
383} 401}
384void rgblight_timer_enable(void) { 402void rgblight_timer_enable(void) {
385 TIMSK3 |= _BV(OCIE3A); 403 rgblight_timer_enabled = true;
386 dprintf("TIMER3 enabled.\n"); 404 dprintf("TIMER3 enabled.\n");
387} 405}
388void rgblight_timer_disable(void) { 406void rgblight_timer_disable(void) {
389 TIMSK3 &= ~_BV(OCIE3A); 407 rgblight_timer_enabled = false;
390 dprintf("TIMER3 disabled.\n"); 408 dprintf("TIMER3 disabled.\n");
391} 409}
392void rgblight_timer_toggle(void) { 410void rgblight_timer_toggle(void) {
393 TIMSK3 ^= _BV(OCIE3A); 411 rgblight_timer_enabled ^= rgblight_timer_enabled;
394 dprintf("TIMER3 toggled.\n"); 412 dprintf("TIMER3 toggled.\n");
395} 413}
396 414
397ISR(TIMER3_COMPA_vect) { 415void rgblight_task(void) {
398 // mode = 1, static light, do nothing here 416 if (rgblight_timer_enabled) {
399 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { 417 // mode = 1, static light, do nothing here
400 // mode = 2 to 5, breathing mode 418 if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) {
401 rgblight_effect_breathing(rgblight_config.mode - 2); 419 // mode = 2 to 5, breathing mode
402 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) { 420 rgblight_effect_breathing(rgblight_config.mode - 2);
403 // mode = 6 to 8, rainbow mood mod 421 } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) {
404 rgblight_effect_rainbow_mood(rgblight_config.mode - 6); 422 // mode = 6 to 8, rainbow mood mod
405 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) { 423 rgblight_effect_rainbow_mood(rgblight_config.mode - 6);
406 // mode = 9 to 14, rainbow swirl mode 424 } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) {
407 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9); 425 // mode = 9 to 14, rainbow swirl mode
408 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) { 426 rgblight_effect_rainbow_swirl(rgblight_config.mode - 9);
409 // mode = 15 to 20, snake mode 427 } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) {
410 rgblight_effect_snake(rgblight_config.mode - 15); 428 // mode = 15 to 20, snake mode
411 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { 429 rgblight_effect_snake(rgblight_config.mode - 15);
412 // mode = 21 to 23, knight mode 430 } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) {
413 rgblight_effect_knight(rgblight_config.mode - 21); 431 // mode = 21 to 23, knight mode
432 rgblight_effect_knight(rgblight_config.mode - 21);
433 }
414 } 434 }
415} 435}
416 436
@@ -449,7 +469,7 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) {
449 last_timer = timer_read(); 469 last_timer = timer_read();
450 for (i = 0; i < RGBLED_NUM; i++) { 470 for (i = 0; i < RGBLED_NUM; i++) {
451 hue = (360 / RGBLED_NUM * i + current_hue) % 360; 471 hue = (360 / RGBLED_NUM * i + current_hue) % 360;
452 sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]); 472 sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]);
453 } 473 }
454 rgblight_set(); 474 rgblight_set();
455 475
@@ -486,7 +506,7 @@ void rgblight_effect_snake(uint8_t interval) {
486 k = k + RGBLED_NUM; 506 k = k + RGBLED_NUM;
487 } 507 }
488 if (i == k) { 508 if (i == k) {
489 sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val*(RGBLIGHT_EFFECT_SNAKE_LENGTH-j)/RGBLIGHT_EFFECT_SNAKE_LENGTH), &led[i]); 509 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]);
490 } 510 }
491 } 511 }
492 } 512 }
@@ -506,7 +526,7 @@ void rgblight_effect_knight(uint8_t interval) {
506 static uint16_t last_timer = 0; 526 static uint16_t last_timer = 0;
507 uint8_t i, j, cur; 527 uint8_t i, j, cur;
508 int8_t k; 528 int8_t k;
509 struct cRGB preled[RGBLED_NUM]; 529 LED_TYPE preled[RGBLED_NUM];
510 static int8_t increment = -1; 530 static int8_t increment = -1;
511 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { 531 if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) {
512 return; 532 return;
@@ -525,7 +545,7 @@ void rgblight_effect_knight(uint8_t interval) {
525 k = RGBLED_NUM - 1; 545 k = RGBLED_NUM - 1;
526 } 546 }
527 if (i == k) { 547 if (i == k) {
528 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]); 548 sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&preled[i]);
529 } 549 }
530 } 550 }
531 } 551 }