Relocate grave keycode processing (#8082)
[jackhill/qmk/firmware.git] / quantum / velocikey.c
CommitLineData
c1c5922a
CL
1#include "velocikey.h"
2#include "timer.h"
3#include "eeconfig.h"
4#include "eeprom.h"
5
6#ifndef MIN
b624f32f 7# define MIN(a, b) (((a) < (b)) ? (a) : (b))
c1c5922a
CL
8#endif
9#ifndef MAX
b624f32f 10# define MAX(a, b) (((a) > (b)) ? (a) : (b))
c1c5922a
CL
11#endif
12
13#define TYPING_SPEED_MAX_VALUE 200
14uint8_t typing_speed = 0;
15
b624f32f 16bool velocikey_enabled(void) { return eeprom_read_byte(EECONFIG_VELOCIKEY) == 1; }
c1c5922a
CL
17
18void velocikey_toggle(void) {
b624f32f 19 if (velocikey_enabled())
c1c5922a 20 eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
b624f32f 21 else
c1c5922a
CL
22 eeprom_update_byte(EECONFIG_VELOCIKEY, 1);
23}
24
25void velocikey_accelerate(void) {
26 if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += (TYPING_SPEED_MAX_VALUE / 100);
27}
28
29void velocikey_decelerate(void) {
b624f32f 30 static uint16_t decay_timer = 0;
31
32 if (timer_elapsed(decay_timer) > 500 || decay_timer == 0) {
33 if (typing_speed > 0) typing_speed -= 1;
34 // Decay a little faster at half of max speed
35 if (typing_speed > TYPING_SPEED_MAX_VALUE / 2) typing_speed -= 1;
36 // Decay even faster at 3/4 of max speed
37 if (typing_speed > TYPING_SPEED_MAX_VALUE / 4 * 3) typing_speed -= 2;
38 decay_timer = timer_read();
39 }
c1c5922a
CL
40}
41
b624f32f 42uint8_t velocikey_match_speed(uint8_t minValue, uint8_t maxValue) { return MAX(minValue, maxValue - (maxValue - minValue) * ((float)typing_speed / TYPING_SPEED_MAX_VALUE)); }