forked from Beherith/camera_joystick_springrts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-joystick.py
86 lines (74 loc) · 2.46 KB
/
send-joystick.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
import socket, time
import pygame
from socketserver import BaseRequestHandler, TCPServer
import json
# Main configuration
TCP_IP = "127.0.0.1" # Localhost
TCP_PORT = 51234 # This port match the ones using on other scripts
update_rate = 0.0166666 # 60 hz loop cycle
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
numbuttons = 0
numaxes = 0
numhats = 0
joyname = ''
try:
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()
numbuttons = joystick.get_numbuttons()
numaxes = joystick.get_numaxes()
numhats = joystick.get_numhats()
print ("Found a joystick:", joystick.get_name(),", with", numaxes,"axes and",numbuttons,"buttons", numhats, 'hats')
except Exception as error:
print ("No joystick connected on the computer, " + str(error))
messagestr = "empty"
class handler(BaseRequestHandler):
def handle(self):
print ("Starting handler")
i = 0
while True:
current = time.time()
elapsed = 0
pygame.event.pump()
i += 1
joydata = {'time': current}
joydata['axes'] = [0.0] * numaxes
joydata['buttons'] = [0] * numbuttons
joydata['hats'] = [0.0] * 2
for ax in range(numaxes):
joydata['axes'][ax] = joystick.get_axis(ax)
for but in range(numbuttons):
joydata['buttons'][but] = joystick.get_button(but)
for hat in range(min(1,numhats)):
joydata['hats'] = joystick.get_hat(hat)
if i%60 == 0 :
print(i, current, 'buttons = ', joydata['buttons'], 'axes = ', joydata['axes'], 'hats = ', joydata['hats'])
msgstr = json.dumps(joydata)
# sock.sendto(json.dumps(joydata),(TCP_IP,TCP_PORT))
# Make this loop work at update_rate
while elapsed < update_rate:
elapsed = time.time() - current
time.sleep(0.0001) # 100us?
self.request.send(str.encode(msgstr))
with TCPServer(("",51234),handler) as server:
server.timeout = 0.5
server.serve_forever(poll_interval= 0.016)
while True:
current = time.time()
elapsed = 0
pygame.event.pump()
joydata = {'time':current}
joydata['axes'] = [0.0] * numaxes
joydata['buttons'] = [0] * numbuttons
for ax in range(numaxes):
joydata['axes'][ax] = joystick.get_axis(ax)
for but in range(numbuttons):
joydata['buttons'][but] = joystick.get_button(but)
print (current, 'buttons = ',joydata['buttons'], 'axes = ',joydata['axes'])
msgstr = json.dumps(joydata)
#sock.sendto(json.dumps(joydata),(TCP_IP,TCP_PORT))
# Make this loop work at update_rate
while elapsed < update_rate:
elapsed = time.time() - current
time.sleep(0.0001) # 100us?