velocikey.c 1.3 KB

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