Pygame: detect joystick disconnect and wait for it to reconnect

I am using the pygame.joystick.Joystick object and want to be able to print a message asking pygame.joystick.Joystick to reconnect the USB joystick after disconnecting it.

right now I have (approximately):

 js = pygame.joystick.Joystick(0) #... some game code and stuff pygame.joystick.quit() pygame.joystick.init() while pygame.joystick.get_count() == 0: print 'please reconnect joystick' pygame.joystick.quit() pygame.joystick.init() js = pygame.joystick.Joystick(0) js.init() 

but it doesnโ€™t recover correctly, idk what exactly it does, but it is definitely wrong. Any direction on this would be helpful.

+4
source share
2 answers

I had to run the old xbox, but I made a function that checks for disconnects and seems to work fine:

 discon = False def check_pad(): global discon pygame.joystick.quit() pygame.joystick.init() joystick_count = pygame.joystick.get_count() for i in range(joystick_count): joystick = pygame.joystick.Joystick(i) joystick.init() if not joystick_count: if not discon: print "reconnect you meat bag" discon = True clock.tick(20) check_pad() else: discon = False 

So, if you run this function in your main loop, it will just continue working until it gets a connection to the joystick. It works for a little test code that I found:

http://programarcadegames.com/python_examples/show_file.php?file=joystick_calls.py

Also found:

http://demolishun.net/?p=21

Where I stole this idea, it had no code examples that were lame

And finally, because you should always check documents:

http://www.pygame.org/docs/ref/joystick.html

+1
source

I managed to get my work with Noelkdโ€™s offer, but I had a similar problem described by Ryan Heinin

At first I had something like this, but it does not work, because it loses traces of the action of the gamepads with all quiting and initing. This works first to check if the controller is connected at all, but not to check efficiently during operation

I also had this problem. I think you're right, when you call quit too often, it does not give the pad enough time to reinitialize - at least on my computer. I found that if you limit the number of calls per second, this works.

This can lead to a temporary disconnection of the player's input, so any calls to joystick will not work.

It is best to run this code only if you find that for a while there was no input (say, 5 seconds or something else). This way you wonโ€™t quit when the user actually uses the device

 import pygame import time INACTIVITY_RECONNECT_TIME = 5 RECONNECT_TIMEOUT = 1 class ControllerInput(): def __init__(self): pygame.joystick.init() self.lastTime = 0 self.lastActive = 0 def getButtons(self, joystickId): joystick = pygame.joystick.Joystick(joystickId) joystick.init() buttons = {} for i in range(joystick.get_numbuttons()): buttons[i] = joystick.get_button(i) if buttons[i]: self.lastActive = time.time() return buttons def hasController(self): now = time.time() if now - self.lastActive > INACTIVITY_RECONNECT_TIME and now - self.lastTime > RECONNECT_TIMEOUT: self.lastTime = now pygame.joystick.quit() pygame.joystick.init() return pygame.joystick.get_count() > 0 

Using

 # ... some constructor controller = ControllerInput() # ... game loop if not controller.hasController(): # handle disconnect print('reconnect') return buttons = controller.getButtons(0) if buttons[0]: # buttons[0] was pressed! 
0
source

All Articles