velocikey.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "velocikey.h"
  2. #include "timer.h"
  3. #include "eeconfig.h"
  4. #include "eeprom.h"
  5. #include "util.h"
  6. #define TYPING_SPEED_MAX_VALUE 200
  7. uint8_t typing_speed = 0;
  8. bool velocikey_enabled(void) {
  9. return eeprom_read_byte(EECONFIG_VELOCIKEY) == 1;
  10. }
  11. void velocikey_toggle(void) {
  12. if (velocikey_enabled())
  13. eeprom_update_byte(EECONFIG_VELOCIKEY, 0);
  14. else
  15. eeprom_update_byte(EECONFIG_VELOCIKEY, 1);
  16. }
  17. void velocikey_accelerate(void) {
  18. if (typing_speed < TYPING_SPEED_MAX_VALUE) typing_speed += (TYPING_SPEED_MAX_VALUE / 100);
  19. }
  20. void velocikey_decelerate(void) {
  21. static uint16_t decay_timer = 0;
  22. if (timer_elapsed(decay_timer) > 500 || decay_timer == 0) {
  23. if (typing_speed > 0) typing_speed -= 1;
  24. // Decay a little faster at half of max speed
  25. if (typing_speed > TYPING_SPEED_MAX_VALUE / 2) typing_speed -= 1;
  26. // Decay even faster at 3/4 of max speed
  27. if (typing_speed > TYPING_SPEED_MAX_VALUE / 4 * 3) typing_speed -= 2;
  28. decay_timer = timer_read();
  29. }
  30. }
  31. uint8_t velocikey_match_speed(uint8_t minValue, uint8_t maxValue) {
  32. return MAX(minValue, maxValue - (maxValue - minValue) * ((float)typing_speed / TYPING_SPEED_MAX_VALUE));
  33. }