-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
90 lines (72 loc) · 2.7 KB
/
server.py
File metadata and controls
90 lines (72 loc) · 2.7 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
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
import socket
import time
import threading
from touchscreen import TouchScreen
def receive_data(conn, addr):
print(f'Connected by {addr}')
while True:
data = conn.recv(1024)
if data:
print(data.decode())
def send_data(conn, addr):
while True:
position = touchscreen.position()
message = f"{position[0]} {position[1]}"
conn.sendall(message.encode())
time.sleep(1) # Send a message every second
def handle_client(conn, addr):
print(f'Connected by {addr}')
while True:
data = conn.recv(1024)
if data:
data = data.decode().split()
print(data)
if data[0] == "0":
W,H = int(data[1]),int(data[2])
message = "0" # Starting calibration
conn.sendto(message.encode(), addr)
time.sleep(2)
touchscreen.calibrate_point(W, H, "tl")
message = "1" # Top-Left calibration complete
conn.sendto(message.encode(), addr)
time.sleep(2)
touchscreen.calibrate_point(W, H, "br")
message = "2" # Bottom-right calibration complete
touchscreen.recalibrate()
conn.sendto(message.encode(), addr)
elif data[0] == "1":
print("getting position")
position = touchscreen.pixels()
if position == -1:
message = f"{position}"
else:
message = f"{position[0]} {position[1]}"
conn.sendto(message.encode(), addr)
def send_position(conn, addr):
while True:
print("getting position")
position = touchscreen.pixels()
if position == -1:
message = f"{position}"
else:
message = f"{position[0]} {position[1]}"
conn.sendto(message.encode(), addr)
time.sleep(0.5)
def start_server(host='0.0.0.0', port=123):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind((host, port))
server_socket.listen()
print(f'Server listening on {host}:{port}')
conn, addr = server_socket.accept()
position_thread = threading.Thread(target=send_position, args=(conn, addr,))
handler_thread = threading.Thread(target=handle_client, args=(conn, addr,))
position_thread.start()
handler_thread.start()
handler_thread.join()
position_thread.join()
touchscreen = TouchScreen()
if __name__ == "__main__":
try:
start_server()
except KeyboardInterrupt:
touchscreen.cleanup()