Skip to content

Instantly share code, notes, and snippets.

@adnan2911
Last active April 8, 2016 15:19
Show Gist options
  • Save adnan2911/a4ecac9561a690aea5a0 to your computer and use it in GitHub Desktop.
Save adnan2911/a4ecac9561a690aea5a0 to your computer and use it in GitHub Desktop.
import pygame
from pygame import locals
import socket
import atexit
pygame.init()
pygame.joystick.init() # main joystick device system
try:
j = pygame.joystick.Joystick(0) # create a joystick instance
j.init() # init instance
print 'Enabled joystick: ' + j.get_name()
print 'Total axis: ' +str(j.get_numaxes())
print 'Total buttons: ' + str(j.get_numbuttons())
except pygame.error:
print 'no joystick found.'
TCP_IP = '192.168.10.44'
TCP_PORT = 50050
BUFFER_SIZE = 128 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
print "Listening..."
conn, addr = s.accept()
print 'Connection address:', addr
while 1:
for e in pygame.event.get(): # iterate over event stack
if e.type == pygame.locals.JOYAXISMOTION or e.type == pygame.locals.JOYBUTTONDOWN: # 7
conn.send(str((j.get_button(0)))+','+ str((j.get_button(1)))+','+ str((j.get_button(2)))+','+ str(round(1500 + ((j.get_axis(0))*1000/2),0))+','+ str(round(1500 + ((j.get_axis(1))*1000/2),0))+','+ str(round(1500 + ((j.get_axis(2))*1000/2),0)))
def exit_handler():
s.shutdown()
s.close()
atexit.register(exit_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment