diff --git a/common_features.mk b/common_features.mk index 0cc26109a92..173af4dea53 100644 --- a/common_features.mk +++ b/common_features.mk @@ -236,7 +236,8 @@ QUANTUM_SRC:= \ $(QUANTUM_DIR)/quantum.c \ $(QUANTUM_DIR)/keymap_common.c \ $(QUANTUM_DIR)/keycode_config.c \ - $(QUANTUM_DIR)/process_keycode/process_leader.c + $(QUANTUM_DIR)/process_keycode/process_leader.c \ + $(QUANTUM_DIR)/momentum.c ifndef CUSTOM_MATRIX ifeq ($(strip $(SPLIT_KEYBOARD)), yes) diff --git a/quantum/momentum.c b/quantum/momentum.c new file mode 100644 index 00000000000..662f71d51c8 --- /dev/null +++ b/quantum/momentum.c @@ -0,0 +1,46 @@ +#include "momentum.h" +#include "timer.h" +#include "eeconfig.h" +#include "eeprom.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; + +bool momentum_enabled() { + return eeprom_read_byte(EECONFIG_MOMENTUM) == 1; +} + +void momentum_toggle() { + if (momentum_enabled()) + eeprom_update_byte(EECONFIG_MOMENTUM, 0); + else + eeprom_update_byte(EECONFIG_MOMENTUM, 1); +} + +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..1fe4c58aea3 --- /dev/null +++ b/quantum/momentum.h @@ -0,0 +1,13 @@ +#ifndef MOMENTUM_H +#define MOMENTUM_H + +#include +#include + +bool momentum_enabled(void); +void momentum_toggle(void); +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 8a4344986d5..130974c34bc 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -1201,7 +1201,10 @@ static inline uint16_t scale_backlight(uint16_t v) { */ ISR(TIMER1_OVF_vect) { - uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS; + uint16_t interval = momentum_enabled() + ? match_momentum(1, 10) + : (uint16_t) breathing_period * 244 / BREATHING_STEPS; + // resetting after one period to prevent ugly reset at overflow. breathing_counter = (breathing_counter + 1) % (breathing_period * 244); uint8_t index = breathing_counter / interval % BREATHING_STEPS; diff --git a/readme.md b/readme.md index 6e6cfaa1bd1..8673f66f79d 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,22 @@ -# Quantum Mechanical Keyboard Firmware +# Quantum Mechanical Keyboard Firmware - chrislewisdev fork + +## Typing Speed -> RGB Animation Control + +This fork of qmk_firmware contains the code I whipped up to make your keyboard's RGB animation speed match your typing speed. As of writing, this is a "first draft" version, aka the simplest implementation I could think of with the quickest/hackiest code. Beware hard-coding :) + +Regardless, I'm happy to share the code and discuss improvements with anyone who'd like to contribute. I'll do my best to facilitate it in my spare time. + +## Getting Started + +My original change amounts to several lines in `quantum.h`, `quantum.c` and `rgblight.c`. To see the details it's probably easiest if you look at [this commit](https://github.com/chrislewisdev/qmk_firmware/commit/2d3fbc5d0ad70309ede5cdeb9cf84380fd69baae) which contains all the changes. + +I've created GitHub Issues for the most pressing things that need to be addressed before this could be merged into QMK - if you're interested in helping out, please do take a look! + +To test it, I've just been using my DZ60 keyboard, building the firmware with `make dz60:default` and flashing with qmk_toolbox. If you're not familiar with how to do that, it's probably best you consult the [QMK documentation](https://docs.qmk.fm/#/). + +Below is the original QMK readme: + +# QMK [![Current Version](https://img.shields.io/github/tag/qmk/qmk_firmware.svg)](https://github.com/qmk/qmk_firmware/tags) [![Build Status](https://travis-ci.org/qmk/qmk_firmware.svg?branch=master)](https://travis-ci.org/qmk/qmk_firmware)