diff --git a/common_features.mk b/common_features.mk index b78f04d2a82..dd215071963 100644 --- a/common_features.mk +++ b/common_features.mk @@ -105,6 +105,7 @@ endif ifeq ($(strip $(RGBLIGHT_ENABLE)), yes) OPT_DEFS += -DRGBLIGHT_ENABLE SRC += $(QUANTUM_DIR)/rgblight.c + SRC += $(QUANTUM_DIR)/momentum.c CIE1931_CURVE = yes LED_BREATHING_TABLE = yes ifeq ($(strip $(RGBLIGHT_CUSTOM_DRIVER)), yes) diff --git a/quantum/momentum.c b/quantum/momentum.c new file mode 100644 index 00000000000..e0fadc991c1 --- /dev/null +++ b/quantum/momentum.c @@ -0,0 +1,32 @@ +#include "momentum.h" + +#ifndef MIN +#define MIN(a,b) (((a)<(b))?(a):(b)) +#endif +#ifndef MAX +#define MAX(a,b) (((a)>(b))?(a):(b)) +#endif + +#define TYPING_SPEED_MAX_VALUE 200 +uint8_t typing_speed = 0; + +void momentum_accelerate() { + if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += (TYPING_SPEED_MAX_VALUE / 100); +} + +void momentum_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 a little faster at half of max speed + if (typing_speed > TYPING_SPEED_MAX_VALUE / 2) typing_speed -= 1; + //Decay even faster at 3/4 of max speed + if (typing_speed > TYPING_SPEED_MAX_VALUE / 4 * 3) typing_speed -= 3; + decay_timer = timer_read(); + } +} + +uint8_t match_momentum(uint8_t minValue, uint8_t maxValue) { + return MAX(minValue, maxValue - (maxValue - minValue) * ((float)typing_speed / TYPING_SPEED_MAX_VALUE)); +} \ No newline at end of file diff --git a/quantum/momentum.h b/quantum/momentum.h new file mode 100644 index 00000000000..7acf139255a --- /dev/null +++ b/quantum/momentum.h @@ -0,0 +1,11 @@ +#ifndef MOMENTUM_H +#define MOMENTUM_H + +#include +#include "timer.h" + +void momentum_accelerate(void); +void momentum_decay_task(void); +uint8_t match_momentum(uint8_t minValue, uint8_t maxValue); + +#endif \ No newline at end of file diff --git a/quantum/quantum.c b/quantum/quantum.c index 9c44c07561b..ea2300ebd08 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -194,7 +194,7 @@ bool process_record_quantum(keyrecord_t *record) { uint16_t keycode; #ifdef RGBLIGHT_ENABLE - if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += (TYPING_SPEED_MAX_VALUE / 100); + momentum_accelerate(); #endif #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS) diff --git a/quantum/rgblight.c b/quantum/rgblight.c index 000d91e9e79..022fe21f169 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -567,29 +567,11 @@ void rgblight_show_solid_color(uint8_t r, uint8_t g, uint8_t b) { rgblight_setrgb(r, g, b); } -uint8_t typing_speed = 0; -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 a little faster at half of max speed - if (typing_speed > TYPING_SPEED_MAX_VALUE / 2) typing_speed -= 1; - //Decay even faster at 3/4 of max speed - if (typing_speed > TYPING_SPEED_MAX_VALUE / 4 * 3) typing_speed -= 3; - 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(); + momentum_decay_task(); // mode = 1, static light, do nothing here if (rgblight_config.mode >= 2 && rgblight_config.mode <= 5) { @@ -623,7 +605,7 @@ void rgblight_effect_breathing(uint8_t interval) { static uint16_t last_timer = 0; float val; - if (timer_elapsed(last_timer) < typing_speed_matched_interval(1, 100)) { + if (timer_elapsed(last_timer) < match_momentum(1, 100)) { return; } last_timer = timer_read(); @@ -638,7 +620,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) < typing_speed_matched_interval(5, 100)) { + if (timer_elapsed(last_timer) < match_momentum(5, 100)) { return; } last_timer = timer_read(); @@ -651,7 +633,7 @@ void rgblight_effect_rainbow_swirl(uint8_t interval) { uint16_t hue; uint8_t i; - if (timer_elapsed(last_timer) < typing_speed_matched_interval(1, 100)) { + if (timer_elapsed(last_timer) < match_momentum(1, 100)) { return; } last_timer = timer_read(); @@ -680,7 +662,7 @@ void rgblight_effect_snake(uint8_t interval) { if (interval % 2) { increment = -1; } - if (timer_elapsed(last_timer) < typing_speed_matched_interval(1, 200)) { + if (timer_elapsed(last_timer) < match_momentum(1, 200)) { return; } last_timer = timer_read(); @@ -711,7 +693,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) < typing_speed_matched_interval(5, 100)) { + if (timer_elapsed(last_timer) < match_momentum(5, 100)) { return; } last_timer = timer_read(); diff --git a/quantum/rgblight.h b/quantum/rgblight.h index b3264dff1f7..67bab7eb57b 100644 --- a/quantum/rgblight.h +++ b/quantum/rgblight.h @@ -80,6 +80,8 @@ #include #endif +#include "momentum.h" + extern LED_TYPE led[RGBLED_NUM]; extern const uint8_t RGBLED_BREATHING_INTERVALS[4] PROGMEM; @@ -89,10 +91,6 @@ extern const uint8_t RGBLED_SNAKE_INTERVALS[3] PROGMEM; extern const uint8_t RGBLED_KNIGHT_INTERVALS[3] PROGMEM; extern const uint16_t RGBLED_RGBTEST_INTERVALS[1] PROGMEM; -//Used in rgblight.c and quantum.c to match RGB animation to typing speed -extern uint8_t typing_speed; -#define TYPING_SPEED_MAX_VALUE 200 - typedef union { uint32_t raw; struct { @@ -171,7 +169,4 @@ 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