forked from per1234-org/MouseTo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEsp32MouseTo.cpp
More file actions
120 lines (91 loc) · 3.39 KB
/
Esp32MouseTo.cpp
File metadata and controls
120 lines (91 loc) · 3.39 KB
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
// ESP32MouseTo - Library for Arduino Leonardo/Micro for moving the mouse pointer to absolute screen coordinates: https://github.com/Youngv/ESP32MouseTo
#include "ESP32MouseTo.h"
ESP32MouseToClass::ESP32MouseToClass() {
//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 ESP32MouseToClass::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 ESP32MouseToClass::getTargetX() {
return targetX;
}
int ESP32MouseToClass::getTargetY() {
return targetY;
}
//used for compatibility with the previous API
boolean ESP32MouseToClass::moveTo(const int targetXinput, const int targetYinput) {
setTarget(targetXinput, targetYinput);
return move();
}
boolean ESP32MouseToClass::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);
USBHIDMouse().move(moveX, 0, 0);
positionX += moveX;
moveAxisX = false;
}
else {
const int moveY = moveToTargetY > positionY ? _min(jumpDistance, moveToTargetY - positionY) : _max(-jumpDistance, moveToTargetY - positionY);
USBHIDMouse().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 ESP32MouseToClass::setScreenResolution(const int x, const int y) {
screenResolutionX = x;
screenResolutionY = y;
}
unsigned int ESP32MouseToClass::getScreenResolutionX() {
return screenResolutionX;
}
unsigned int ESP32MouseToClass::getScreenResolutionY() {
return screenResolutionY;
}
void ESP32MouseToClass::setCorrectionFactor(const float correctionFactorInput) {
correctionFactor = correctionFactorInput;
}
float ESP32MouseToClass::getCorrectionFactor() {
return correctionFactor;
}
void ESP32MouseToClass::setMaxJump(const int8_t jumpDistanceInput) {
jumpDistance = jumpDistanceInput;
}
int8_t ESP32MouseToClass::getMaxJump() {
return jumpDistance;
}
void ESP32MouseToClass::home() {
homed = false;
}
ESP32MouseToClass ESP32MouseTo; //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.