-
Notifications
You must be signed in to change notification settings - Fork 19
/
control.py
66 lines (54 loc) · 2.14 KB
/
control.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
import struct
from time import sleep
class ControlMixin:
ACTION_MOVE = b'\x02'
ACTION_DOWN = b'\x00'
ACTION_UP = b'\x01'
move_step_length = 5 # Move by 5 pixels in one iteration
move_steps_delay = 0.005 # Delay between each move step
# Define in base class
resolution = None
control_socket = None
def _build_touch_message(self, x, y, action):
b = bytearray(b'\x02')
b += action
b += b'\xff\xff\xff\xff\xff\xff\xff\xff'
b += struct.pack('>I', int(x))
b += struct.pack('>I', int(y))
b += struct.pack('>h', int(self.resolution[0]))
b += struct.pack('>h', int(self.resolution[1]))
b += b'\xff\xff' # Pressure
b += b'\x00\x00\x00\x01' # Event button primary
return bytes(b)
def swipe(self, start_x, start_y, end_x, end_y):
self.control_socket.send(self._build_touch_message(x=start_x, y=start_y, action=self.ACTION_DOWN))
next_x = start_x
next_y = start_y
if end_x > self.resolution[0]:
end_x = self.resolution[0]
if end_y > self.resolution[1]:
end_y = self.resolution[1]
decrease_x = True if start_x > end_x else False
decrease_y = True if start_y > end_y else False
while True:
if decrease_x:
next_x -= self.move_step_length
if next_x < end_x:
next_x = end_x
else:
next_x += self.move_step_length
if next_x > end_x:
next_x = end_x
if decrease_y:
next_y -= self.move_step_length
if next_y < end_y:
next_y = end_y
else:
next_y += self.move_step_length
if next_y > end_y:
next_y = end_y
self.control_socket.send(self._build_touch_message(x=next_x, y=next_y, action=self.ACTION_MOVE))
if next_x == end_x and next_y == end_y:
self.control_socket.send(self._build_touch_message(x=next_x, y=next_y, action=self.ACTION_UP))
break
sleep(self.move_steps_delay)