diff --git a/ArduinoKeyboard.h b/ArduinoKeyboard.h index 2b4bf5d9..25ab1ae1 100644 --- a/ArduinoKeyboard.h +++ b/ArduinoKeyboard.h @@ -2,7 +2,7 @@ #define KeyboardIO_H_ #include "Arduino.h" //add your includes for the project KeyboardIO here - +#include //end of add your includes here #ifdef __cplusplus @@ -24,66 +24,29 @@ typedef struct { #include "key_defs.h" -boolean key_was_pressed (byte keyState) -{ - if ( byte((keyState >> 4)) ^ B00001111 ) { - return false; - } else { - return true; - } - -} - -boolean key_was_not_pressed (byte keyState) -{ - if ( byte((keyState >> 4)) ^ B00000000 ) { - return false; - } else { - return true; - } - -} - -boolean key_is_pressed (byte keyState) -{ - - if ( byte((keyState << 4)) ^ B11110000 ) { - return false; - } else { - return true; - } -} -boolean key_is_not_pressed (byte keyState) -{ +// Switch status and debouncing +boolean key_was_pressed (byte keyState); +boolean key_was_not_pressed (byte keyState); +boolean key_is_pressed (byte keyState); +boolean key_is_not_pressed (byte keyState); +boolean key_toggled_off(byte keyState); +boolean key_toggled_on(byte keyState); - if ( byte((keyState << 4)) ^ B00000000 ) { - return false; - } else { - return true; - } -} -boolean key_toggled_on(byte keyState) -{ - if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) { - return true; - } else { - return false; - } -} +// EEPROM related +void save_current_layer(byte layer); +byte load_current_layer(); -boolean key_toggled_off(byte keyState) -{ - if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) { - return true; - } else { - return false; - } -} +// Keyboard debugging +void report(byte row, byte col, boolean value); +void report_matrix(); +// Mouse-related methods +double mouse_accel (double cycles); +void handle_mouse_movement( char x, char y); //Do not add code below this line #endif /* KeyboardIO_H_ */ diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index 764a082e..5b7c1f7a 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -42,6 +42,9 @@ long reporting_counter = 0; byte current_layer = 0; byte active_layer = 0; +double mouseActiveForCycles = 0; +float carriedOverX = 0; +float carriedOverY = 0; void release_keys_not_being_pressed() @@ -296,3 +299,193 @@ void loop() reset_matrix(); } + + + + + + + + + + + + +// switch debouncing and status +// +// + + +boolean key_was_pressed (byte keyState) +{ + if ( byte((keyState >> 4)) ^ B00001111 ) { + return false; + } else { + return true; + } + +} + +boolean key_was_not_pressed (byte keyState) +{ + if ( byte((keyState >> 4)) ^ B00000000 ) { + return false; + } else { + return true; + } + +} + +boolean key_is_pressed (byte keyState) +{ + + if ( byte((keyState << 4)) ^ B11110000 ) { + return false; + } else { + return true; + } +} +boolean key_is_not_pressed (byte keyState) +{ + + if ( byte((keyState << 4)) ^ B00000000 ) { + return false; + } else { + return true; + } +} + +boolean key_toggled_on(byte keyState) +{ + if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) { + return true; + } else { + return false; + } +} + + +boolean key_toggled_off(byte keyState) +{ + if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) { + return true; + } else { + return false; + } +} + + + +void save_current_layer(byte layer) +{ + EEPROM.write(EEPROM_LAYER_LOCATION, layer); +} + +byte load_current_layer() +{ + byte layer = EEPROM.read(EEPROM_LAYER_LOCATION); + if (layer >= LAYERS ) { + return 0; // undefined positions get saved as 255 + } + return layer; +} + + + + +// Debugging Reporting +// + +void report_matrix() +{ +#ifdef DEBUG_SERIAL + if (reporting_counter++ % 100 == 0 ) { + for (byte row = 0; row < ROWS; row++) { + for (byte col = 0; col < COLS; col++) { + Serial.print(matrixState[row][col], HEX); + Serial.print(", "); + + } + Serial.println(""); + } + Serial.println(""); + } +#endif +} + +void report(byte row, byte col, boolean value) +{ +#ifdef DEBUG_SERIAL + Serial.print("Detected a change on "); + Serial.print(col); + Serial.print(" "); + Serial.print(row); + Serial.print(" to "); + Serial.print(value); + Serial.println("."); +#endif +} + + +// Mouse-related methods +// +// + + +double mouse_accel (double cycles) +{ + double accel = atan((cycles / 50) - 5); + 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.25) { + accel = 0.25; + } + return accel; +} + +void handle_mouse_movement( char x, char 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; + } +#ifdef DEBUG_SERIAL + 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); +#endif + Mouse.move(moveX, moveY, 0); + } else { + mouseActiveForCycles = 0; + } + +} + + diff --git a/KeyboardDebugging.h b/KeyboardDebugging.h deleted file mode 100644 index ef17c4b3..00000000 --- a/KeyboardDebugging.h +++ /dev/null @@ -1,32 +0,0 @@ - - -void report_matrix() -{ -#ifdef DEBUG_SERIAL - if (reporting_counter++ % 100 == 0 ) { - for (byte row = 0; row < ROWS; row++) { - for (byte col = 0; col < COLS; col++) { - Serial.print(matrixState[row][col], HEX); - Serial.print(", "); - - } - Serial.println(""); - } - Serial.println(""); - } -#endif -} - -void report(byte row, byte col, boolean value) -{ -#ifdef DEBUG_SERIAL - Serial.print("Detected a change on "); - Serial.print(col); - Serial.print(" "); - Serial.print(row); - Serial.print(" to "); - Serial.print(value); - Serial.println("."); -#endif -} - diff --git a/KeyboardEEPROM.h b/KeyboardEEPROM.h deleted file mode 100644 index c8ffd421..00000000 --- a/KeyboardEEPROM.h +++ /dev/null @@ -1,19 +0,0 @@ -#include - - -void save_current_layer(byte layer) -{ - EEPROM.write(EEPROM_LAYER_LOCATION, layer); -} - -byte load_current_layer() -{ - byte layer = EEPROM.read(EEPROM_LAYER_LOCATION); - if (layer >= LAYERS ) { - return 0; // undefined positions get saved as 255 - } - return layer; -} - - - diff --git a/KeyboardMouse.h b/KeyboardMouse.h deleted file mode 100644 index fbcb9dd2..00000000 --- a/KeyboardMouse.h +++ /dev/null @@ -1,59 +0,0 @@ -double mouseActiveForCycles = 0; -float carriedOverX = 0; -float carriedOverY = 0; - -double mouse_accel (double cycles) -{ - double accel = atan((cycles / 50) - 5); - 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.25) { - accel = 0.25; - } - return accel; -} - -void handle_mouse_movement( char x, char 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; - } -#ifdef DEBUG_SERIAL - 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); -#endif - Mouse.move(moveX, moveY, 0); - } else { - mouseActiveForCycles = 0; - } - -}