-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch_server.py
More file actions
executable file
·56 lines (38 loc) · 1.31 KB
/
touch_server.py
File metadata and controls
executable file
·56 lines (38 loc) · 1.31 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
import asyncio
from touch_binding import makeTouch, POINTER_FLAGS
async def make_touch(finguer, state, x, y):
makeTouch(x, y, finguer, POINTER_FLAGS[state])
async def message_control(data):
if len(data) == 4:
state = data[0] & 0b11
finguer = data[0] >> 2 & 0b11
x = (data[1] * 2**3) + (data[0] & 0b111)
y = (data[3] * 2**3) + (data[2] & 0b111)
await make_touch(finguer, state, x, y)
return True
else:
return False
async def control_server(reader, writer):
buffer = bytearray(b'')
while not reader.at_eof():
data = await reader.read(1)
if data == b'\n':
await message_control(buffer)
buffer.clear()
#reader._buffer.clear()
elif data:
buffer.append(data[0])
#await writer.drain()
writer.close()
loop = asyncio.get_event_loop()
coro = asyncio.start_server(control_server, '0.0.0.0', 7800, loop=loop)
server = loop.run_until_complete(coro)
# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()