diff options
| author | Erez Zukerman <bulk@ezuk.org> | 2016-11-29 08:16:04 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-11-29 08:16:04 -0500 |
| commit | 51ae6da99edd732ccdec6e102bd757e08950d23b (patch) | |
| tree | 53e316e1fbe468e0b1f65fd11e161ebc74c6ef70 /quantum/rgblight.c | |
| parent | 7b219a7f6016dfe80c1c3db33bcf859e3e355d8d (diff) | |
| parent | f946d830f98da0161753d37da9659caa7469cf4f (diff) | |
| download | qmk_firmware-51ae6da99edd732ccdec6e102bd757e08950d23b.tar.gz qmk_firmware-51ae6da99edd732ccdec6e102bd757e08950d23b.zip | |
Merge pull request #918 from jackhumbert/wu5y7
Adds Ergodox EZ RGB lights (both sides using I2C) and implements API base, Midi SysEx API
Diffstat (limited to 'quantum/rgblight.c')
| -rw-r--r-- | quantum/rgblight.c | 139 |
1 files changed, 86 insertions, 53 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c index d550c5866..bb03d6e91 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 | ||
| 70 | rgblight_config_t rgblight_config; | 70 | rgblight_config_t rgblight_config; |
| 71 | rgblight_config_t inmem_config; | 71 | rgblight_config_t inmem_config; |
| 72 | struct cRGB led[RGBLED_NUM]; | ||
| 73 | uint8_t rgblight_inited = 0; | ||
| 74 | 72 | ||
| 73 | LED_TYPE led[RGBLED_NUM]; | ||
| 74 | uint8_t rgblight_inited = 0; | ||
| 75 | bool rgblight_timer_enabled = false; | ||
| 75 | 76 | ||
| 76 | void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) { | 77 | void 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 | ||
| 127 | void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { | 128 | void 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 | } |
| 149 | void eeconfig_debug_rgblight(void) { | 150 | void 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 | ||
| @@ -182,6 +183,19 @@ void rgblight_init(void) { | |||
| 182 | } | 183 | } |
| 183 | } | 184 | } |
| 184 | 185 | ||
| 186 | void rgblight_update_dword(uint32_t dword) { | ||
| 187 | rgblight_config.raw = dword; | ||
| 188 | eeconfig_update_rgblight(rgblight_config.raw); | ||
| 189 | if (rgblight_config.enable) | ||
| 190 | rgblight_mode(rgblight_config.mode); | ||
| 191 | else { | ||
| 192 | #ifdef RGBLIGHT_ANIMATIONS | ||
| 193 | rgblight_timer_disable(); | ||
| 194 | #endif | ||
| 195 | rgblight_set(); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 185 | void rgblight_increase(void) { | 199 | void rgblight_increase(void) { |
| 186 | uint8_t mode = 0; | 200 | uint8_t mode = 0; |
| 187 | if (rgblight_config.mode < RGBLIGHT_MODES) { | 201 | if (rgblight_config.mode < RGBLIGHT_MODES) { |
| @@ -220,7 +234,7 @@ void rgblight_mode(uint8_t mode) { | |||
| 220 | eeconfig_update_rgblight(rgblight_config.raw); | 234 | eeconfig_update_rgblight(rgblight_config.raw); |
| 221 | xprintf("rgblight mode: %u\n", rgblight_config.mode); | 235 | xprintf("rgblight mode: %u\n", rgblight_config.mode); |
| 222 | if (rgblight_config.mode == 1) { | 236 | if (rgblight_config.mode == 1) { |
| 223 | #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) | 237 | #ifdef RGBLIGHT_ANIMATIONS |
| 224 | rgblight_timer_disable(); | 238 | rgblight_timer_disable(); |
| 225 | #endif | 239 | #endif |
| 226 | } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) { | 240 | } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) { |
| @@ -230,7 +244,7 @@ void rgblight_mode(uint8_t mode) { | |||
| 230 | // MODE 15-20, snake | 244 | // MODE 15-20, snake |
| 231 | // MODE 21-23, knight | 245 | // MODE 21-23, knight |
| 232 | 246 | ||
| 233 | #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) | 247 | #ifdef RGBLIGHT_ANIMATIONS |
| 234 | rgblight_timer_enable(); | 248 | rgblight_timer_enable(); |
| 235 | #endif | 249 | #endif |
| 236 | } | 250 | } |
| @@ -244,7 +258,7 @@ void rgblight_toggle(void) { | |||
| 244 | if (rgblight_config.enable) { | 258 | if (rgblight_config.enable) { |
| 245 | rgblight_mode(rgblight_config.mode); | 259 | rgblight_mode(rgblight_config.mode); |
| 246 | } else { | 260 | } else { |
| 247 | #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) | 261 | #ifdef RGBLIGHT_ANIMATIONS |
| 248 | rgblight_timer_disable(); | 262 | rgblight_timer_disable(); |
| 249 | #endif | 263 | #endif |
| 250 | _delay_ms(50); | 264 | _delay_ms(50); |
| @@ -252,6 +266,13 @@ void rgblight_toggle(void) { | |||
| 252 | } | 266 | } |
| 253 | } | 267 | } |
| 254 | 268 | ||
| 269 | void rgblight_enable(void) { | ||
| 270 | rgblight_config.enable = 1; | ||
| 271 | eeconfig_update_rgblight(rgblight_config.raw); | ||
| 272 | xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable); | ||
| 273 | rgblight_mode(rgblight_config.mode); | ||
| 274 | } | ||
| 275 | |||
| 255 | 276 | ||
| 256 | void rgblight_increase_hue(void) { | 277 | void rgblight_increase_hue(void) { |
| 257 | uint16_t hue; | 278 | uint16_t hue; |
| @@ -307,7 +328,7 @@ void rgblight_decrease_val(void) { | |||
| 307 | void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { | 328 | void rgblight_sethsv_noeeprom(uint16_t hue, uint8_t sat, uint8_t val) { |
| 308 | inmem_config.raw = rgblight_config.raw; | 329 | inmem_config.raw = rgblight_config.raw; |
| 309 | if (rgblight_config.enable) { | 330 | if (rgblight_config.enable) { |
| 310 | struct cRGB tmp_led; | 331 | LED_TYPE tmp_led; |
| 311 | sethsv(hue, sat, val, &tmp_led); | 332 | sethsv(hue, sat, val, &tmp_led); |
| 312 | inmem_config.hue = hue; | 333 | inmem_config.hue = hue; |
| 313 | inmem_config.sat = sat; | 334 | inmem_config.sat = sat; |
| @@ -351,66 +372,78 @@ void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { | |||
| 351 | 372 | ||
| 352 | void rgblight_set(void) { | 373 | void rgblight_set(void) { |
| 353 | if (rgblight_config.enable) { | 374 | if (rgblight_config.enable) { |
| 354 | ws2812_setleds(led, RGBLED_NUM); | 375 | #ifdef RGBW |
| 376 | ws2812_setleds_rgbw(led, RGBLED_NUM); | ||
| 377 | #else | ||
| 378 | ws2812_setleds(led, RGBLED_NUM); | ||
| 379 | #endif | ||
| 355 | } else { | 380 | } else { |
| 356 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { | 381 | for (uint8_t i = 0; i < RGBLED_NUM; i++) { |
| 357 | led[i].r = 0; | 382 | led[i].r = 0; |
| 358 | led[i].g = 0; | 383 | led[i].g = 0; |
| 359 | led[i].b = 0; | 384 | led[i].b = 0; |
| 360 | } | 385 | } |
| 361 | ws2812_setleds(led, RGBLED_NUM); | 386 | #ifdef RGBW |
| 387 | ws2812_setleds_rgbw(led, RGBLED_NUM); | ||
| 388 | #else | ||
| 389 | ws2812_setleds(led, RGBLED_NUM); | ||
| 390 | #endif | ||
| 362 | } | 391 | } |
| 363 | } | 392 | } |
| 364 | 393 | ||
| 365 | #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) | 394 | #ifdef RGBLIGHT_ANIMATIONS |
| 366 | 395 | ||
| 367 | // Animation timer -- AVR Timer3 | 396 | // Animation timer -- AVR Timer3 |
| 368 | void rgblight_timer_init(void) { | 397 | void rgblight_timer_init(void) { |
| 369 | static uint8_t rgblight_timer_is_init = 0; | 398 | // static uint8_t rgblight_timer_is_init = 0; |
| 370 | if (rgblight_timer_is_init) { | 399 | // if (rgblight_timer_is_init) { |
| 371 | return; | 400 | // return; |
| 372 | } | 401 | // } |
| 373 | rgblight_timer_is_init = 1; | 402 | // rgblight_timer_is_init = 1; |
| 374 | /* Timer 3 setup */ | 403 | // /* Timer 3 setup */ |
| 375 | TCCR3B = _BV(WGM32) //CTC mode OCR3A as TOP | 404 | // TCCR3B = _BV(WGM32) // CTC mode OCR3A as TOP |
| 376 | | _BV(CS30); //Clock selelct: clk/1 | 405 | // | _BV(CS30); // Clock selelct: clk/1 |
| 377 | /* Set TOP value */ | 406 | // /* Set TOP value */ |
| 378 | uint8_t sreg = SREG; | 407 | // uint8_t sreg = SREG; |
| 379 | cli(); | 408 | // cli(); |
| 380 | OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff; | 409 | // OCR3AH = (RGBLED_TIMER_TOP >> 8) & 0xff; |
| 381 | OCR3AL = RGBLED_TIMER_TOP & 0xff; | 410 | // OCR3AL = RGBLED_TIMER_TOP & 0xff; |
| 382 | SREG = sreg; | 411 | // SREG = sreg; |
| 412 | |||
| 413 | rgblight_timer_enabled = true; | ||
| 383 | } | 414 | } |
| 384 | void rgblight_timer_enable(void) { | 415 | void rgblight_timer_enable(void) { |
| 385 | TIMSK3 |= _BV(OCIE3A); | 416 | rgblight_timer_enabled = true; |
| 386 | dprintf("TIMER3 enabled.\n"); | 417 | dprintf("TIMER3 enabled.\n"); |
| 387 | } | 418 | } |
| 388 | void rgblight_timer_disable(void) { | 419 | void rgblight_timer_disable(void) { |
| 389 | TIMSK3 &= ~_BV(OCIE3A); | 420 | rgblight_timer_enabled = false; |
| 390 | dprintf("TIMER3 disabled.\n"); | 421 | dprintf("TIMER3 disabled.\n"); |
| 391 | } | 422 | } |
| 392 | void rgblight_timer_toggle(void) { | 423 | void rgblight_timer_toggle(void) { |
| 393 | TIMSK3 ^= _BV(OCIE3A); | 424 | rgblight_timer_enabled ^= rgblight_timer_enabled; |
| 394 | dprintf("TIMER3 toggled.\n"); | 425 | dprintf("TIMER3 toggled.\n"); |
| 395 | } | 426 | } |
| 396 | 427 | ||
| 397 | ISR(TIMER3_COMPA_vect) { | 428 | void rgblight_task(void) { |
| 398 | // mode = 1, static light, do nothing here | 429 | if (rgblight_timer_enabled) { |
| 399 | if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { | 430 | // mode = 1, static light, do nothing here |
| 400 | // mode = 2 to 5, breathing mode | 431 | if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { |
| 401 | rgblight_effect_breathing(rgblight_config.mode - 2); | 432 | // mode = 2 to 5, breathing mode |
| 402 | } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) { | 433 | rgblight_effect_breathing(rgblight_config.mode - 2); |
| 403 | // mode = 6 to 8, rainbow mood mod | 434 | } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 8) { |
| 404 | rgblight_effect_rainbow_mood(rgblight_config.mode - 6); | 435 | // mode = 6 to 8, rainbow mood mod |
| 405 | } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) { | 436 | rgblight_effect_rainbow_mood(rgblight_config.mode - 6); |
| 406 | // mode = 9 to 14, rainbow swirl mode | 437 | } else if (rgblight_config.mode >= 9 && rgblight_config.mode <= 14) { |
| 407 | rgblight_effect_rainbow_swirl(rgblight_config.mode - 9); | 438 | // mode = 9 to 14, rainbow swirl mode |
| 408 | } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) { | 439 | rgblight_effect_rainbow_swirl(rgblight_config.mode - 9); |
| 409 | // mode = 15 to 20, snake mode | 440 | } else if (rgblight_config.mode >= 15 && rgblight_config.mode <= 20) { |
| 410 | rgblight_effect_snake(rgblight_config.mode - 15); | 441 | // mode = 15 to 20, snake mode |
| 411 | } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { | 442 | rgblight_effect_snake(rgblight_config.mode - 15); |
| 412 | // mode = 21 to 23, knight mode | 443 | } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { |
| 413 | rgblight_effect_knight(rgblight_config.mode - 21); | 444 | // mode = 21 to 23, knight mode |
| 445 | rgblight_effect_knight(rgblight_config.mode - 21); | ||
| 446 | } | ||
| 414 | } | 447 | } |
| 415 | } | 448 | } |
| 416 | 449 | ||
| @@ -449,7 +482,7 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) { | |||
| 449 | last_timer = timer_read(); | 482 | last_timer = timer_read(); |
| 450 | for (i = 0; i < RGBLED_NUM; i++) { | 483 | for (i = 0; i < RGBLED_NUM; i++) { |
| 451 | hue = (360 / RGBLED_NUM * i + current_hue) % 360; | 484 | hue = (360 / RGBLED_NUM * i + current_hue) % 360; |
| 452 | sethsv(hue, rgblight_config.sat, rgblight_config.val, &led[i]); | 485 | sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); |
| 453 | } | 486 | } |
| 454 | rgblight_set(); | 487 | rgblight_set(); |
| 455 | 488 | ||
| @@ -486,7 +519,7 @@ void rgblight_effect_snake(uint8_t interval) { | |||
| 486 | k = k + RGBLED_NUM; | 519 | k = k + RGBLED_NUM; |
| 487 | } | 520 | } |
| 488 | if (i == k) { | 521 | 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]); | 522 | 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 | } | 523 | } |
| 491 | } | 524 | } |
| 492 | } | 525 | } |
| @@ -506,7 +539,7 @@ void rgblight_effect_knight(uint8_t interval) { | |||
| 506 | static uint16_t last_timer = 0; | 539 | static uint16_t last_timer = 0; |
| 507 | uint8_t i, j, cur; | 540 | uint8_t i, j, cur; |
| 508 | int8_t k; | 541 | int8_t k; |
| 509 | struct cRGB preled[RGBLED_NUM]; | 542 | LED_TYPE preled[RGBLED_NUM]; |
| 510 | static int8_t increment = -1; | 543 | static int8_t increment = -1; |
| 511 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { | 544 | if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { |
| 512 | return; | 545 | return; |
| @@ -525,7 +558,7 @@ void rgblight_effect_knight(uint8_t interval) { | |||
| 525 | k = RGBLED_NUM - 1; | 558 | k = RGBLED_NUM - 1; |
| 526 | } | 559 | } |
| 527 | if (i == k) { | 560 | if (i == k) { |
| 528 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, &preled[i]); | 561 | sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&preled[i]); |
| 529 | } | 562 | } |
| 530 | } | 563 | } |
| 531 | } | 564 | } |
