-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.py
138 lines (113 loc) · 3.13 KB
/
python.py
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
import serial
import pyautogui
ser = serial.Serial('COM3') # change serial to your own port
data = [0, 0, 0, 0]
prevdata = [0, 0, 0, 0]
xsensitivity = 0.1
ysensitivity = 0.1
threshold = 175
xymove = [0, 0]
update = [False, False]
counter = [0, 0]
click = [False, False]
def leftClick():
pyautogui.mouseDown(button='left')
def rightClick():
pyautogui.mouseDown(button='right')
def leftRelease():
pyautogui.mouseUp(button='left')
def rightRelease():
pyautogui.mouseUp(button='right')
def move(x, y):
pyautogui.moveRel(x, y)
def scroll(x):
pyautogui.scroll(x)
def executeTask():
if data[0] >= 4 and data[1] < 4:
if click[0] == False:
leftClick()
click[0] = True
else:
if click[0] == True:
leftRelease()
click[0] = False
if data[0] < 4 and data[1] >= 4:
if click[1] == False:
rightClick()
click[1] = True
else:
if click[1] == True:
rightRelease()
click[1] = False
if data[0] >= 4 and data[1] >= 4:
if (abs(data[2] - prevdata[2]) > threshold) and xymove[0] == 0:
print()
if (abs(data[2] - prevdata[2]) > threshold) and xymove[0] == 0:
move(0, (-data[2] + prevdata[2] + 25) * xsensitivity)
if (abs(data[3] - prevdata[3]) > threshold) and xymove[1] == 0:
move((data[3] - prevdata[3] + 25) * ysensitivity, 0)
def decode():
string = str(ser.readline())[2:]
print(string)
try:
if string[0] == '<':
index = -1
element = ""
for character in string:
if character == '<':
index += 1
elif character == ' ' or character == ">":
data[index] = float(element)
element = ""
index += 1
else:
element += character
if character == ">":
break
except:
print()
def updatexy():
if (update[0]):
if (data[2] - prevdata[2] > threshold) and xymove[0] < 1:
xymove[0] += 1
counter[0] = 0
if (data[2] - prevdata[2] < -threshold) and xymove[0] > -1:
xymove[0] += -1
counter[0] = 0
if (update[1]):
if (data[3] - prevdata[3] > threshold) and xymove[1] < 1:
xymove[1] += 1
counter[1] = 0
if (data[3] - prevdata[3] < -threshold) and xymove[1] > -1:
xymove[1] += -1
counter[1] = 0
def updateMove():
if (abs(data[2] - prevdata[2]) < threshold):
update[0] = False
else:
update[0] = True
if (abs(data[3] - prevdata[3]) < threshold):
update[1] = False
else:
update[1] = True
def updateCounter():
counter[0] += 1
counter[1] += 1
if (counter[0] == 3):
xymove[0] = 0
counter[0] = 0
if (counter[1] == 3):
xymove[1] = 0
counter[1] = 0
while True:
decode()
updateCounter()
updateMove()
updatexy()
print(prevdata)
executeTask()
prevdata = data.copy()
print(data)
print(update)
print(xymove)
print(click)