-
Notifications
You must be signed in to change notification settings - Fork 14
/
MouseTo.cpp
120 lines (91 loc) · 3.28 KB
/
MouseTo.cpp
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
// MouseTo - Library for Arduino Leonardo/Micro for moving the mouse pointer to absolute screen coordinates: http://github.com/per1234/MouseTo
#include "MouseTo.h"
MouseToClass::MouseToClass() {
//set default values
screenResolutionX = 3840; //4K UHD
screenResolutionY = 2160; //4K UHD
correctionFactor = 1;
jumpDistance = 10; //this seems like a good balance between speed and accuracy
}
void MouseToClass::setTarget(const int targetXinput, const int targetYinput, const boolean homeFirst) {
//convert screen coordinates to Arduino coordinates
targetX = targetXinput * correctionFactor;
targetY = targetYinput * correctionFactor;
homed = !homeFirst;
}
int MouseToClass::getTargetX() {
return targetX;
}
int MouseToClass::getTargetY() {
return targetY;
}
//used for compatibility with the previous API
boolean MouseToClass::moveTo(const int targetXinput, const int targetYinput) {
setTarget(targetXinput, targetYinput);
return move();
}
boolean MouseToClass::move() {
//the mouse is homed to 0,0 on each mouse movement to make sure the absolute screen coordinates will be reached even if the physical mouse has been moved since the last moveTo
int moveToTargetX;
int moveToTargetY;
if (homed == false) {
//make sure it reaches 0,0 even in the worst case scenario of the cursor being at the bottom right corner
moveToTargetX = -screenResolutionX - 50;
moveToTargetY = -screenResolutionY - 50;
}
else {
moveToTargetX = targetX;
moveToTargetY = targetY;
}
if (positionX != moveToTargetX || positionY != moveToTargetY) {
if (positionX != moveToTargetX && (positionY == moveToTargetY || (positionY != moveToTargetY && moveAxisX == true))) {
const int moveX = moveToTargetX > positionX ? min(jumpDistance, moveToTargetX - positionX) : max(-jumpDistance, moveToTargetX - positionX);
Mouse.move(moveX, 0, 0);
positionX += moveX;
moveAxisX = false;
}
else {
const int moveY = moveToTargetY > positionY ? min(jumpDistance, moveToTargetY - positionY) : max(-jumpDistance, moveToTargetY - positionY);
Mouse.move(0, moveY, 0);
positionY += moveY;
moveAxisX = true;
}
}
else { //home or target position reached
if (homed == false) { //mouse is homed
homed = true;
positionX = 0;
positionY = 0;
return false;
}
homed = false; //reset for the next go
return true;
}
return false;
}
void MouseToClass::setScreenResolution(const int x, const int y) {
screenResolutionX = x;
screenResolutionY = y;
}
unsigned int MouseToClass::getScreenResolutionX() {
return screenResolutionX;
}
unsigned int MouseToClass::getScreenResolutionY() {
return screenResolutionY;
}
void MouseToClass::setCorrectionFactor(const float correctionFactorInput) {
correctionFactor = correctionFactorInput;
}
float MouseToClass::getCorrectionFactor() {
return correctionFactor;
}
void MouseToClass::setMaxJump(const int8_t jumpDistanceInput) {
jumpDistance = jumpDistanceInput;
}
int8_t MouseToClass::getMaxJump() {
return jumpDistance;
}
void MouseToClass::home() {
homed = false;
}
MouseToClass MouseTo; //This sets up a single global instance of the library so the class doesn't need to be declared in the user sketch and multiple instances are not necessary in this case.