From e688c80b58128ef9a74f84f7289b0ef36678fad8 Mon Sep 17 00:00:00 2001 From: jesse Date: Wed, 20 Nov 2013 16:35:36 +0800 Subject: [PATCH] extract the mouse movement code and remove a variable we don't really need --- ArduinoKeyboard.ino | 84 ++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index f1c336b2..af2df897 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -68,7 +68,6 @@ static const Key keymaps[LAYERS][ROWS][COLS] = { }; - void release_keys_not_being_pressed() { @@ -133,6 +132,48 @@ double mouse_accel (double cycles) return accel; } +void handle_mouse_movement( int x, int y) { + + if (x!=0 && y!=0) { + 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; + } + + if (y >0) { + moveY = (y*accel) + carriedOverY; + carriedOverY = moveY - floor(moveY); + } else if (y<0) { + moveY = (y*accel) - carriedOverY; + carriedOverY = ceil(moveY) - moveY; + } + Serial.println(); + Serial.print("cycles: "); + Serial.println(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; + } + +} + void send_key_events(int layer) { //for every newly pressed button, figure out what logical key it is and send a key down event @@ -142,7 +183,6 @@ void send_key_events(int layer) // TODO:switch to sending raw HID packets - bool mouseActiveThisCycle = false; int x = 0; int y = 0; @@ -153,7 +193,6 @@ void send_key_events(int layer) Key mappedKey = keymaps[layer][row][col]; if (mappedKey.flags & MOUSE_KEY ) { if (key_is_pressed(switchState)) { - mouseActiveThisCycle = true; if (mappedKey.rawKey & MOUSE_UP) { y-=1; } @@ -189,47 +228,12 @@ void send_key_events(int layer) } } } - if (mouseActiveThisCycle) { - 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; - } - - if (y >0) { - moveY = (y*accel) + carriedOverY; - carriedOverY = moveY - floor(moveY); - } else if (y<0) { - moveY = (y*accel) - carriedOverY; - carriedOverY = ceil(moveY) - moveY; - } - Serial.println(); - Serial.print("cycles: "); - Serial.println(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; - } + handle_mouse_movement(x,y); release_keys_not_being_pressed(); } + void setup_matrix() { //set up the row pins as outputs