X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=quantum%2Frgblight.c;h=eff70aae1df701aa98ffacc26e80ebdbd4bbde7c;hb=344ebbac43ef510b29cdcdff2688186ee3ffead7;hp=1901010bf96ab4d5b0c67b6a17f07119afc4afa7;hpb=e9f748751808de2f1e85cf7fb670d78773bd5e76;p=qmk_firmware.git diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 1901010bf..eff70aae1 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -1,3 +1,18 @@ +/* Copyright 2016-2017 Yang Liu + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ #include #include #include @@ -66,6 +81,8 @@ __attribute__ ((weak)) const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20}; __attribute__ ((weak)) const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {100, 50, 20}; +__attribute__ ((weak)) +const uint16_t RGBLED_GRADIENT_RANGES[] PROGMEM = {360, 240, 180, 120, 90}; rgblight_config_t rgblight_config; rgblight_config_t inmem_config; @@ -174,7 +191,7 @@ void rgblight_init(void) { } eeconfig_debug_rgblight(); // display current eeprom values - #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) + #ifdef RGBLIGHT_ANIMATIONS rgblight_timer_init(); // setup the timer #endif @@ -183,6 +200,19 @@ void rgblight_init(void) { } } +void rgblight_update_dword(uint32_t dword) { + rgblight_config.raw = dword; + eeconfig_update_rgblight(rgblight_config.raw); + if (rgblight_config.enable) + rgblight_mode(rgblight_config.mode); + else { + #ifdef RGBLIGHT_ANIMATIONS + rgblight_timer_disable(); + #endif + rgblight_set(); + } +} + void rgblight_increase(void) { uint8_t mode = 0; if (rgblight_config.mode < RGBLIGHT_MODES) { @@ -206,6 +236,14 @@ void rgblight_step(void) { } rgblight_mode(mode); } +void rgblight_step_reverse(void) { + uint8_t mode = 0; + mode = rgblight_config.mode - 1; + if (mode < 1) { + mode = RGBLIGHT_MODES; + } + rgblight_mode(mode); +} void rgblight_mode(uint8_t mode) { if (!rgblight_config.enable) { @@ -221,19 +259,25 @@ void rgblight_mode(uint8_t mode) { eeconfig_update_rgblight(rgblight_config.raw); xprintf("rgblight mode: %u\n", rgblight_config.mode); if (rgblight_config.mode == 1) { - #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) + #ifdef RGBLIGHT_ANIMATIONS rgblight_timer_disable(); #endif - } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 23) { + } else if (rgblight_config.mode >= 2 && rgblight_config.mode <= 24) { // MODE 2-5, breathing // MODE 6-8, rainbow mood // MODE 9-14, rainbow swirl // MODE 15-20, snake // MODE 21-23, knight - #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) + #ifdef RGBLIGHT_ANIMATIONS rgblight_timer_enable(); #endif + } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) { + // MODE 25-34, static gradient + + #ifdef RGBLIGHT_ANIMATIONS + rgblight_timer_disable(); + #endif } rgblight_sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val); } @@ -245,7 +289,7 @@ void rgblight_toggle(void) { if (rgblight_config.enable) { rgblight_mode(rgblight_config.mode); } else { - #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) + #ifdef RGBLIGHT_ANIMATIONS rgblight_timer_disable(); #endif _delay_ms(50); @@ -253,6 +297,13 @@ void rgblight_toggle(void) { } } +void rgblight_enable(void) { + rgblight_config.enable = 1; + eeconfig_update_rgblight(rgblight_config.raw); + xprintf("rgblight enable: rgblight_config.enable = %u\n", rgblight_config.enable); + rgblight_mode(rgblight_config.mode); +} + void rgblight_increase_hue(void) { uint16_t hue; @@ -330,6 +381,17 @@ void rgblight_sethsv(uint16_t hue, uint8_t sat, uint8_t val) { } else if (rgblight_config.mode >= 6 && rgblight_config.mode <= 14) { // rainbow mood and rainbow swirl, ignore the change of hue hue = rgblight_config.hue; + } else if (rgblight_config.mode >= 25 && rgblight_config.mode <= 34) { + // static gradient + uint16_t _hue; + int8_t direction = ((rgblight_config.mode - 25) % 2) ? -1 : 1; + uint16_t range = pgm_read_word(&RGBLED_GRADIENT_RANGES[(rgblight_config.mode - 25) / 2]); + for (uint8_t i = 0; i < RGBLED_NUM; i++) { + _hue = (range / RGBLED_NUM * i * direction + hue + 360) % 360; + dprintf("rgblight rainbow set hsv: %u,%u,%d,%u\n", i, _hue, direction, range); + sethsv(_hue, sat, val, (LED_TYPE *)&led[i]); + } + rgblight_set(); } } rgblight_config.hue = hue; @@ -350,6 +412,7 @@ void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { rgblight_set(); } +__attribute__ ((weak)) void rgblight_set(void) { if (rgblight_config.enable) { #ifdef RGBW @@ -371,7 +434,7 @@ void rgblight_set(void) { } } -#if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) +#ifdef RGBLIGHT_ANIMATIONS // Animation timer -- AVR Timer3 void rgblight_timer_init(void) { @@ -405,6 +468,12 @@ void rgblight_timer_toggle(void) { dprintf("TIMER3 toggled.\n"); } +void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) { + rgblight_enable(); + rgblight_mode(1); + rgblight_setrgb(r, g, b); +} + void rgblight_task(void) { if (rgblight_timer_enabled) { // mode = 1, static light, do nothing here @@ -423,6 +492,9 @@ void rgblight_task(void) { } else if (rgblight_config.mode >= 21 && rgblight_config.mode <= 23) { // mode = 21 to 23, knight mode rgblight_effect_knight(rgblight_config.mode - 21); + } else if (rgblight_config.mode == 24) { + // mode = 24, christmas mode + rgblight_effect_christmas(); } } } @@ -568,4 +640,22 @@ void rgblight_effect_knight(uint8_t interval) { } } + +void rgblight_effect_christmas(void) { + static uint16_t current_offset = 0; + static uint16_t last_timer = 0; + uint16_t hue; + uint8_t i; + if (timer_elapsed(last_timer) < RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL) { + return; + } + last_timer = timer_read(); + current_offset = (current_offset + 1) % 2; + for (i = 0; i < RGBLED_NUM; i++) { + hue = 0 + ((i/RGBLIGHT_EFFECT_CHRISTMAS_STEP + current_offset) % 2) * 120; + sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i]); + } + rgblight_set(); +} + #endif