From 7b2cc10d4a9f7464589e614d449a804dcfd1ec76 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Wed, 11 Jan 2017 20:22:46 -0800 Subject: [PATCH] Switch mouse keys to use integer math. This removes the last use of floating point math in the Model 01 core, saving us a good few K of program space --- .../src/Keyboardio-MouseKeys.cpp | 16 +++-- .../Keyboardio-MouseKeys/src/MouseWrapper.cpp | 58 +++++++++---------- .../Keyboardio-MouseKeys/src/MouseWrapper.h | 21 ++----- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp b/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp index 7c3b713a..aba457a0 100644 --- a/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp +++ b/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp @@ -5,19 +5,27 @@ #include "KeyboardioFirmware.h" static void handle_mouse_key_event(Key mappedKey, uint8_t keyState) { + if (key_toggled_off(keyState)) { + if (mappedKey.keyCode & KEY_MOUSE_UP || mappedKey.keyCode & KEY_MOUSE_DOWN) { + MouseWrapper.mouseActiveForCyclesY=0; + } + if (mappedKey.keyCode & KEY_MOUSE_LEFT || mappedKey.keyCode & KEY_MOUSE_RIGHT) { + MouseWrapper.mouseActiveForCyclesX=0; + } + } + if (!key_is_pressed(keyState)) return; if (mappedKey.keyCode & KEY_MOUSE_UP) { MouseWrapper.move(0,-1); - } - if (mappedKey.keyCode & KEY_MOUSE_DOWN) { + } else if (mappedKey.keyCode & KEY_MOUSE_DOWN) { MouseWrapper.move(0,1); } + if (mappedKey.keyCode & KEY_MOUSE_LEFT) { MouseWrapper.move(-1,0); - } - if (mappedKey.keyCode & KEY_MOUSE_RIGHT) { + } else if (mappedKey.keyCode & KEY_MOUSE_RIGHT) { MouseWrapper.move(1,0); } } diff --git a/libraries/Keyboardio-MouseKeys/src/MouseWrapper.cpp b/libraries/Keyboardio-MouseKeys/src/MouseWrapper.cpp index dfe55eb5..ccf4fb6a 100644 --- a/libraries/Keyboardio-MouseKeys/src/MouseWrapper.cpp +++ b/libraries/Keyboardio-MouseKeys/src/MouseWrapper.cpp @@ -77,43 +77,43 @@ void MouseWrapper_::warp(uint8_t warp_cmd) { } +// cubic wave function based on code from FastLED +uint8_t MouseWrapper_::acceleration(uint8_t cycles) { + uint8_t i = cycles; -double MouseWrapper_::acceleration (double cycles) { - double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER; - if (accel < ACCELERATION_FLOOR) { - accel = ACCELERATION_FLOOR; + if( i & 0x80) { + i = 255 - i; } - return accel; + + i = i << 1; + + uint8_t ii = (i*i) >> 8; + uint8_t iii = (ii*i) >> 8; + + i = (( (3 * (uint16_t)(ii)) - ( 2 * (uint16_t)(iii))) / 2) + ACCELERATION_FLOOR; + + if ( i > ACCELERATION_CEIL) { + i = ACCELERATION_CEIL; + } + return i; } + void MouseWrapper_::move( int8_t x, int8_t y) { + int16_t moveX =0; + int16_t moveY = 0; + if (x != 0 ) { + if (mouseActiveForCyclesX < 255) { mouseActiveForCyclesX++;} + moveX = (x * acceleration(mouseActiveForCyclesX)); + } + if (y != 0) { + if (mouseActiveForCyclesY < 255) { mouseActiveForCyclesY++;} + moveY = (y * acceleration(mouseActiveForCyclesY)); - if (x != 0 || y != 0) { - mouseActiveForCycles++; - double accel = (double) acceleration(mouseActiveForCycles); - float moveX = 0; - float moveY = 0; - if (x > 0) { - moveX = (x * accel) + carriedOverX; - carriedOverX = moveX - floor(moveX); - } else if (x < 0) { - moveX = (x * accel) - carriedOverX; - carriedOverX = ceil(moveX) - moveX; - } - - if (y > 0) { - moveY = (y * accel) + carriedOverY; - carriedOverY = moveY - floor(moveY); - } else if (y < 0) { - moveY = (y * accel) - carriedOverY; - carriedOverY = ceil(moveY) - moveY; - } + } + end_warping(); - Mouse.move(moveX, moveY, 0); - } else { - mouseActiveForCycles = 0; - } } diff --git a/libraries/Keyboardio-MouseKeys/src/MouseWrapper.h b/libraries/Keyboardio-MouseKeys/src/MouseWrapper.h index afffbbcd..f5413a17 100644 --- a/libraries/Keyboardio-MouseKeys/src/MouseWrapper.h +++ b/libraries/Keyboardio-MouseKeys/src/MouseWrapper.h @@ -26,16 +26,8 @@ // we want the whole s curve, not just the bit // that's usually above the x and y axes; -#define ATAN_LIMIT 1.57079633 -#define ACCELERATION_FLOOR 0.25 -#define ACCELERATION_MULTIPLIER 5 -#define ACCELERATION_RUNWAY 5 -// Climb speed is how fast we get to max speed -// 1 is "instant" -// 0.05 is just right -// 0.001 is insanely slow - -#define ACCELERATION_CLIMB_SPEED 0.05 +#define ACCELERATION_FLOOR 2 +#define ACCELERATION_CEIL 50 class MouseWrapper_ { @@ -45,20 +37,17 @@ class MouseWrapper_ { void warp(uint8_t warp_cmd); void press_button(uint8_t button); void release_button(uint8_t button); + uint8_t mouseActiveForCyclesX = 0; + uint8_t mouseActiveForCyclesY = 0; private: - double mouseActiveForCycles = 0; - float carriedOverX = 0; - float carriedOverY = 0; - - uint16_t next_width = 0; uint16_t next_height = 0; uint16_t section_top = 0; uint16_t section_left = 0; boolean is_warping = false; - double acceleration (double cycles); + uint8_t acceleration (uint8_t cycles); void begin_warping(); void end_warping(); void warp_jump(long left, long top, long height, long width);