Skip to content

Commit 3713d07

Browse files
committed
Alternate move axes
Previously the movement on the X axis was completed before starting the Y axis movement. This could be problematic in situations where the target coordinate was continually changing because that would cause only movement on the X axis to occur and also results in less natural looking movement. By alternating the axis of movement the move will be made to target coordinate in both axes even if it's never reached. The 45 degree movement with horizontal/vertical movement at the end is an aesthetic improvement, though still not ideal. Unfortunately this causes an increase of 24 bytes program memory and 1 byte SRAM due to the axis switching code.
1 parent 70e9afa commit 3713d07

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

MouseTo.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@ boolean MouseToClass::move() {
5050
moveToTargetY = targetY;
5151
}
5252

53-
if (positionX != moveToTargetX) {
54-
const int moveX = moveToTargetX > positionX ? min(jumpDistance, moveToTargetX - positionX) : max(-jumpDistance, moveToTargetX - positionX);
55-
Mouse.move(moveX, 0, 0);
56-
positionX += moveX;
57-
}
58-
else if (positionY != moveToTargetY) {
59-
const int moveY = moveToTargetY > positionY ? min(jumpDistance, moveToTargetY - positionY) : max(-jumpDistance, moveToTargetY - positionY);
60-
Mouse.move(0, moveY, 0);
61-
positionY += moveY;
53+
if (positionX != moveToTargetX || positionY != moveToTargetY) {
54+
if (positionX != moveToTargetX && (positionY == moveToTargetY || (positionY != moveToTargetY && moveAxisX == true))) {
55+
const int moveX = moveToTargetX > positionX ? min(jumpDistance, moveToTargetX - positionX) : max(-jumpDistance, moveToTargetX - positionX);
56+
Mouse.move(moveX, 0, 0);
57+
positionX += moveX;
58+
moveAxisX = false;
59+
}
60+
else {
61+
const int moveY = moveToTargetY > positionY ? min(jumpDistance, moveToTargetY - positionY) : max(-jumpDistance, moveToTargetY - positionY);
62+
Mouse.move(0, moveY, 0);
63+
positionY += moveY;
64+
moveAxisX = true;
65+
}
6266
}
6367
else { //home or target position reached
6468
if (homed == false) { //mouse is homed

MouseTo.h

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class MouseToClass {
3333
int screenResolutionY;
3434
float correctionFactor;
3535
int8_t jumpDistance;
36+
boolean moveAxisX;
3637
};
3738
extern MouseToClass MouseTo; //declare the class so it doesn't have to be done in the sketch
3839
#endif //MouseTo_h

0 commit comments

Comments
 (0)