def run(): sdl2.ext.init() sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK) window = sdl2.ext.Window("Controller test", size=(640, 480)) window.show() running = True if sdl2.joystick.SDL_NumJoysticks() < 1: print("No joysticks plugged in") return 0 joystick = sdl2.SDL_JoystickOpen(0) print("Name:", sdl2.SDL_JoystickName(joystick)) print("NumAxes", sdl2.SDL_JoystickNumAxes(joystick)) print("Trackballs:", sdl2.SDL_JoystickNumBalls(joystick)) print("Buttons:", sdl2.SDL_JoystickNumButtons(joystick)) print("Hats:", sdl2.SDL_JoystickNumHats(joystick)) print("Haptic?:", sdl2.SDL_JoystickIsHaptic(joystick)) #sdl2.SDL_JoystickClose(joystick) #return 0 while (running): events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_JOYAXISMOTION: print("=======================") for axis in range(sdl2.SDL_JoystickNumAxes(joystick)): print("Axis: %i, value: %i" % (axis, sdl2.SDL_JoystickGetAxis(joystick, axis))) if event.type == sdl2.SDL_JOYBUTTONDOWN: print("=======================") for button in range(sdl2.SDL_JoystickNumButtons(joystick)): print( "Button: %i, value: %i" % (button, sdl2.SDL_JoystickGetButton(joystick, button))) if event.type == sdl2.SDL_JOYHATMOTION: print("=======================") for hat in range(sdl2.SDL_JoystickNumHats(joystick)): print("Hat: %i, value: %i" % (hat, sdl2.SDL_JoystickGetHat(joystick, hat))) if event.type == sdl2.SDL_QUIT: running = False break window.refresh() return 0
def direction(self): return util.convert_sdl_hat(sdl2.SDL_JoystickGetHat( self._joystick, self._index) )
# 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 get(self): if not self.valid: return None js = self.js sdl2.SDL_JoystickUpdate() share = sdl2.SDL_JoystickGetButton(js, 8) if share: exit(0) ls = Axis(sdl2.SDL_JoystickGetAxis(js, 0), sdl2.SDL_JoystickGetAxis(js, 1)) rs = Axis(sdl2.SDL_JoystickGetAxis(js, 2), sdl2.SDL_JoystickGetAxis(js, 5)) triggers = Trigger(sdl2.SDL_JoystickGetAxis(js, 3), sdl2.SDL_JoystickGetAxis(js, 4)) shoulder = [ sdl2.SDL_JoystickGetButton(js, 4), sdl2.SDL_JoystickGetButton(js, 5) ] stick = [ sdl2.SDL_JoystickGetButton(js, 10), sdl2.SDL_JoystickGetButton(js, 11) ] # I seem to have lost sensors # a = Sensors( # sdl2.SDL_JoystickGetAxis(js, 6), # sdl2.SDL_JoystickGetAxis(js, 7), # sdl2.SDL_JoystickGetAxis(js, 8) # ) # # g = Sensors( # sdl2.SDL_JoystickGetAxis(js, 9), # sdl2.SDL_JoystickGetAxis(js, 10), # sdl2.SDL_JoystickGetAxis(js, 11) # ) b = [ sdl2.SDL_JoystickGetButton(js, 0), # square sdl2.SDL_JoystickGetButton(js, 3), # triangle sdl2.SDL_JoystickGetButton(js, 2), # circle sdl2.SDL_JoystickGetButton(js, 1) # x ] hat = sdl2.SDL_JoystickGetHat(js, 0) share = sdl2.SDL_JoystickGetButton(js, 8) options = sdl2.SDL_JoystickGetButton(js, 9) ps4 = PS4(ls, rs, triggers, hat, shoulder, stick, b, options, share) # left axis # x = sdl2.SDL_JoystickGetAxis(js, 0) / 32768 # y = sdl2.SDL_JoystickGetAxis(js, 1) / 32768 # ps4['leftStick'] = [x, y] # # # right axis # x = sdl2.SDL_JoystickGetAxis(js, 2) / 32768 # y = sdl2.SDL_JoystickGetAxis(js, 5) / 32768 # ps4['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)) return ps4
def hat(self, index=0): return SDL.SDL_JoystickGetHat(self.raw, index)
def read(self, verbose=False): js = self.js dt = self.sleep ps4 = Msg.Joystick() twist = Msg.Twist() 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)) #self.pub.pub('js', ps4) x, y = ps4.axes.rightStick twist.linear.set(x, y, 0) x, y = ps4.axes.leftStick twist.angular.set(x, y, 0) # verbose = True if verbose: print(twist) if (self.old_twist.angular == twist.angular) and (self.old_twist.linear == twist.linear): # print('js no msg') pass else: self.pub.pub(self.topic, twist) self.old_twist = twist # print('js message sent') time.sleep(dt) except (IOError, EOFError): print('[-] Connection gone .... 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 ...')
for i in range(0, sdl2.SDL_NumJoySticks()): joy = sdl2.SDL_JoystickOpen(i) self.joysticks.append(joy) print("joystick initialized") """ joy = sdl2.SDL_JoystickOpen(0) while True: sdl2.SDL_PumpEvents() data = [] for i in range(0, 6): data.append(int(sdl2.SDL_JoystickGetAxis(joy, i))) for i in range(0, 11): data.append(int(sdl2.SDL_JoystickGetButton(joy, i))) data.append(int(sdl2.SDL_JoystickGetHat(joy, 0))) msg = json.dumps(data) jsnode.send("inputs-out", msg) time.sleep(0.05) print(data) # Data array format: ## 16 bit signed ints: # 0 | right joystick x # 1 | right joystick y # 2 | right trigger # 3 | left joystick x # 4 | left joystick y # 5 | left trigger ## 1 bit (0 = unpressed, 1 = pressed) # 6 | A
def get(self): if not self.valid: return None js = self.js sdl2.SDL_JoystickUpdate() ls = Axis(sdl2.SDL_JoystickGetAxis(js, 0), sdl2.SDL_JoystickGetAxis(js, 1)) rs = Axis( sdl2.SDL_JoystickGetAxis(js, 3), # 2 sdl2.SDL_JoystickGetAxis(js, 4) # 5 ) triggers = Trigger( sdl2.SDL_JoystickGetAxis(js, 2), # 3 sdl2.SDL_JoystickGetAxis(js, 5) # 4 ) # shoulder = [ # sdl2.SDL_JoystickGetButton(js, 4), # sdl2.SDL_JoystickGetButton(js, 5) # ] # stick = [ # sdl2.SDL_JoystickGetButton(js, 10), # sdl2.SDL_JoystickGetButton(js, 11) # ] b = ( sdl2.SDL_JoystickGetButton(js, 0), # square sdl2.SDL_JoystickGetButton(js, 3), # triangle sdl2.SDL_JoystickGetButton(js, 2), # circle sdl2.SDL_JoystickGetButton(js, 1) # x ) # print("6", sdl2.SDL_JoystickGetButton(js, 6)) # left trigger T/F # print("7", sdl2.SDL_JoystickGetButton(js, 7)) # right trigger T/F # for n in [10,11,12]: # print(n, sdl2.SDL_JoystickGetButton(js, n)) # # b_triggers = ( # sdl2.SDL_JoystickGetButton(js, 6)), # sdl2.SDL_JoystickGetButton(js, 7)), # ) ps = sdl2.SDL_JoystickGetButton(js, 10) lb = ( sdl2.SDL_JoystickGetButton(js, 4), # L1 sdl2.SDL_JoystickGetButton(js, 6), # L2 sdl2.SDL_JoystickGetButton(js, 11) # L3 ) rb = ( sdl2.SDL_JoystickGetButton(js, 5), # R1 sdl2.SDL_JoystickGetButton(js, 7), # R2 sdl2.SDL_JoystickGetButton(js, 12) # R3 ) hat = sdl2.SDL_JoystickGetHat(js, 0) share = sdl2.SDL_JoystickGetButton(js, 8) option = sdl2.SDL_JoystickGetButton(js, 9) # 'leftStick rightStick triggers leftButtons rightButtons hat buttons option share ps' ps4 = PS4(ls, rs, triggers, lb, rb, hat, b, option, share, ps) return ps4