forked from StrawberryPear/healstatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardiono.c
98 lines (89 loc) · 2.17 KB
/
ardiono.c
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
#include "Keyboard.h"
#include "Mouse.h"
int CONTROL_ENCODE_VALUE = 1024;
int ALT_ENCODE_VALUE = 2048;
int SHIFT_ENCODE_VALUE = 4096;
void setup() {
Serial.begin(9600);
Serial.setTimeout(50);
Keyboard.begin();
Mouse.begin();
}
int test = 0;
void doKeyboard(int keyValue) {
if (keyValue & CONTROL_ENCODE_VALUE) {
// hold control
Keyboard.press(KEY_LEFT_CTRL);
delay(1);
}
if (keyValue & ALT_ENCODE_VALUE) {
// hold control
Keyboard.press(KEY_LEFT_ALT);
delay(1);
}
if (keyValue & SHIFT_ENCODE_VALUE) {
// hold control
Keyboard.press(KEY_LEFT_SHIFT);
delay(1);
}
// if data is less than 100,
Keyboard.press(136 + keyValue % 256);
delay(5);
Keyboard.release(136 + keyValue % 256);
delay(2);
if (keyValue & CONTROL_ENCODE_VALUE) {
// hold control
Keyboard.release(KEY_LEFT_CTRL);
delay(1);
}
if (keyValue & ALT_ENCODE_VALUE) {
// hold control
Keyboard.release(KEY_LEFT_ALT);
delay(1);
}
if (keyValue & SHIFT_ENCODE_VALUE) {
// hold control
Keyboard.release(KEY_LEFT_SHIFT);
}
}
void mouseMove(long x, long y) {
long max = max(abs(x), abs(y));
int count = (int) (max / 127);
signed char stepX = x / (count + 1);
signed char stepY = y / (count + 1);
for (int i = 0; i < count; i++) {
Mouse.move(stepX, stepY, 0);
delay(1);
}
signed char resX = x - (stepX * count);
signed char resY = y - (stepY * count);
if (resX != 0 || resY != 0) {
Mouse.move(resX, resY, 0);
delay(1);
}
}
void doMouse(int firstValue, int lastValue) {
mouseMove(firstValue, lastValue);
delay(2);
Mouse.click();
}
void loop() {
if(Serial.available() > 0) {
String a = Serial.readString();
if (a[0] == 'm') {
int commaIndex = a.indexOf(',');
int firstValue = a.substring(1, commaIndex).toInt();
int lastValue = a.substring(commaIndex + 1).toInt();
doMouse(firstValue, lastValue);
}
else if (a[0] == 'k') {
int value = a.substring(1).toInt();
doKeyboard(value);
}
else {
int value = a.toInt();
doKeyboard(value);
}
Serial.println(a);
}
}