From 5f2b065dad5054b0301ec2fecba47f66818c4fd7 Mon Sep 17 00:00:00 2001 From: Chris Lewis Date: Fri, 27 Jul 2018 00:23:44 +1000 Subject: [PATCH] Support all RGB animation modes (Fixes #1) * Added support for all RGB light modes to use typing speed Except christmas lights because that is seizure-inducing at high speeds! * Introduced a value range specific to each RGB mode Because some modes are a little too much when running at full speed! --- quantum/quantum.c | 2 +- quantum/quantum.h | 1 + quantum/rgblight.c | 36 ++++++++++++++++++++++-------------- quantum/rgblight.h | 3 +++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/quantum/quantum.c b/quantum/quantum.c index 1a5d992818..30a7beba40 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -194,7 +194,7 @@ bool process_record_quantum(keyrecord_t *record) { keypos_t key = record->event.key; uint16_t keycode; - if (typing_speed < 100) typing_speed += 1; + if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += 1; #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS) /* TODO: Use store_or_get_action() or a similar function. */ diff --git a/quantum/quantum.h b/quantum/quantum.h index 35822f9a4f..1837caddbb 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -57,6 +57,7 @@ extern uint32_t default_layer_state; //Used in rgblight.c to match RGB animation to typing speed extern uint8_t typing_speed; +#define TYPING_SPEED_MAX_VALUE 100 #ifndef NO_ACTION_LAYER extern uint32_t layer_state; diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 548c84a74e..fda64855d1 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -569,8 +569,25 @@ void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) { rgblight_setrgb(r, g, b); } +void typing_speed_decay_task() { + static uint16_t decay_timer = 0; + + if (timer_elapsed(decay_timer) > 500 || decay_timer == 0) { + if (typing_speed > 0) typing_speed -= 1; + decay_timer = timer_read(); + } +} + +uint8_t typing_speed_matched_interval(uint8_t minValue, uint8_t maxValue) { + return MAX(minValue, maxValue - (maxValue - minValue) * ((float)typing_speed / TYPING_SPEED_MAX_VALUE)); +} + void rgblight_task(void) { + if (rgblight_timer_enabled) { + + typing_speed_decay_task(); + // mode = 1, static light, do nothing here if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { // mode = 2 to 5, breathing mode @@ -603,7 +620,7 @@ void rgblight_effect_breathing(uint8_t interval) { static uint16_t last_timer = 0; float val; - if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_BREATHING_INTERVALS[interval])) { + if (timer_elapsed(last_timer) < typing_speed_matched_interval(1, 100)) { return; } last_timer = timer_read(); @@ -618,7 +635,7 @@ void rgblight_effect_rainbow_mood(uint8_t interval) { static uint16_t current_hue = 0; static uint16_t last_timer = 0; - if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_RAINBOW_MOOD_INTERVALS[interval])) { + if (timer_elapsed(last_timer) < typing_speed_matched_interval(5, 100)) { return; } last_timer = timer_read(); @@ -631,16 +648,7 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) { uint16_t hue; uint8_t i; - //Improvement: move this code into rgblight_task() so that the typing_speed can be used across all RGB effects, not just swirl - static uint16_t decay_timer = 0; - if (timer_elapsed(decay_timer) > 250 || decay_timer == 0) { - if (typing_speed > 0) typing_speed -= 1; - //Improvement(?): decay by a greater rate depending on how big typing_speed is, so you can't reach max speed just by outpacing the regular decay - decay_timer = timer_read(); - } - - //Improvement: make the usage of typing speed more easily configurable, either with a pre-processor toggle or with real-time key toggles - if (timer_elapsed(last_timer) < MAX(1, 100 - typing_speed)) { + if (timer_elapsed(last_timer) < typing_speed_matched_interval(1, 100)) { return; } last_timer = timer_read(); @@ -669,7 +677,7 @@ void rgblight_effect_snake(uint8_t interval) { if (interval % 2) { increment = -1; } - if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_SNAKE_INTERVALS[interval / 2])) { + if (timer_elapsed(last_timer) < typing_speed_matched_interval(5, 100)) { return; } last_timer = timer_read(); @@ -700,7 +708,7 @@ void rgblight_effect_snake(uint8_t interval) { } void rgblight_effect_knight(uint8_t interval) { static uint16_t last_timer = 0; - if (timer_elapsed(last_timer) < pgm_read_byte(&RGBLED_KNIGHT_INTERVALS[interval])) { + if (timer_elapsed(last_timer) < typing_speed_matched_interval(5, 100)) { return; } last_timer = timer_read(); diff --git a/quantum/rgblight.h b/quantum/rgblight.h index 0f7b5ffb56..ecc1ff6058 100644 --- a/quantum/rgblight.h +++ b/quantum/rgblight.h @@ -167,4 +167,7 @@ void rgblight_effect_knight(uint8_t interval); void rgblight_effect_christmas(void); void rgblight_effect_rgbtest(void); +void typing_speed_decay_task(void); +uint8_t typing_speed_matched_interval(uint8_t minValue, uint8_t maxValue); + #endif