diff --git a/src/Keyboardio-MouseKeys.cpp b/src/Keyboardio-MouseKeys.cpp index 702af5b6..7c05490d 100644 --- a/src/Keyboardio-MouseKeys.cpp +++ b/src/Keyboardio-MouseKeys.cpp @@ -5,8 +5,15 @@ #include "KeyboardioFirmware.h" uint8_t MouseKeys_::mouseMoveIntent; -uint8_t MouseKeys_::accelDelay; -uint8_t MouseKeys_::accelDelayCounter; + +uint8_t MouseKeys_::speed = 1; +uint16_t MouseKeys_::speedDelay = 0; + +uint8_t MouseKeys_::accelSpeed = 1; +uint16_t MouseKeys_::accelDelay = 50; + +uint32_t MouseKeys_::accelStartTime; +uint32_t MouseKeys_::startTime; void MouseKeys_::loopHook(bool postClear) { if (postClear) { @@ -15,28 +22,33 @@ void MouseKeys_::loopHook(bool postClear) { } if (mouseMoveIntent == 0) { - MouseWrapper.mouseActiveForCycles = 0; + MouseWrapper.accelStep = 0; + startTime = 0; + accelStartTime = 0; return; } + if ((millis() - startTime) < speedDelay) + return; + + startTime = millis(); + int8_t moveX = 0, moveY = 0; - if (accelDelayCounter == accelDelay) { - if (MouseWrapper.mouseActiveForCycles < 255) - MouseWrapper.mouseActiveForCycles++; - accelDelayCounter = 0; - } else - accelDelayCounter++; + if ((millis() - accelStartTime) >= (accelDelay * MouseWrapper.accelStep)) { + if (MouseWrapper.accelStep < 255 - accelSpeed) + MouseWrapper.accelStep += accelSpeed; + } if (mouseMoveIntent & KEY_MOUSE_UP) - moveY = -1; + moveY = -speed; else if (mouseMoveIntent & KEY_MOUSE_DOWN) - moveY = 1; + moveY = speed; if (mouseMoveIntent & KEY_MOUSE_LEFT) - moveX = -1; + moveX = -speed; else if (mouseMoveIntent & KEY_MOUSE_RIGHT) - moveX = 1; + moveX = speed; MouseWrapper.move(moveX, moveY); } @@ -54,6 +66,12 @@ Key MouseKeys_::eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyS MouseWrapper.release_button(button); } } else if (!(mappedKey.keyCode & KEY_MOUSE_WARP)) { + if (key_toggled_on(keyState)) { + if (!startTime) + startTime = millis(); + if (!accelStartTime) + accelStartTime = millis(); + } if (key_is_pressed(keyState)) mouseMoveIntent |= mappedKey.keyCode; } else if (key_toggled_on(keyState)) { diff --git a/src/Keyboardio-MouseKeys.h b/src/Keyboardio-MouseKeys.h index ff42ea07..7bc805ed 100644 --- a/src/Keyboardio-MouseKeys.h +++ b/src/Keyboardio-MouseKeys.h @@ -8,11 +8,16 @@ class MouseKeys_ : public KeyboardioPlugin { MouseKeys_ (void); virtual void begin(void) final; - static uint8_t accelDelay; + + static uint8_t speed; + static uint16_t speedDelay; + static uint8_t accelSpeed; + static uint16_t accelDelay; private: static uint8_t mouseMoveIntent; - static uint8_t accelDelayCounter; + static uint32_t startTime; + static uint32_t accelStartTime; static void loopHook(bool postClear); static Key eventHandlerHook(Key mappedKey, byte row, byte col, uint8_t keyState); diff --git a/src/MouseWrapper.cpp b/src/MouseWrapper.cpp index fb58547e..553a5356 100644 --- a/src/MouseWrapper.cpp +++ b/src/MouseWrapper.cpp @@ -10,7 +10,7 @@ uint16_t MouseWrapper_::section_top; uint16_t MouseWrapper_::section_left; boolean MouseWrapper_::is_warping; -uint8_t MouseWrapper_::mouseActiveForCycles; +uint8_t MouseWrapper_::accelStep; MouseWrapper_::MouseWrapper_(void) { Mouse.begin(); @@ -101,10 +101,10 @@ void MouseWrapper_::move(int8_t x, int8_t y) { int16_t moveX =0; int16_t moveY = 0; if (x != 0 ) { - moveX = (x * acceleration(mouseActiveForCycles)); + moveX = (x * acceleration(accelStep)); } if (y != 0) { - moveY = (y * acceleration(mouseActiveForCycles)); + moveY = (y * acceleration(accelStep)); } end_warping(); diff --git a/src/MouseWrapper.h b/src/MouseWrapper.h index 6c9fff10..fc2f8126 100644 --- a/src/MouseWrapper.h +++ b/src/MouseWrapper.h @@ -35,7 +35,7 @@ class MouseWrapper_ { static void warp(uint8_t warp_cmd); static void press_button(uint8_t button); static void release_button(uint8_t button); - static uint8_t mouseActiveForCycles; + static uint8_t accelStep; private: static uint16_t next_width;