-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMouseShield.ino
211 lines (185 loc) · 4.39 KB
/
MouseShield.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
#include <hidboot.h>
#include <usbhub.h>
#include <Mouse.h>
USB Usb;
USBHub Hub(&Usb);
HIDBoot < USB_HID_PROTOCOL_KEYBOARD | USB_HID_PROTOCOL_MOUSE > HidComposite(&Usb);
HIDBoot<USB_HID_PROTOCOL_MOUSE> HidMouse(&Usb);
// Signed char can be between -128 to 127
int delta[2];
int negMax = -127;
int posMax = 127;
// Mouse
int lmb = 0;
int rmb = 0;
int mmb = 0;
class MouseRptParser : public MouseReportParser
{
protected:
void OnMouseMove(MOUSEINFO *mi);
void OnLeftButtonUp(MOUSEINFO *mi);
void OnLeftButtonDown(MOUSEINFO *mi);
void OnRightButtonUp(MOUSEINFO *mi);
void OnRightButtonDown(MOUSEINFO *mi);
void OnMiddleButtonUp(MOUSEINFO *mi);
void OnMiddleButtonDown(MOUSEINFO *mi);
};
void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{
delta[0] = mi->dX;
delta[1] = mi->dY;
};
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
{
lmb = 0;
};
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
{
lmb = 1;
};
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
{
rmb = 0;
};
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
{
rmb = 1;
};
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
{
mmb = 0;
};
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
{
mmb = 1;
};
MouseRptParser MousePrs;
void setup()
{
Mouse.begin();
Serial.begin(9600);
Usb.Init();
HidComposite.SetReportParser(1, &MousePrs);
HidMouse.SetReportParser(0, &MousePrs);
}
void loop()
{
delta[0] = 0;
delta[1] = 0;
Usb.Task();
// Left Mouse
if (lmb == 0){
Mouse.release(MOUSE_LEFT);
} else if (lmb == 1){
Mouse.press(MOUSE_LEFT);
}
// Right Mouse
if (rmb == 0){
Mouse.release(MOUSE_RIGHT);
} else if (rmb == 1){
Mouse.press(MOUSE_RIGHT);
}
// Middle Mouse
if (mmb == 0){
Mouse.release(MOUSE_MIDDLE);
} else if (mmb == 1){
Mouse.press(MOUSE_MIDDLE);
}
if (Serial.available() > 0) {
// Read Data
String data = Serial.readStringUntil('x');
// Gets demarcation between deltaX, DeltaY, and Click
int deltaXIndex = data.indexOf(':');
int deltaYIndex = data.indexOf(':', deltaXIndex + 1);
// Extract DeltaX, DeltaY, and Click
int delta[2];
delta[0] = data.substring(0, deltaXIndex).toInt();
delta[1] = data.substring(deltaXIndex + 1, deltaYIndex).toInt();
int click = data.substring(deltaYIndex + 1).toInt();
handleX(delta[0]);
handleY(-delta[1]);
if (click == 1) {
Mouse.click();
}
} else{
Mouse.move(delta[0], delta[1]);
}
}
// Handle Moving of x
void handleX(int dx){
int spawns;
int remainder;
if(dx < negMax)
{
// How many times we move mouse
spawns = int(dx / negMax);
// How much we move after for loop
remainder = int(dx % negMax);
// Because we can only move 125 at a time,
// we need a for loop to spawn multiple mouse events.
for(int i = 0; i < spawns; i++)
{
Mouse.move(negMax , 0, 0);
}
// Move Remainder
Mouse.move(remainder, 0, 0);
}
else if (dx >= negMax && dx <= posMax)
{
Mouse.move(dx, 0, 0);
}
else if (dx > posMax)
{
// How many times we move mouse
spawns = int(dx / posMax);
// How much we move after for loop
remainder = int(dx % posMax);
for(int i = 0; i < spawns; i++)
{
Mouse.move(posMax , 0, 0);
}
// Move Remainder
Mouse.move(remainder, 0, 0);
}
}
// Handle Moving of x
void handleY(int dy){
int spawns;
int remainder;
// MindTrip, Neg is pos & Pos is Neg for move, hence the inverted pos & Neg
if(dy < negMax)
{
// How many times we move mouse
spawns = int(dy / negMax);
// How much we move after for loop. -1 converts to correct direction on arduino (pos,neg,neg,pos)
remainder = int(dy % negMax);
remainder *= -1;
// Because we can only move 125 at a time,
// we need a for loop to spawn multiple mouse events.
for(int i = 0; i < spawns; i++)
{
Mouse.move(0, posMax, 0);
}
// Move Remainder
Mouse.move(0, remainder, 0);
}
else if (dy >= negMax && dy <= posMax)
{
dy *= -1;
Mouse.move(0, dy, 0);
}
else if (dy > posMax)
{
// How many times we move mouse
spawns = int(dy / posMax);
// How much we move after for loop
remainder = int(dy % posMax);
remainder *= -1;
for(int i = 0; i < spawns; i++)
{
Mouse.move(0, negMax, 0);
}
// Move Remainder
Mouse.move(0, remainder, 0);
}
};