def run(self, verbose=False): # clean-up while True: self.read(verbose) sdl2.SDL_JoystickClose(self.js) print('Bye ...')
def __del__(self): if self.game_controller is not None: if sdl2.SDL_GameControllerGetAttached(self.game_controller): sdl2.SDL_GameControllerClose(self.game_controller) else: if sdl2.SDL_JoystickGetAttached(self.joystick): sdl2.SDL_JoystickClose(self.joystick)
def close(self): """Close the joystick.""" try: sdl2.SDL_GameControllerClose(self.gamecontroller) except: pass try: sdl2.SDL_JoystickClose(self.joystick) except: pass
def read(self): """Read all the sticks and switches""" # Force an update of the joystick channels sdl2.SDL_PumpEvents() # Ensure that the Tx is connected if sdl2.SDL_NumJoysticks() == 0: if self.joystick: sdl2.SDL_JoystickClose(self.joystick) self.joystick = None return False elif not self.joystick: # Open the first joystick if necessary self.joystick = sdl2.SDL_JoystickOpen(0) # Read all the channels ret = [] for channel in range(self.channels): val = sdl2.SDL_JoystickGetAxis(self.joystick, channel) ret.append(int(((val / 65536.0) + 0.5) * 800.0 + 1100.0)) # Return the list of values return ret
print " Control Panel has Win ID: " + str(joystick["Win ID"]) joystick["Res Name"] = "Control Panel" # Resolve the vJoy devices (vJoy2 has 49 buttons rather than 50) print "\n-- RESOLVING VJOYS --" for joystick in joysticksSorted: if joystick["Name"] == "vJoy Device" and joystick["Buttons"] == 50: joystick["Res Name"] = "vJoy 1" print " vJoy 1 has Win ID: " + str(joystick["Win ID"]) elif joystick["Name"] == "vJoy Device" and joystick["Buttons"] == 49: print " vJoy 2 has Win ID: " + str(joystick["Win ID"]) joystick["Res Name"] = "vJoy 2" # Done with the joysticks - close them. for joystick in joysticksSorted: sdl2.SDL_JoystickClose(joystick["Obj"]) # printJoyReport() # Lets check the JG XML print "\n-- PARSING THE JOYSTICK GREMLIN MAPPING XML --" try: xmldoc = minidom.parse(JGXML) except: print " OOPS: Can't open the Joystick Gremlin XML." sys.exit(1) devices = xmldoc.getElementsByTagName('device') for device in devices: if device.getAttribute("name") == "T.16000M": axes = device.getElementsByTagName('axis') for axis in axes:
# pro['lt2'] = sdl2.SDL_JoystickGetAxis(js,6) pro['lt2'] = sdl2.SDL_JoystickGetButton(js,6) # right trigger axis # pro['rt2'] = sdl2.SDL_JoystickGetAxis(js,7) pro['rt2'] = sdl2.SDL_JoystickGetButton(js,7) # get buttons pro['Y'] = sdl2.SDL_JoystickGetButton(js,0) pro['B'] = sdl2.SDL_JoystickGetButton(js,1) pro['A'] = sdl2.SDL_JoystickGetButton(js,2) pro['X'] = sdl2.SDL_JoystickGetButton(js,3) pro['lt1'] = sdl2.SDL_JoystickGetButton(js,4) pro['rt1'] = sdl2.SDL_JoystickGetButton(js,5) # use share button as a quit quit = sdl2.SDL_JoystickGetButton(js,8) # get hat pro['hat'] = sdl2.SDL_JoystickGetHat(js,0) prettyPrintPRO(pro) if quit == True: break time.sleep(0.3) sdl2.SDL_JoystickClose(js) print 'Bye ...'
def __del__(self): # clean-up sdl2.SDL_JoystickClose(self.js) print('Bye ...')
def run(self, verbose, rate): js = self.js dt = 1.0/rate ps4 = Msg.Joystick() while True: try: sdl2.SDL_JoystickUpdate() # left axis x = sdl2.SDL_JoystickGetAxis(js, 0) / 32768 y = sdl2.SDL_JoystickGetAxis(js, 1) / 32768 ps4.axes.leftStick = [x, y] # right axis x = sdl2.SDL_JoystickGetAxis(js, 2) / 32768 y = sdl2.SDL_JoystickGetAxis(js, 5) / 32768 ps4.axes.rightStick = [x, y] # other axes ps4.axes.L2 = sdl2.SDL_JoystickGetAxis(js, 3) / 32768 ps4.axes.R2 = sdl2.SDL_JoystickGetAxis(js, 4) / 32768 # accels x = sdl2.SDL_JoystickGetAxis(js, 6) / 32768 y = sdl2.SDL_JoystickGetAxis(js, 7) / 32768 z = sdl2.SDL_JoystickGetAxis(js, 8) / 32768 ps4.axes.accels = [x, y, z] # gyros x = sdl2.SDL_JoystickGetAxis(js, 9) / 32768 y = sdl2.SDL_JoystickGetAxis(js, 10) / 32768 z = sdl2.SDL_JoystickGetAxis(js, 11) / 32768 ps4.axes.gyros = [x, y, z] # get buttons ps4.buttons.s = sdl2.SDL_JoystickGetButton(js, 0) ps4.buttons.x = sdl2.SDL_JoystickGetButton(js, 1) ps4.buttons.o = sdl2.SDL_JoystickGetButton(js, 2) ps4.buttons.t = sdl2.SDL_JoystickGetButton(js, 3) ps4.buttons.L1 = sdl2.SDL_JoystickGetButton(js, 4) ps4.buttons.R1 = sdl2.SDL_JoystickGetButton(js, 5) ps4.buttons.L2 = sdl2.SDL_JoystickGetButton(js, 6) ps4.buttons.R2 = sdl2.SDL_JoystickGetButton(js, 7) ps4.buttons.share = sdl2.SDL_JoystickGetButton(js, 8) ps4.buttons.options = sdl2.SDL_JoystickGetButton(js, 9) ps4.buttons.L3 = sdl2.SDL_JoystickGetButton(js, 10) ps4.buttons.R3 = sdl2.SDL_JoystickGetButton(js, 11) ps4.buttons.ps = sdl2.SDL_JoystickGetButton(js, 12) ps4.buttons.pad = sdl2.SDL_JoystickGetButton(js, 13) # get hat # [up right down left] = [1 2 4 8] ps4.buttons.hat = sdl2.SDL_JoystickGetHat(js, 0) # print('b 12', sdl2.SDL_JoystickGetButton(js, 12)) # print('b 13', sdl2.SDL_JoystickGetButton(js, 13)) if verbose: print(Msg.Joystick.screen(ps4)) self.pub.pub('js', ps4) time.sleep(dt) except (IOError, EOFError): print('[-] Connection gone .... bye') break # except Exception as e: # print('Ooops:', e) # else: # raise Exception('Joystick: Something bad happened!') # clean-up sdl2.SDL_JoystickClose(js) print('Bye ...')
def poll_sdl_events(self): import ctypes as c sdl.SDL_JoystickEventState(sdl.SDL_ENABLE) self.num_gamepads = sdl.SDL_NumJoysticks() self.active_gamepads = {} while self.pending: event = sdl.SDL_Event() while sdl.SDL_PollEvent(c.byref(event)): if event.type == sdl.SDL_JOYBUTTONDOWN: button = event.jbutton self.gamepad_input = button.which self.gamepad_pressed = button.button self.gamepad_type = "button" elif event.type == sdl.SDL_JOYAXISMOTION: axis = event.jaxis if event.jaxis.value < -16000: self.gamepad_input = axis.which self.gamepad_pressed = axis.axis self.gamepad_type = "Naxis" elif event.jaxis.value > 16000: self.gamepad_input = axis.which self.gamepad_pressed = axis.axis self.gamepad_type = "Paxis" elif event.type == sdl.SDL_JOYDEVICEADDED: n = event.jdevice.which device = sdl.SDL_JoystickOpen(n) joy_id = sdl.SDL_JoystickInstanceID(device) self.active_gamepads[joy_id] = device name = sdl.SDL_JoystickName(device).decode("utf-8") page = 1 while page < 5: self.pages_list[page][1].insert( n, str(n), str(n) + ": " + name) if self.gamepads_stored[page][1] == name: self.pages_list[page][1].set_active_id( str(self.gamepads_stored[page][0])) page += 1 log.info(f"Controller added: {name}") log.info( f"Current active controllers: {sdl.SDL_NumJoysticks()}" ) elif event.type == sdl.SDL_JOYDEVICEREMOVED: joy_id = event.jdevice.which device = self.active_gamepads[joy_id] if sdl.SDL_JoystickGetAttached(device): sdl.SDL_JoystickClose(device) name = sdl.SDL_JoystickName(device).decode("utf-8") page = 1 while page < 5: if self.gamepads_stored[page][1] == name: self.pages_list[page][1].set_active_id("-1") self.pages_list[page][1].remove( self.gamepads_stored[page][0]) page += 1 log.info(f"Controller removed: {name}") self.active_gamepads.pop(joy_id) log.info( f"Current active controllers: {sdl.SDL_NumJoysticks()}" )
def close_joystick(self): sdl2.SDL_JoystickClose(self.joy)
def __del__(self): # clean-up sdl2.SDL_JoystickClose(self.js)