-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader_joystick.py
executable file
·49 lines (41 loc) · 1.59 KB
/
reader_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
#Python... The worst thing ever. Never again.
import pygame
from pygame import locals
import socket
import time
from time import sleep
pygame.init()
pygame.joystick.init()
try:
j = pygame.joystick.Joystick(0)
j.init()
except pygame.error:
print 'no joystick found.'
UDP_IP = "127.0.0.1"
#UDP_IP = "192.168.5.1"
UDP_PORT1 = 1235
UDP_PORT2 = 1236
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
loop = 1000 / 20
updates = 0
delta = 0
while 1:
start_time = int(round(time.time() * 1000))
for e in pygame.event.get(): # iterate over event stack
if e.type == pygame.locals.JOYAXISMOTION: # 7
x, y, z, a = j.get_axis(0)*100, j.get_axis(1)*100, j.get_axis(2)*100, j.get_axis(3)*100
MESSAGE = str(int(x)) + ';' + str(int(y)) + ';' + str(int(z)) + ';' + str(int(a))
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT1))
if e.type == pygame.locals.JOYBUTTONDOWN: # 10
a, b, c, d, e, f, g, h, i, v, k, l = j.get_button(0), j.get_button(1), j.get_button(2), j.get_button(3), j.get_button(4), j.get_button(5), j.get_button(6), j.get_button(7), j.get_button(8), j.get_button(9), j.get_button(10), j.get_button(11)
MESSAGE = str(a) + ';' + str(b) + ';' + str(c) + ';' + str(d) + ';' + str(e) + ';' + str(f) + ';' + str(g) + ';' + str(h) + ';' + str(i) + ';' + str(v) + ';' + str(k) + ';' + str(l) + '\n\r'
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT2))
end_time = int(round(time.time()*1000))
difference = end_time - start_time
if difference > 0:
sleep((loop - difference) / 1000.0)
updates = updates + 1
delta = delta + difference
if updates == 20:
updates = 0
#I'm still having nightmares because of this.