diff options
author | QMK Bot <hello@qmk.fm> | 2021-01-05 00:27:52 +0000 |
---|---|---|
committer | QMK Bot <hello@qmk.fm> | 2021-01-05 00:27:52 +0000 |
commit | fd7ab5a22bd5ba85dde3a5da54856784bfb19120 (patch) | |
tree | a63890753c82e981764a35bb0b7ae5a38689f639 /keyboards/mxss/rgblight.c | |
parent | 25e972e8a41909f88ce66a7b8e9ac3f744425156 (diff) | |
parent | c38fe492426676cf101eeb024f7f33d8e98c445f (diff) |
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'keyboards/mxss/rgblight.c')
-rw-r--r-- | keyboards/mxss/rgblight.c | 382 |
1 files changed, 289 insertions, 93 deletions
diff --git a/keyboards/mxss/rgblight.c b/keyboards/mxss/rgblight.c index 2e9352c55d..d2d79815cd 100644 --- a/keyboards/mxss/rgblight.c +++ b/keyboards/mxss/rgblight.c @@ -15,13 +15,16 @@ */ #include <math.h> #include <string.h> +#include <stdlib.h> #ifdef __AVR__ # include <avr/eeprom.h> # include <avr/interrupt.h> #endif +#ifdef EEPROM_ENABLE +# include "eeprom.h" +#endif #ifdef STM32_EEPROM_ENABLE # include <hal.h> -# include "eeprom.h" # include "eeprom_stm32.h" #endif #include "wait.h" @@ -38,17 +41,23 @@ // MxSS custom #include "mxss_frontled.h" +#ifndef MIN +# define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif + #ifdef RGBLIGHT_SPLIT /* for split keyboard */ # define RGBLIGHT_SPLIT_SET_CHANGE_MODE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_MODE # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_HSVS # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS rgblight_status.change_flags |= (RGBLIGHT_STATUS_CHANGE_MODE | RGBLIGHT_STATUS_CHANGE_HSVS) +# define RGBLIGHT_SPLIT_SET_CHANGE_LAYERS rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_LAYERS # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE rgblight_status.change_flags |= RGBLIGHT_STATUS_CHANGE_TIMER # define RGBLIGHT_SPLIT_ANIMATION_TICK rgblight_status.change_flags |= RGBLIGHT_STATUS_ANIMATION_TICK #else # define RGBLIGHT_SPLIT_SET_CHANGE_MODE # define RGBLIGHT_SPLIT_SET_CHANGE_HSVS # define RGBLIGHT_SPLIT_SET_CHANGE_MODEHSVS +# define RGBLIGHT_SPLIT_SET_CHANGE_LAYERS # define RGBLIGHT_SPLIT_SET_CHANGE_TIMER_ENABLE # define RGBLIGHT_SPLIT_ANIMATION_TICK #endif @@ -97,11 +106,11 @@ LED_TYPE led[RGBLED_NUM]; # define LED_ARRAY led #endif -static uint8_t clipping_start_pos = 0; -static uint8_t clipping_num_leds = RGBLED_NUM; -static uint8_t effect_start_pos = 0; -static uint8_t effect_end_pos = RGBLED_NUM; -static uint8_t effect_num_leds = RGBLED_NUM; +#ifdef RGBLIGHT_LAYERS +rgblight_segment_t const *const *rgblight_layers = NULL; +#endif + +rgblight_ranges_t rgblight_ranges = {0, RGBLED_NUM, 0, RGBLED_NUM, RGBLED_NUM}; // MxSS custom extern uint8_t fled_mode; @@ -116,18 +125,20 @@ void copyrgb(LED_TYPE *src, LED_TYPE *dst) { } void rgblight_set_clipping_range(uint8_t start_pos, uint8_t num_leds) { - clipping_start_pos = start_pos; - clipping_num_leds = num_leds; + rgblight_ranges.clipping_start_pos = start_pos; + rgblight_ranges.clipping_num_leds = num_leds; } void rgblight_set_effect_range(uint8_t start_pos, uint8_t num_leds) { if (start_pos >= RGBLED_NUM) return; if (start_pos + num_leds > RGBLED_NUM) return; - effect_start_pos = start_pos; - effect_end_pos = start_pos + num_leds; - effect_num_leds = num_leds; + rgblight_ranges.effect_start_pos = start_pos; + rgblight_ranges.effect_end_pos = start_pos + num_leds; + rgblight_ranges.effect_num_leds = num_leds; } +__attribute__((weak)) RGB rgblight_hsv_to_rgb(HSV hsv) { return hsv_to_rgb(hsv); } + void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { HSV hsv = {hue, sat, val}; // MxSS custom @@ -138,7 +149,8 @@ void sethsv_raw(uint8_t hue, uint8_t sat, uint8_t val, LED_TYPE *led1) { } else if (led1 == &led[RGBLIGHT_FLED2]) { fled_hs[1].hue = hue; fled_hs[1].sat = sat; - } RGB rgb = hsv_to_rgb(hsv); + } + RGB rgb = rgblight_hsv_to_rgb(hsv); setrgb(rgb.r, rgb.g, rgb.b, led1); } @@ -168,7 +180,7 @@ void rgblight_check_config(void) { } uint32_t eeconfig_read_rgblight(void) { -#if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) +#ifdef EEPROM_ENABLE return eeprom_read_dword(EECONFIG_RGBLIGHT); #else return 0; @@ -176,15 +188,13 @@ uint32_t eeconfig_read_rgblight(void) { } void eeconfig_update_rgblight(uint32_t val) { -#if defined(__AVR__) || defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE) +#ifdef EEPROM_ENABLE rgblight_check_config(); eeprom_update_dword(EECONFIG_RGBLIGHT, val); #endif } -void eeconfig_update_rgblight_current(void) { - eeconfig_update_rgblight(rgblight_config.raw); -} +void eeconfig_update_rgblight_current(void) { eeconfig_update_rgblight(rgblight_config.raw); } void eeconfig_update_rgblight_default(void) { rgblight_config.enable = 1; @@ -233,9 +243,7 @@ void rgblight_init(void) { eeconfig_debug_rgblight(); // display current eeprom values -#ifdef RGBLIGHT_USE_TIMER rgblight_timer_init(); // setup the timer -#endif if (rgblight_config.enable) { rgblight_mode_noeeprom(rgblight_config.mode); @@ -252,9 +260,7 @@ void rgblight_update_dword(uint32_t dword) { if (rgblight_config.enable) rgblight_mode_noeeprom(rgblight_config.mode); else { -#ifdef RGBLIGHT_USE_TIMER rgblight_timer_disable(); -#endif rgblight_set(); } } @@ -322,13 +328,9 @@ void rgblight_mode_eeprom_helper(uint8_t mode, bool write_to_eeprom) { dprintf("rgblight mode [NOEEPROM]: %u\n", rgblight_config.mode); } if (is_static_effect(rgblight_config.mode)) { -#ifdef RGBLIGHT_USE_TIMER rgblight_timer_disable(); -#endif } else { -#ifdef RGBLIGHT_USE_TIMER rgblight_timer_enable(); -#endif } #ifdef RGBLIGHT_USE_TIMER animation_status.restart = true; @@ -376,9 +378,7 @@ void rgblight_disable(void) { rgblight_config.enable = 0; eeconfig_update_rgblight(rgblight_config.raw); dprintf("rgblight disable [EEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable); -#ifdef RGBLIGHT_USE_TIMER rgblight_timer_disable(); -#endif RGBLIGHT_SPLIT_SET_CHANGE_MODE; wait_ms(50); rgblight_set(); @@ -387,14 +387,14 @@ void rgblight_disable(void) { void rgblight_disable_noeeprom(void) { rgblight_config.enable = 0; dprintf("rgblight disable [NOEEPROM]: rgblight_config.enable = %u\n", rgblight_config.enable); -#ifdef RGBLIGHT_USE_TIMER rgblight_timer_disable(); -#endif RGBLIGHT_SPLIT_SET_CHANGE_MODE; wait_ms(50); rgblight_set(); } +bool rgblight_is_enabled(void) { return rgblight_config.enable; } + void rgblight_increase_hue_helper(bool write_to_eeprom) { uint8_t hue = rgblight_config.hue + RGBLIGHT_HUE_STEP; rgblight_sethsv_eeprom_helper(hue, rgblight_config.sat, rgblight_config.val, write_to_eeprom); @@ -431,17 +431,25 @@ void rgblight_decrease_val_helper(bool write_to_eeprom) { } void rgblight_decrease_val_noeeprom(void) { rgblight_decrease_val_helper(false); } void rgblight_decrease_val(void) { rgblight_decrease_val_helper(true); } -void rgblight_increase_speed(void) { + +void rgblight_increase_speed_helper(bool write_to_eeprom) { if (rgblight_config.speed < 3) rgblight_config.speed++; // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED? - eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this + if (write_to_eeprom) { + eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this + } } - -void rgblight_decrease_speed(void) { +void rgblight_increase_speed(void) { rgblight_increase_speed_helper(true); } +void rgblight_increase_speed_noeeprom(void) { rgblight_increase_speed_helper(false); } +void rgblight_decrease_speed_helper(bool write_to_eeprom) { if (rgblight_config.speed > 0) rgblight_config.speed--; // RGBLIGHT_SPLIT_SET_CHANGE_HSVS; // NEED?? - eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this + if (write_to_eeprom) { + eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this + } } +void rgblight_decrease_speed(void) { rgblight_decrease_speed_helper(true); } +void rgblight_decrease_speed_noeeprom(void) { rgblight_decrease_speed_helper(false); } void rgblight_sethsv_noeeprom_old(uint8_t hue, uint8_t sat, uint8_t val) { if (rgblight_config.enable) { @@ -502,15 +510,15 @@ void rgblight_sethsv_eeprom_helper(uint8_t hue, uint8_t sat, uint8_t val, bool w # else uint8_t range = RGBLED_GRADIENT_RANGES[delta / 2]; # endif - for (uint8_t i = 0; i < effect_num_leds; i++) { - uint8_t _hue = ((uint16_t)i * (uint16_t)range) / effect_num_leds; + for (uint8_t i = 0; i < rgblight_ranges.effect_num_leds; i++) { + uint8_t _hue = ((uint16_t)i * (uint16_t)range) / rgblight_ranges.effect_num_leds; if (direction) { _hue = hue + _hue; } else { _hue = hue - _hue; } dprintf("rgblight rainbow set hsv: %d,%d,%d,%u\n", i, _hue, direction, range); - sethsv(_hue, sat, val, (LED_TYPE *)&led[i + effect_start_pos]); + sethsv(_hue, sat, val, (LED_TYPE *)&led[i + rgblight_ranges.effect_start_pos]); } rgblight_set(); } @@ -542,7 +550,7 @@ uint8_t rgblight_get_speed(void) { return rgblight_config.speed; } void rgblight_set_speed_eeprom_helper(uint8_t speed, bool write_to_eeprom) { rgblight_config.speed = speed; if (write_to_eeprom) { - eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this + eeconfig_update_rgblight(rgblight_config.raw); // EECONFIG needs to be increased to support this dprintf("rgblight set speed [EEPROM]: %u\n", rgblight_config.speed); } else { dprintf("rgblight set speed [NOEEPROM]: %u\n", rgblight_config.speed); @@ -559,12 +567,14 @@ uint8_t rgblight_get_sat(void) { return rgblight_config.sat; } uint8_t rgblight_get_val(void) { return rgblight_config.val; } +HSV rgblight_get_hsv(void) { return (HSV){rgblight_config.hue, rgblight_config.sat, rgblight_config.val}; } + void rgblight_setrgb(uint8_t r, uint8_t g, uint8_t b) { if (!rgblight_config.enable) { return; } - for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) { + for (uint8_t i = rgblight_ranges.effect_start_pos; i < rgblight_ranges.effect_end_pos; i++) { led[i].r = r; led[i].g = g; led[i].b = b; @@ -599,7 +609,7 @@ void rgblight_sethsv_at(uint8_t hue, uint8_t sat, uint8_t val, uint8_t index) { rgblight_setrgb_at(tmp_led.r, tmp_led.g, tmp_led.b, index); } -#if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT) +#if defined(RGBLIGHT_EFFECT_BREATHING) || defined(RGBLIGHT_EFFECT_RAINBOW_MOOD) || defined(RGBLIGHT_EFFECT_RAINBOW_SWIRL) || defined(RGBLIGHT_EFFECT_SNAKE) || defined(RGBLIGHT_EFFECT_KNIGHT) || defined(RGBLIGHT_EFFECT_TWINKLE) static uint8_t get_interval_time(const uint8_t *default_interval_address, uint8_t velocikey_min, uint8_t velocikey_max) { return @@ -648,13 +658,97 @@ void rgblight_sethsv_master(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_se void rgblight_sethsv_slave(uint8_t hue, uint8_t sat, uint8_t val) { rgblight_sethsv_range(hue, sat, val, (uint8_t)RGBLED_NUM / 2, (uint8_t)RGBLED_NUM); } #endif // ifndef RGBLIGHT_SPLIT +#ifdef RGBLIGHT_LAYERS +void rgblight_set_layer_state(uint8_t layer, bool enabled) { + rgblight_layer_mask_t mask = 1 << layer; + if (enabled) { + rgblight_status.enabled_layer_mask |= mask; + } else { + rgblight_status.enabled_layer_mask &= ~mask; + } + RGBLIGHT_SPLIT_SET_CHANGE_LAYERS; + // Static modes don't have a ticker running to update the LEDs + if (rgblight_status.timer_enabled == false) { + rgblight_mode_noeeprom(rgblight_config.mode); + } + +# ifdef RGBLIGHT_LAYERS_OVERRIDE_RGB_OFF + // If not enabled, then nothing else will actually set the LEDs... + if (!rgblight_config.enable) { + rgblight_set(); + } +# endif +} + +bool rgblight_get_layer_state(uint8_t layer) { + rgblight_layer_mask_t mask = 1 << layer; + return (rgblight_status.enabled_layer_mask & mask) != 0; +} + +// Write any enabled LED layers into the buffer +static void rgblight_layers_write(void) { + uint8_t i = 0; + // For each layer + for (const rgblight_segment_t *const *layer_ptr = rgblight_layers; i < RGBLIGHT_MAX_LAYERS; layer_ptr++, i++) { + if (!rgblight_get_layer_state(i)) { + continue; // Layer is disabled + } + const rgblight_segment_t *segment_ptr = pgm_read_ptr(layer_ptr); + if (segment_ptr == NULL) { + break; // No more layers + } + // For each segment + while (1) { + rgblight_segment_t segment; + memcpy_P(&segment, segment_ptr, sizeof(rgblight_segment_t)); + if (segment.index == RGBLIGHT_END_SEGMENT_INDEX) { + break; // No more segments + } + // Write segment.count LEDs + LED_TYPE *const limit = &led[MIN(segment.index + segment.count, RGBLED_NUM)]; + for (LED_TYPE *led_ptr = &led[segment.index]; led_ptr < limit; led_ptr++) { + sethsv(segment.hue, segment.sat, segment.val, led_ptr); + } + segment_ptr++; + } + } +} + +# ifdef RGBLIGHT_LAYER_BLINK +rgblight_layer_mask_t _blinked_layer_mask = 0; +uint16_t _blink_duration = 0; +static uint16_t _blink_timer; + +void rgblight_blink_layer(uint8_t layer, uint16_t duration_ms) { + rgblight_set_layer_state(layer, true); + _blinked_layer_mask |= 1 << layer; + _blink_timer = timer_read(); + _blink_duration = duration_ms; +} + +void rgblight_unblink_layers(void) { + if (_blinked_layer_mask != 0 && timer_elapsed(_blink_timer) > _blink_duration) { + for (uint8_t layer = 0; layer < RGBLIGHT_MAX_LAYERS; layer++) { + if ((_blinked_layer_mask & 1 << layer) != 0) { + rgblight_set_layer_state(layer, false); + } + } + _blinked_layer_mask = 0; + } +} +# endif + +#endif + +__attribute__((weak)) void rgblight_call_driver(LED_TYPE *start_led, uint8_t num_leds) { ws2812_setleds(start_led, num_leds); } + #ifndef RGBLIGHT_CUSTOM_DRIVER void rgblight_set(void) { LED_TYPE *start_led; - uint16_t num_leds = clipping_num_leds; + uint8_t num_leds = rgblight_ranges.clipping_num_leds; if (!rgblight_config.enable) { - for (uint8_t i = effect_start_pos; i < effect_end_pos; i++) { + for (uint8_t i = rgblight_ranges.effect_start_pos; i < rgblight_ranges.effect_end_pos; i++) { if (i == RGBLIGHT_FLED1 && i == RGBLIGHT_FLED2) continue; @@ -667,54 +761,63 @@ void rgblight_set(void) { } } +# ifdef RGBLIGHT_LAYERS + if (rgblight_layers != NULL +# ifndef RGBLIGHT_LAYERS_OVERRIDE_RGB_OFF + && rgblight_config.enable +# endif + ) { + rgblight_layers_write(); + } +# endif + # ifdef RGBLIGHT_LED_MAP LED_TYPE led0[RGBLED_NUM]; for (uint8_t i = 0; i < RGBLED_NUM; i++) { led0[i] = led[pgm_read_byte(&led_map[i])]; } - start_led = led0 + clipping_start_pos; + start_led = led0 + rgblight_ranges.clipping_start_pos; # else - start_led = led + clipping_start_pos; + start_led = led + rgblight_ranges.clipping_start_pos; # endif -#ifdef RGBW +# ifdef RGBW for (uint8_t i = 0; i < num_leds; i++) { convert_rgb_to_rgbw(&start_led[i]); } -#endif +# endif // MxSS custom switch (fled_mode) { - case FLED_OFF: - setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]); - setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]); - break; - - case FLED_INDI: - copyrgb(&fleds[0], &led[RGBLIGHT_FLED1]); - copyrgb(&fleds[1], &led[RGBLIGHT_FLED2]); - break; - - case FLED_RGB: - if (fled_hs[0].hue == 0 && fled_hs[0].hue == 0 && - (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE || - rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT)) + case FLED_OFF: setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]); - else - sethsv(fled_hs[0].hue, fled_hs[0].sat, fled_val, &led[RGBLIGHT_FLED1]); + setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]); + break; + + case FLED_INDI: + copyrgb(&fleds[0], &led[RGBLIGHT_FLED1]); + copyrgb(&fleds[1], &led[RGBLIGHT_FLED2]); + break; - if (fled_hs[1].hue == 0 && fled_hs[1].hue == 0 && + case FLED_RGB: + if (fled_hs[0].hue == 0 && fled_hs[0].hue == 0 && (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE || rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT)) - setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]); - else - sethsv(fled_hs[1].hue, fled_hs[1].sat, fled_val, &led[RGBLIGHT_FLED2]); - break; + setrgb(0, 0, 0, &led[RGBLIGHT_FLED1]); + else + sethsv(fled_hs[0].hue, fled_hs[0].sat, fled_val, &led[RGBLIGHT_FLED1]); - default: - break; - } + if (fled_hs[1].hue == 0 && fled_hs[1].hue == 0 && + (rgblight_status.base_mode == RGBLIGHT_MODE_SNAKE || + rgblight_status.base_mode == RGBLIGHT_MODE_KNIGHT)) + setrgb(0, 0, 0, &led[RGBLIGHT_FLED2]); + else + sethsv(fled_hs[1].hue, fled_hs[1].sat, fled_val, &led[RGBLIGHT_FLED2]); + break; + default: + break; + } - ws2812_setleds(start_led, num_leds); + rgblight_call_driver(start_led, num_leds); } #endif @@ -731,6 +834,11 @@ void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo) { /* for split keyboard slave side */ void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom) { +# ifdef RGBLIGHT_LAYERS + if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_LAYERS) { + rgblight_status.enabled_layer_mask = syncinfo->status.enabled_layer_mask; + } +# endif if (syncinfo->status.change_flags & RGBLIGHT_STATUS_CHANGE_MODE) { if (syncinfo->config.enable) { rgblight_config.enable = 1; // == rgblight_enable_noeeprom(); @@ -893,6 +1001,12 @@ void rgblight_task(void) { effect_func = (effect_func_t)rgblight_effect_alternating; } # endif +# ifdef RGBLIGHT_EFFECT_TWINKLE + else if (rgblight_status.base_mode == RGBLIGHT_MODE_TWINKLE) { + interval_time = get_interval_time(&RGBLED_TWINKLE_INTERVALS[delta % 3], 5, 50); + effect_func = (effect_func_t)rgblight_effect_twinkle; + } +# endif if (animation_status.restart) { animation_status.restart = false; animation_status.last_timer = timer_read() - interval_time - 1; @@ -922,6 +1036,10 @@ void rgblight_task(void) { # endif } } + +# ifdef RGBLIGHT_LAYER_BLINK + rgblight_unblink_layers(); +# endif } #endif /* RGBLIGHT_USE_TIMER */ @@ -972,9 +1090,9 @@ void rgblight_effect_rainbow_swirl(animation_status_t *anim) { uint8_t hue; uint8_t i; - for (i = 0; i < effect_num_leds; i++) { - hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / effect_num_leds * i + anim->current_hue); - sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]); + for (i = 0; i < rgblight_ranges.effect_num_leds; i++) { + hue = (RGBLIGHT_RAINBOW_SWIRL_RANGE / rgblight_ranges.effect_num_leds * i + anim->current_hue); + sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + rgblight_ranges.effect_start_pos]); } rgblight_set(); @@ -1002,7 +1120,7 @@ void rgblight_effect_snake(animation_status_t *anim) { # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) if (anim->pos == 0) { // restart signal if (increment == 1) { - pos = effect_num_leds - 1; + pos = rgblight_ranges.effect_num_leds - 1; } else { pos = 0; } @@ -1014,8 +1132,8 @@ void rgblight_effect_snake(animation_status_t *anim) { fled_hs[0].hue = fled_hs[1].hue = 0; fled_hs[0].sat = fled_hs[1].sat = 0; - for (i = 0; i < effect_num_leds; i++) { - LED_TYPE *ledp = led + i + effect_start_pos; + for (i = 0; i < rgblight_ranges.effect_num_leds; i++) { + LED_TYPE *ledp = led + i + rgblight_ranges.effect_start_pos; ledp->r = 0; ledp->g = 0; ledp->b = 0; @@ -1028,7 +1146,7 @@ void rgblight_effect_snake(animation_status_t *anim) { k = k % RGBLED_NUM; } if (k < 0) { - k = k + effect_num_leds; + k = k + rgblight_ranges.effect_num_leds; } if (i == k) { sethsv(rgblight_config.hue, rgblight_config.sat, (uint8_t)(rgblight_config.val * (RGBLIGHT_EFFECT_SNAKE_LENGTH - j) / RGBLIGHT_EFFECT_SNAKE_LENGTH), ledp); @@ -1038,7 +1156,7 @@ void rgblight_effect_snake(animation_status_t *anim) { rgblight_set(); if (increment == 1) { if (pos - 1 < 0) { - pos = effect_num_leds - 1; + pos = rgblight_ranges.effect_num_leds - 1; # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) anim->pos = 0; # endif @@ -1049,7 +1167,7 @@ void rgblight_effect_snake(animation_status_t *anim) { # endif } } else { - pos = (pos + 1) % effect_num_leds; + pos = (pos + 1) % rgblight_ranges.effect_num_leds; # if defined(RGBLIGHT_SPLIT) && !defined(RGBLIGHT_SPLIT_NO_ANIMATION_SYNC) anim->pos = pos; # endif @@ -1075,7 +1193,7 @@ void rgblight_effect_knight(animation_status_t *anim) { } # endif // Set all the LEDs to 0 - for (i = effect_start_pos; i < effect_end_pos; i++) { + for (i = rgblight_ranges.effect_start_pos; i < rgblight_ranges.effect_end_pos; i++) { led[i].r = 0; led[i].g = 0; led[i].b = 0; @@ -1085,7 +1203,7 @@ void rgblight_effect_knight(animation_status_t *anim) { } // Determine which LEDs should be lit up for (i = 0; i < RGBLIGHT_EFFECT_KNIGHT_LED_NUM; i++) { - cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % effect_num_leds + effect_start_pos; + cur = (i + RGBLIGHT_EFFECT_KNIGHT_OFFSET) % rgblight_ranges.effect_num_leds + rgblight_ranges.effect_start_pos; if (i >= low_bound && i <= high_bound) { sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[cur]); @@ -1124,16 +1242,39 @@ void rgblight_effect_knight(animation_status_t *anim) { #endif #ifdef RGBLIGHT_EFFECT_CHRISTMAS -void rgblight_effect_christmas(animation_status_t *anim) { - uint8_t hue; - uint8_t i; +# define CUBED(x) ((x) * (x) * (x)) - anim->current_offset = (anim->current_offset + 1) % 2; - for (i = 0; i < effect_num_leds; i++) { - hue = 0 + ((i / RGBLIGHT_EFFECT_CHRISTMAS_STEP + anim->current_offset) % 2) * 85; - sethsv(hue, rgblight_config.sat, rgblight_config.val, (LED_TYPE *)&led[i + effect_start_pos]); +/** + * Christmas lights effect, with a smooth animation between red & green. + */ +void rgblight_effect_christmas(animation_status_t *anim) { + static int8_t increment = 1; + const uint8_t max_pos = 32; + const uint8_t hue_green = 85; + + uint32_t xa; + uint8_t hue, val; + uint8_t i; + + // The effect works by animating anim->pos from 0 to 32 and back to 0. + // The pos is used in a cubic bezier formula to ease-in-out between red and green, leaving the interpolated colors visible as short as possible. + xa = CUBED((uint32_t)anim->pos); + hue = ((uint32_t)hue_green) * xa / (xa + CUBED((uint32_t)(max_pos - anim->pos))); + // Additionally, these interpolated colors get shown with a slightly darker value, to make them less prominent than the main colors. + val = 255 - (3 * (hue < hue_green / 2 ? hue : hue_green - hue) / 2); + + for (i = 0; i < rgblight_ranges.effect_num_leds; i++) { + uint8_t local_hue = (i / RGBLIGHT_EFFECT_CHRISTMAS_STEP) % 2 ? hue : hue_green - hue; + sethsv(local_hue, rgblight_config.sat, val, (LED_TYPE *)&led[i + rgblight_ranges.effect_start_pos]); } rgblight_set(); + + if (anim->pos == 0) { + increment = 1; + } else if (anim->pos == max_pos) { + increment = -1; + } + anim->pos += increment; } #endif @@ -1183,11 +1324,11 @@ void rgblight_effect_rgbtest(animation_status_t *anim) { #ifdef RGBLIGHT_EFFECT_ALTERNATING void rgblight_effect_alternating(animation_status_t *anim) { - for (int i = 0; i < effect_num_leds; i++) { - LED_TYPE *ledp = led + i + effect_start_pos; - if (i < effect_num_leds / 2 && anim->pos) { + for (int i = 0; i < rgblight_ranges.effect_num_leds; i++) { + LED_TYPE *ledp = led + i + rgblight_ranges.effect_start_pos; + if (i < rgblight_ranges.effect_num_leds / 2 && anim->pos) { sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp); - } else if (i >= effect_num_leds / 2 && !anim->pos) { + } else if (i >= rgblight_ranges.effect_num_leds / 2 && !anim->pos) { sethsv(rgblight_config.hue, rgblight_config.sat, rgblight_config.val, ledp); } else { sethsv(rgblight_config.hue, rgblight_config.sat, 0, ledp); @@ -1197,3 +1338,58 @@ void rgblight_effect_alternating(animation_status_t *anim) { anim->pos = (anim->pos + 1) % 2; } #endif + +#ifdef RGBLIGHT_EFFECT_TWINKLE +__attribute__((weak)) const uint8_t RGBLED_TWINKLE_INTERVALS[] PROGMEM = {50, 25, 10}; + +typedef struct PACKED { + HSV hsv; + uint8_t life; + bool up; +} TwinkleState; + +static TwinkleState led_twinkle_state[RGBLED_NUM]; + +void rgblight_effect_twinkle(animation_status_t *anim) { + bool random_color = anim->delta / 3; + bool restart = anim->pos == 0; + anim->pos = 1; + + for (uint8_t i = 0; i < rgblight_ranges.effect_num_leds; i++) { + TwinkleState *t = &(led_twinkle_state[i]); + HSV * c = &(t->hsv); + if (restart) { + // Restart + t->life = 0; + t->hsv.v = 0; + } else if (t->life) { + // This LED is already on, either brightening or dimming + t->life--; + uint8_t on = t->up ? RGBLIGHT_EFFECT_TWINKLE_LIFE - t->life : t->life; + c->v = (uint16_t)rgblight_config.val * on / RGBLIGHT_EFFECT_TWINKLE_LIFE; + if (t->life == 0 && t->up) { + t->up = false; + t->life = RGBLIGHT_EFFECT_TWINKLE_LIFE; + } + if (!random_color) { + c->h = rgblight_config.hue; + c->s = rgblight_config.sat; + } + } else if (rand() < RAND_MAX * RGBLIGHT_EFFECT_TWINKLE_PROBABILITY) { + // This LED is off, but was randomly selected to start brightening + c->h = random_color ? rand() % 0xFF : rgblight_config.hue; + c->s = random_color ? (rand() % (rgblight_config.sat / 2)) + (rgblight_config.sat / 2) : rgblight_config.sat; + c->v = 0; + t->life = RGBLIGHT_EFFECT_TWINKLE_LIFE; + t->up = true; + } else { + // This LED is off, and was NOT selected to start brightening + } + + LED_TYPE *ledp = led + i + rgblight_ranges.effect_start_pos; + sethsv(c->h, c->s, c->v, ledp); + } + + rgblight_set(); +} +#endif |