-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmighty-mouse.ino
222 lines (190 loc) · 5.73 KB
/
mighty-mouse.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <Mouse.h>
#include <Keyboard.h>
// === Buttons includes
#include <Button.h>
#include <ButtonEventCallback.h>
#include <PushButton.h>
#include <Bounce2.h>
// === Display includes
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int JOYSTICK_X = A0;
const int JOYSTICK_Y = A1;
const int JOYSTICK_PADDING = 10; // Dead zone to avoid jitter
const int BUTTON_MODE = 4;
const int SPEED_REDUCTION = 20; // More is slower
const int MOVE_DELAY = 20; // More is slower
const int SCROLL_DELAY = 20; // More is slower
const int SCROLL_VALUE = 2; // More is faster
const int BTN_HOLD_DELAY = 300; // Milliseconds
const float MOUSE_OS_SPEED = 2.4; // To compensate for OS mouse acceleration settings
const int MODE_PAN = 1;
const int MODE_ORBIT = 2;
const int MODE_ZOOM = 3;
PushButton btn_mode = PushButton(BUTTON_MODE, ENABLE_INTERNAL_PULLUP);
float displacedX = 0;
float displacedY = 0;
int currentMode = MODE_PAN;
int previousMode = MODE_PAN;
bool isMoving = false;
void setup() {
// Init I2C display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 display
//Init mouse and keyboard emulation
Mouse.begin();
Keyboard.begin();
// Set mode button listeners
btn_mode.onHold(BTN_HOLD_DELAY, onModeButtonHeld);
btn_mode.onRelease(onModeButtonReleased);
// Display the current mode
displayMode();
}
void loop() {
btn_mode.update();
readJoystickValues();
}
void onModeButtonHeld(Button& btn){
if(currentMode != MODE_ZOOM){
previousMode = currentMode;
currentMode = MODE_ZOOM;
}
displayMode();
}
void onModeButtonReleased(Button& btn, uint16_t duration){
// Ignore when coming from a held button (Zoom mode)
if(duration < BTN_HOLD_DELAY){
switch(currentMode){
case MODE_PAN:
currentMode = MODE_ORBIT;
break;
case MODE_ORBIT:
currentMode = MODE_PAN;
break;
case MODE_ZOOM:
currentMode = previousMode;
break;
}
}
displayMode();
}
void readJoystickValues(){
// The Mouse.move() function accepts a "signed char" (-128 to +127)
// Convert joystick values (from 0 to 1023) to valid mouse values (from -127 to 127)
int dX = map(analogRead(JOYSTICK_X), 0, 1023, -127, 127);
int dY = map(analogRead(JOYSTICK_Y), 0, 1023, -127, 127);
// Padding is used to prevent jitter
if((abs(dX) >= JOYSTICK_PADDING) || (abs(dY) >= JOYSTICK_PADDING)){
if(isMoving == false){
isMoving = true;
onStartMoving();
}
onMoving(dX, dY);
} else if((abs(dX) < JOYSTICK_PADDING / 2) && (abs(dY) < JOYSTICK_PADDING / 2)){ // Lower threshold for debounce
if(isMoving == true){
isMoving = false;
onStopMoving();
}
}
}
void onStartMoving(){
displacedX = 0; // Reset X displacement
displacedY = 0; // Reset Y displacement
switch(currentMode){
case MODE_PAN:
Mouse.press(MOUSE_MIDDLE);
break;
case MODE_ORBIT:
Keyboard.press(KEY_LEFT_SHIFT);
Mouse.press(MOUSE_MIDDLE);
break;
}
}
void onMoving(int valX, int valY){
int reducedX = valX / SPEED_REDUCTION;
int reducedY = valY / SPEED_REDUCTION;
switch(currentMode){
case MODE_PAN:
case MODE_ORBIT:
Mouse.move(reducedX, reducedY);
displacedX += reducedX;
displacedY += reducedY;
delay(MOVE_DELAY);
break;
case MODE_ZOOM:
Mouse.move(0, 0, reducedY / SCROLL_VALUE * -1); // Inver axis
delay(SCROLL_DELAY);
break;
}
}
void onStopMoving(){
switch(currentMode){
case MODE_PAN:
Mouse.release(MOUSE_MIDDLE);
moveToOrigin(displacedX, displacedY);
break;
case MODE_ORBIT:
Keyboard.releaseAll();
Mouse.release(MOUSE_MIDDLE);
moveToOrigin(displacedX, displacedY);
break;
}
}
void moveToOrigin(int displacedX, int displacedY){
// The Mouse library does not accept numbers on a range outside -127 to 127,
// so we have to move several times to reach the origin
int timesX = abs(displacedX / 127);
int remainingX = ((displacedX % 127) * -1) / MOUSE_OS_SPEED; // Change symbol so it goes in the opposite direction
int timesY = abs(displacedY / 127);
int remainingY = ((displacedY % 127) * -1) / MOUSE_OS_SPEED; // Change symbol so it goes in the opposite direction
int value = 127 / MOUSE_OS_SPEED; // Take into account OS mouse settings
if(displacedX > 0){
// Move left to go back to the origin
for(int i=0; i < timesX; i++){
Mouse.move(value * -1, 0);
}
} else {
// Move right to go back to the origin
for(int i=0; i < timesX; i++){
Mouse.move(value, 0);
}
}
if(displacedY > 0){
// Move down to go back to the origin
for(int i=0; i < timesY; i++){
Mouse.move(0, value * -1);
}
} else {
// Move up to go back to the origin
for(int i=0; i < timesY; i++){
Mouse.move(0, value);
}
}
// Move the remaining positions
Mouse.move(remainingX, 0);
Mouse.move(0, remainingY);
}
void displayMode() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
switch(currentMode){
case MODE_PAN:
display.println(F("MODE: Pan"));
break;
case MODE_ORBIT:
display.println(F("MODE: Orbit"));
break;
case MODE_ZOOM:
display.println(F("MODE: Zoom"));
break;
}
display.display();
}