split out setup, debouncing and mouse movement into separate files

pull/18/head
Jesse Vincent 10 years ago
parent 01cdada1a0
commit acfd229fd0

@ -15,27 +15,40 @@ void setup();
#endif #endif
//add your function definitions for the project KeyboardIO here //add your function definitions for the project KeyboardIO here
typedef struct {
byte flags;
byte rawKey;
} Key;
#include <stdio.h>
#include <math.h>
#include <avr/wdt.h>
#include "key_defs.h" #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]; byte commandBuffer[32];
int commandBufferSize; int commandBufferSize;
bool commandMode; bool commandMode;
bool commandPromptPrinted; 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 // Console related
void process_command_buffer(); void process_command_buffer();
@ -82,6 +95,9 @@ void press_key(Key mappedKey);
void release_key(Key mappedKey); void release_key(Key mappedKey);
#ifndef VERSION
#define VERSION "locally-built"
#endif
//Do not add code below this line //Do not add code below this line
#endif /* KeyboardIO_H_ */ #endif /* KeyboardIO_H_ */

@ -16,33 +16,9 @@
**/ **/
#include <stdio.h> #include "ArduinoKeyboard.h"
#include <math.h>
#include <avr/wdt.h>
#include "KeyboardConfig.h"
#include "keymaps_generated.h"
#include <EEPROM.h> // Don't need this for CLI compilation, but do need it in the IDE #include <EEPROM.h> // Don't need this for CLI compilation, but do need it in the IDE
#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;
double mouseActiveForCycles = 0;
float carriedOverX = 0;
float carriedOverY = 0;
void setup_matrix() void setup_matrix()
{ {
@ -271,68 +247,7 @@ 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;
}
}
@ -386,143 +301,6 @@ void report(byte row, byte col, boolean value)
#endif #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 // 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() void send_key_events()
{ {

@ -0,0 +1,63 @@
#include <Arduino.h>
#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;
}
}

@ -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

@ -1,3 +1,10 @@
typedef struct {
byte flags;
byte rawKey;
} Key;
#define KEY_FLAGS B00000000 #define KEY_FLAGS B00000000
#define CTRL_HELD B00000001 #define CTRL_HELD B00000001
#define ALT_HELD B00000010 #define ALT_HELD B00000010

@ -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 ;
}
}
}

@ -0,0 +1,7 @@
#ifndef mouse_movement_h
#define mouse_movement_h
double mouseActiveForCycles = 0;
float carriedOverX = 0;
float carriedOverY = 0;
#endif
Loading…
Cancel
Save