Today I wanted to make some tests with my new 1 euro joystick. After a quick search on Google I found that the python library pygame allows for direct polling of joysticks. The counterpart for Java is the Jinput library.
- Install python2-pygame
- Copy the following code to test your joysticks
""" Joystick Management - Author: Andrea Monacchi - Date: 2014, February 16th """ from pygame import * class JoystickMan: def __init__(self): init() self.joysticks = [] def get_joystic_count(self): return joystick.get_count() def connect_joystick(self, id): joy = joystick.Joystick(id) joy.init() if joy.get_init() == 1: print "Joystick is initialized" self.joysticks.append(joy) def connect_all_joysticks(self): for i in range(self.get_joystic_count()): joy = joystick.Joystick(i) joy.init() self.joysticks.append(joy) def print_info(self): for i, joy in enumerate(self.joysticks): print "Joystick #", i," ID: ",joy.get_id(),", Name: ",joy.get_name() print "#Axes: ", joy.get_numaxes() print "#Trackballs: ", joy.get_numballs() print "#Buttons: ", joy.get_numbuttons() print "#Hat controls: ", joy.get_numhats() def terminate(self): quit() # quits pygame def run(self): while 1: for e in event.get(): # Event list at: http://www.pygame.org/ftp/contrib/input.html if e.type is JOYBUTTONDOWN: name = 'joystick%d-button%d' % (e.joy, e.button) print name elif e.type is JOYBUTTONUP: name = 'joystick%d-button%d-up' % (e.joy, e.button) print name elif e.type is JOYAXISMOTION: name = 'joystick%d-axis%d' % (e.joy, e.axis) print name, e.value elif e.type is JOYBALLMOTION: name = 'joystick%d-ball%d' % (e.joy, e.hat) print name, e.rel elif e.type is JOYHATMOTION: name = 'joystick%d-hat%d' % (e.joy, e.hat) print name, e.value if __name__ == "__main__": try: j = JoystickMan() #j.connect_joystick(0) j.connect_all_joysticks() j.print_info() j.run() except KeyboardInterrupt: print "terminating" j.terminate()
No comments:
Post a Comment