First pass at encapsulating mouse management into something approximating a class

pull/18/head
Jesse Vincent 9 years ago
parent 5df1a1814f
commit d93dc0c9b3

@ -5,33 +5,22 @@
#include "MouseWrapper.h" #include "MouseWrapper.h"
// Warping MouseWrapper_::MouseWrapper_(void) {
static double mouseActiveForCycles = 0; Mouse.begin();
static float carriedOverX = 0; }
static float carriedOverY = 0;
static int abs_left = 0;
static int abs_top = 0;
static int next_width;
static int next_height;
static int section_top;
static int section_left;
static boolean is_warping = false;
void press_button(uint8_t button) { void MouseWrapper_::press_button(uint8_t button) {
Mouse.press(button); Mouse.press(button);
end_warping(); end_warping();
} }
void release_button(uint8_t button) { void MouseWrapper_::release_button(uint8_t button) {
Mouse.release(button); Mouse.release(button);
} }
void _warp_jump(long left, long top, long height, long width) { void MouseWrapper_::warp_jump(long left, long top, long height, long width) {
long x_center = left + width/2; long x_center = left + width/2;
long y_center = top + height/2; long y_center = top + height/2;
AbsoluteMouse.moveTo(x_center,y_center); AbsoluteMouse.moveTo(x_center,y_center);
@ -41,19 +30,19 @@ void _warp_jump(long left, long top, long height, long width) {
void begin_warping() { void MouseWrapper_::begin_warping() {
section_left = abs_left; section_left = WARP_ABS_LEFT;
section_top = abs_top; section_top = WARP_ABS_TOP;
next_width = MAX_WARP_WIDTH; next_width = MAX_WARP_WIDTH;
next_height = MAX_WARP_HEIGHT; next_height = MAX_WARP_HEIGHT;
is_warping = true; is_warping = true;
} }
void end_warping() { void MouseWrapper_::end_warping() {
is_warping= false; is_warping= false;
} }
void warp_mouse(uint8_t warp_cmd) { void MouseWrapper_::warp(uint8_t warp_cmd) {
if (is_warping == false) { if (is_warping == false) {
begin_warping(); begin_warping();
} }
@ -82,13 +71,13 @@ void warp_mouse(uint8_t warp_cmd) {
section_left = section_left + next_width; section_left = section_left + next_width;
} }
_warp_jump(section_left, section_top, next_height,next_width); warp_jump(section_left, section_top, next_height,next_width);
} }
double mouse_accel (double cycles) { double MouseWrapper_::acceleration (double cycles) {
double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER; double accel = (atan((cycles * ACCELERATION_CLIMB_SPEED)-ACCELERATION_RUNWAY) + ATAN_LIMIT) * ACCELERATION_MULTIPLIER;
if (accel < ACCELERATION_FLOOR) { if (accel < ACCELERATION_FLOOR) {
accel = ACCELERATION_FLOOR; accel = ACCELERATION_FLOOR;
@ -96,11 +85,11 @@ double mouse_accel (double cycles) {
return accel; return accel;
} }
void move_mouse( int8_t x, int8_t y) { void MouseWrapper_::move( int8_t x, int8_t y) {
if (x != 0 || y != 0) { if (x != 0 || y != 0) {
mouseActiveForCycles++; mouseActiveForCycles++;
double accel = (double) mouse_accel(mouseActiveForCycles); double accel = (double) acceleration(mouseActiveForCycles);
float moveX = 0; float moveX = 0;
float moveY = 0; float moveY = 0;
if (x > 0) { if (x > 0) {
@ -127,4 +116,4 @@ void move_mouse( int8_t x, int8_t y) {
} }
MouseWrapper_ MouseWrapper;

@ -1,20 +1,8 @@
#pragma once #pragma once
#include "Arduino.h" #include "Arduino.h"
#include "matrix_state.h"
#include "HID-Project.h" #include "HID-Project.h"
// Mouse-related 'public' API methods
double mouse_accel (double cycles);
void move_mouse( int8_t x, int8_t y);
void begin_warping();
void end_warping();
void warp_mouse(uint8_t warp_cmd);
void press_button(uint8_t button);
void release_button(uint8_t button);
// Buttons // Buttons
#define MOUSE_BUTTON_LEFT 0x01 #define MOUSE_BUTTON_LEFT 0x01
@ -38,6 +26,8 @@ void release_button(uint8_t button);
#define MAX_WARP_WIDTH 32767 #define MAX_WARP_WIDTH 32767
#define MAX_WARP_HEIGHT 32767 #define MAX_WARP_HEIGHT 32767
#define WARP_ABS_TOP 0
#define WARP_ABS_LEFT 0
// Mouse acceleration // Mouse acceleration
@ -53,3 +43,32 @@ void release_button(uint8_t button);
// 0.001 is insanely slow // 0.001 is insanely slow
#define ACCELERATION_CLIMB_SPEED 0.05 #define ACCELERATION_CLIMB_SPEED 0.05
class MouseWrapper_ {
public:
MouseWrapper_(void);
void move( int8_t x, int8_t y);
void warp(uint8_t warp_cmd);
void press_button(uint8_t button);
void release_button(uint8_t button);
private:
double mouseActiveForCycles = 0;
float carriedOverX = 0;
float carriedOverY = 0;
int next_width;
int next_height;
int section_top;
int section_left;
boolean is_warping = false;
double acceleration (double cycles);
void begin_warping();
void end_warping();
void warp_jump(long left, long top, long height, long width);
};
extern MouseWrapper_ MouseWrapper;

@ -1,15 +1,12 @@
#include "key_events.h" #include "key_events.h"
void handle_synthetic_key_event(byte switchState, Key mappedKey) { void handle_synthetic_key_event(byte switchState, Key mappedKey) {
if (mappedKey.flags & IS_MOUSE_KEY ) { if (mappedKey.flags & IS_MOUSE_KEY ) {
if (mappedKey.rawKey & MOUSE_WARP) { if (mappedKey.rawKey & MOUSE_WARP) {
if (key_toggled_on(switchState)) { if (key_toggled_on(switchState)) {
// we don't pass in the left and up values because those are the // we don't pass in the left and up values because those are the
// default, "no-op" conditionals // default, "no-op" conditionals
warp_mouse( (mappedKey.rawKey & MOUSE_WARP_END ? WARP_END : 0x00) | MouseWrapper.warp( (mappedKey.rawKey & MOUSE_WARP_END ? WARP_END : 0x00) |
(mappedKey.rawKey & MOUSE_DOWN ? WARP_DOWN : 0x00) | (mappedKey.rawKey & MOUSE_DOWN ? WARP_DOWN : 0x00) |
(mappedKey.rawKey & MOUSE_RIGHT ? WARP_RIGHT : 0x00) ); (mappedKey.rawKey & MOUSE_RIGHT ? WARP_RIGHT : 0x00) );
} }
@ -40,13 +37,13 @@ void handle_synthetic_key_event(byte switchState, Key mappedKey) {
|| mappedKey.rawKey == KEY_MOUSE_BTN_M || mappedKey.rawKey == KEY_MOUSE_BTN_M
|| mappedKey.rawKey == KEY_MOUSE_BTN_R) { || mappedKey.rawKey == KEY_MOUSE_BTN_R) {
if (key_toggled_on (switchState)) { if (key_toggled_on (switchState)) {
press_button( MouseWrapper.press_button(
(mappedKey.rawKey == KEY_MOUSE_BTN_L ? MOUSE_BUTTON_LEFT : 0x00) | (mappedKey.rawKey == KEY_MOUSE_BTN_L ? MOUSE_BUTTON_LEFT : 0x00) |
(mappedKey.rawKey == KEY_MOUSE_BTN_M ? MOUSE_BUTTON_MIDDLE : 0x00) | (mappedKey.rawKey == KEY_MOUSE_BTN_M ? MOUSE_BUTTON_MIDDLE : 0x00) |
(mappedKey.rawKey == KEY_MOUSE_BTN_R ? MOUSE_BUTTON_RIGHT : 0x00) ); (mappedKey.rawKey == KEY_MOUSE_BTN_R ? MOUSE_BUTTON_RIGHT : 0x00) );
} else if (key_toggled_off(switchState)) { } else if (key_toggled_off(switchState)) {
release_button( MouseWrapper.release_button(
(mappedKey.rawKey == KEY_MOUSE_BTN_L ? MOUSE_BUTTON_LEFT : 0x00) | (mappedKey.rawKey == KEY_MOUSE_BTN_L ? MOUSE_BUTTON_LEFT : 0x00) |
(mappedKey.rawKey == KEY_MOUSE_BTN_M ? MOUSE_BUTTON_MIDDLE : 0x00) | (mappedKey.rawKey == KEY_MOUSE_BTN_M ? MOUSE_BUTTON_MIDDLE : 0x00) |
(mappedKey.rawKey == KEY_MOUSE_BTN_R ? MOUSE_BUTTON_RIGHT : 0x00) ); (mappedKey.rawKey == KEY_MOUSE_BTN_R ? MOUSE_BUTTON_RIGHT : 0x00) );
@ -119,16 +116,16 @@ void handle_keymap_key_event(byte switchState, Key keymapEntry) {
void handle_mouse_key_event(byte switchState, Key mappedKey) { void handle_mouse_key_event(byte switchState, Key mappedKey) {
if (key_is_pressed(switchState)) { if (key_is_pressed(switchState)) {
if (mappedKey.rawKey & MOUSE_UP) { if (mappedKey.rawKey & MOUSE_UP) {
move_mouse(0,-1); MouseWrapper.move(0,-1);
} }
if (mappedKey.rawKey & MOUSE_DOWN) { if (mappedKey.rawKey & MOUSE_DOWN) {
move_mouse(0,1); MouseWrapper.move(0,1);
} }
if (mappedKey.rawKey & MOUSE_LEFT) { if (mappedKey.rawKey & MOUSE_LEFT) {
move_mouse(-1,0); MouseWrapper.move(-1,0);
} }
if (mappedKey.rawKey & MOUSE_RIGHT) { if (mappedKey.rawKey & MOUSE_RIGHT) {
move_mouse(1,0); MouseWrapper.move(1,0);
} }
} }
} }

Loading…
Cancel
Save