-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
63 lines (40 loc) · 2.61 KB
/
Copy pathclient.py
File metadata and controls
63 lines (40 loc) · 2.61 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
import socket # Data transmission
from inputs import get_gamepad # Gamepad input
from time import sleep
# Auxiliary function for easy data sending
def send_data(host, port, data):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port)) # Connects to server...
sock.sendall(bytes(data, 'utf-8')) # ...and sends data
# Setup
host = '192.168.43.219' # Server IP
port = 12397 # Server port
stick_values_X = [] # Array used for collecting input events from the left analog stick (x axis)
stick_values_Y = [] # Array used for collecting input events from the left analog stick (y axis)
sleep(0.05) # Avoids lagging related problems
# Main loop
while True: # Listens for gamepad input events and sends them to server
events = get_gamepad() # Gets input events generated by the gamepad
if events[0].ev_type != 'Sync': # Only considers 'Key' type events
code = events[0].code # Event code
state = events[0].state # Event state (pressed, released or somewhere in between for non-binary buttons)
commands_to_send = 5 # Number of commands to send to server (needs to be reduced for the left analog stick to avoid lagging)
# Left analog stick (x axis)
if code == 'ABS_X':
stick_values_X.append(state) # Appends the value of an input to the stick_values_X array
if len(stick_values_X) == commands_to_send: # If the maximum number of events to collect before sending them to the server has been reached...
data = 'ABS_X' + ',' + str(stick_values_X[commands_to_send-1]) # ...then the last event is sent...
stick_values_X = [] # ...and the array is reset
send_data(host, port, data) # Actual sending of the data
# Left analog stick (y axis)
elif code == 'ABS_Y':
stick_values_Y.append(state) # Appends the value of an input to the stick_values_Y array
if len(stick_values_Y) == commands_to_send: # If the maximum number of events to collect before sending them to the server has been reached...
data = 'ABS_Y' + ',' + str(stick_values_Y[commands_to_send-1]) # ...then the last event is sent...
stick_values_Y = [] # ...and the array is reset
send_data(host, port, data) # Actual sending of the data
# Every other button
else:
data = str(code) + ',' + str(state) # Data to be sent
send_data(host, port, data) # Actual sending of the data
sleep(0.001) # Avoids lagging related problems