From f860619cd48e2f39ffdf6795fe7a9fcb1401cafd Mon Sep 17 00:00:00 2001 From: jesse Date: Mon, 18 Nov 2013 13:33:38 +0800 Subject: [PATCH] a quickie first-pass at a non-linear acceleration curve for the mouse cursor code. it's not right yet. --- ArduinoKeyboard.ino | 48 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index 63bf64f1..d60ccb1a 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -46,7 +46,8 @@ long reporting_counter = 0; static const int LAYERS = 2; int current_layer = 0; int mouseActiveForCycles = 0; - +float carriedOverX =0; +float carriedOverY =0; static const Key keymaps[LAYERS][ROWS][COLS] = { @@ -178,6 +179,15 @@ void reset_matrix() { charsBeingReported[i] = 0x00; } } +double mouse_accel (double cycles) { + double accel = atan((cycles/10)-12); + accel += 1.5707963267944; // we want the whole s curve, not just the bit that's usually above the x and y axes; + accel = accel *0.85; + if (accel<0.85) { + accel =0.85; + } + return accel; +} void send_key_events(int layer) { //for every newly pressed button, figure out what logical key it is and send a key down event @@ -236,24 +246,38 @@ void send_key_events(int layer) { } } if (mouseActiveThisCycle) { - if(mouseActiveForCycles<=2400) { - mouseActiveForCycles++; - } - int accel = - // int(log10(double( mouseActiveForCycles)))/ - int(mouseActiveForCycles/600)+1; - - ; + mouseActiveForCycles++; + double accel = (double) mouse_accel(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; + } - Mouse.move(x*accel, y*accel, 0); + if (y >0) { + moveY = (y*accel) + carriedOverY; + carriedOverY = moveY - floor(moveY); + } else if (y<0) { + moveY = (y*accel) - carriedOverY; + carriedOverY = ceil(moveY) - moveY; + } + Serial.print('cycles: '); + Serial.print(mouseActiveForCycles); + Serial.print("Accel: "); + Serial.print(accel); Serial.print(" moveX is "); Serial.print(moveX); Serial.print(" moveY is "); Serial.print(moveY); Serial.print(" carriedoverx is "); Serial.print(carriedOverX); Serial.print(" carriedOverY is "); Serial.println(carriedOverY); + Mouse.move(moveX,moveY, 0); } else { mouseActiveForCycles=0; - } + } release_keys_not_being_pressed(); } - void setup_matrix() { //set up the row pins as outputs for (int row = 0; row < ROWS; row++) {