diff --git a/ArduinoKeyboard.h b/ArduinoKeyboard.h index 1d38ee2c..48783121 100644 --- a/ArduinoKeyboard.h +++ b/ArduinoKeyboard.h @@ -15,27 +15,40 @@ void setup(); #endif //add your function definitions for the project KeyboardIO here -typedef struct { - byte flags; - byte rawKey; -} Key; +#include +#include +#include #include "key_defs.h" +#include "KeyboardConfig.h" + +#include "keymaps_generated.h" +#include "debouncing.h" + + +//extern int usbMaxPower; +#define DEBUG_SERIAL 0 + + + byte matrixState[ROWS][COLS]; + + +byte charsBeingReported[KEYS_HELD_BUFFER]; // A bit vector for the 256 keys we might conceivably be holding down +byte charsReportedLastTime[KEYS_HELD_BUFFER]; // A bit vector for the 256 keys we might conceivably be holding down + + +long reporting_counter = 0; +byte primary_keymap = 0; +byte active_keymap = 0; + + byte commandBuffer[32]; int commandBufferSize; bool commandMode; bool commandPromptPrinted; -// 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); - // Console related void process_command_buffer(); @@ -82,6 +95,9 @@ void press_key(Key mappedKey); void release_key(Key mappedKey); +#ifndef VERSION +#define VERSION "locally-built" +#endif //Do not add code below this line #endif /* KeyboardIO_H_ */ diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index f2223eba..771e65c0 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -16,33 +16,9 @@ **/ -#include -#include -#include - -#include "KeyboardConfig.h" -#include "keymaps_generated.h" +#include "ArduinoKeyboard.h" #include // Don't need this for CLI compilation, but do need it in the IDE - -//extern int usbMaxPower; -#define DEBUG_SERIAL 0 - - - byte matrixState[ROWS][COLS]; - - -byte charsBeingReported[KEYS_HELD_BUFFER]; // A bit vector for the 256 keys we might conceivably be holding down -byte charsReportedLastTime[KEYS_HELD_BUFFER]; // A bit vector for the 256 keys we might conceivably be holding down - - -long reporting_counter = 0; -byte primary_keymap = 0; -byte active_keymap = 0; - -double mouseActiveForCycles = 0; -float carriedOverX = 0; -float carriedOverY = 0; - +#include "debouncing.h" void setup_matrix() { @@ -271,69 +247,8 @@ void loop() -// 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_primary_keymap(byte keymap) @@ -386,143 +301,6 @@ void report(byte row, byte col, boolean value) #endif } - -// Mouse-related methods -// -// - - - -void _warp_jump(long left, long top, long height, long width) { - long x_center = left + width/2; - long y_center = top + height/2; - Mouse.moveAbsolute(x_center,y_center); -} - - - -int last_x; -int last_y; - -// apparently, the mac discards 15% of the value space for mouse movement. -// need to test this on other platforms -// - -#define HALF_WIDTH 16384 -#define HALF_HEIGHT 16384 - - - -int abs_left = 0; -int abs_top = 0; - -int next_width; -int next_height; -int section_top; -int section_left; -boolean is_warping = false; - -void begin_warping() { - section_left = abs_left; - section_top = abs_top; - next_width = 32767; - next_height = 32767; - is_warping = true; -} - -void end_warping() { - is_warping= false; -} - -void warp_mouse(Key ninth) { - if (is_warping == false) { - begin_warping(); - } - - - if ( ninth.rawKey & MOUSE_END_WARP) { - end_warping(); - return; - } - - - next_width = next_width / 2; - next_height = next_height/2; - - if (ninth.rawKey & MOUSE_UP) { -// Serial.print(" - up "); - } else if (ninth.rawKey & MOUSE_DN) { - // Serial.print(" - down "); - section_top = section_top + next_height; - } - - if (ninth.rawKey & MOUSE_L) { - // Serial.print(" - left "); - } else if (ninth.rawKey & MOUSE_R) { - // Serial.print(" - right "); - section_left = section_left + next_width; - } - - _warp_jump(section_left, section_top, next_height,next_width); - -} - - -// 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 - -double mouse_accel (double cycles) -{ - double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER; - if (accel < ACCELERATION_FLOOR) { - accel = ACCELERATION_FLOOR; - } - 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; - } - end_warping(); - - Mouse.move(moveX, moveY, 0); - } else { - mouseActiveForCycles = 0; - } - -} - - // // Key Reports // @@ -608,24 +386,6 @@ void handle_synthetic_key_press(byte switchState, Key mappedKey) { } } } -void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y) { - - if (key_is_pressed(switchState)) { - if (mappedKey.rawKey & MOUSE_UP) { - y -= 1; - } - if (mappedKey.rawKey & MOUSE_DN) { - y += 1; - } - if (mappedKey.rawKey & MOUSE_L) { - x -= 1; - } - - if (mappedKey.rawKey & MOUSE_R) { - x += 1 ; - } - } -} void send_key_events() { diff --git a/debouncing.cpp b/debouncing.cpp new file mode 100644 index 00000000..b7bfaf49 --- /dev/null +++ b/debouncing.cpp @@ -0,0 +1,63 @@ + +#include +#include "debouncing.h" +// 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; + } +} diff --git a/debouncing.h b/debouncing.h new file mode 100644 index 00000000..d8035259 --- /dev/null +++ b/debouncing.h @@ -0,0 +1,14 @@ +#ifndef debouncing_h +#define debouncing_h + +// switch debouncing and status + + +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_on(byte keyState); +boolean key_toggled_off(byte keyState); + +#endif diff --git a/key_defs.h b/key_defs.h index 9c18a618..bede3f26 100644 --- a/key_defs.h +++ b/key_defs.h @@ -1,3 +1,10 @@ +typedef struct { + byte flags; + byte rawKey; +} Key; + + + #define KEY_FLAGS B00000000 #define CTRL_HELD B00000001 #define ALT_HELD B00000010 diff --git a/mouse_movement.cpp b/mouse_movement.cpp new file mode 100644 index 00000000..1ef9f4f2 --- /dev/null +++ b/mouse_movement.cpp @@ -0,0 +1,158 @@ + +// Mouse-related methods +// +// +#include "Arduino.h" +#include "key_defs.h" +#include "mouse_movement.h" +#include "debouncing.h" + +void _warp_jump(long left, long top, long height, long width) { + long x_center = left + width/2; + long y_center = top + height/2; + Mouse.moveAbsolute(x_center,y_center); +} + + + +int last_x; +int last_y; + +// apparently, the mac discards 15% of the value space for mouse movement. +// need to test this on other platforms +// + +#define HALF_WIDTH 16384 +#define HALF_HEIGHT 16384 + + + +int abs_left = 0; +int abs_top = 0; + +int next_width; +int next_height; +int section_top; +int section_left; +boolean is_warping = false; + +void begin_warping() { + section_left = abs_left; + section_top = abs_top; + next_width = 32767; + next_height = 32767; + is_warping = true; +} + +void end_warping() { + is_warping= false; +} + +void warp_mouse(Key ninth) { + if (is_warping == false) { + begin_warping(); + } + + + if ( ninth.rawKey & MOUSE_END_WARP) { + end_warping(); + return; + } + + + next_width = next_width / 2; + next_height = next_height/2; + + if (ninth.rawKey & MOUSE_UP) { +// Serial.print(" - up "); + } else if (ninth.rawKey & MOUSE_DN) { + // Serial.print(" - down "); + section_top = section_top + next_height; + } + + if (ninth.rawKey & MOUSE_L) { + // Serial.print(" - left "); + } else if (ninth.rawKey & MOUSE_R) { + // Serial.print(" - right "); + section_left = section_left + next_width; + } + + _warp_jump(section_left, section_top, next_height,next_width); + +} + + +// 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 + +double mouse_accel (double cycles) +{ + double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER; + if (accel < ACCELERATION_FLOOR) { + accel = ACCELERATION_FLOOR; + } + 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; + } + end_warping(); + + Mouse.move(moveX, moveY, 0); + } else { + mouseActiveForCycles = 0; + } + +} + + +void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y) { + + if (key_is_pressed(switchState)) { + if (mappedKey.rawKey & MOUSE_UP) { + y -= 1; + } + if (mappedKey.rawKey & MOUSE_DN) { + y += 1; + } + if (mappedKey.rawKey & MOUSE_L) { + x -= 1; + } + + if (mappedKey.rawKey & MOUSE_R) { + x += 1 ; + } + } +} + diff --git a/mouse_movement.h b/mouse_movement.h new file mode 100644 index 00000000..02995017 --- /dev/null +++ b/mouse_movement.h @@ -0,0 +1,7 @@ +#ifndef mouse_movement_h +#define mouse_movement_h +double mouseActiveForCycles = 0; +float carriedOverX = 0; +float carriedOverY = 0; + +#endif \ No newline at end of file diff --git a/setup.cpp b/setup.cpp new file mode 100644 index 00000000..e69de29b diff --git a/setup.h b/setup.h new file mode 100644 index 00000000..e69de29b