Last active
April 8, 2016 15:19
-
-
Save adnan2911/a4ecac9561a690aea5a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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