def update(self): # sdl2.SDL_PumpEvents() # Pump, retreive events so they clear sdl2.SDL_JoystickUpdate() # Read all the joystick values axisScale = 1 / 2**15 self.axis = [ sdl2.SDL_JoystickGetAxis(self.joy, i) for i in range(sdl2.SDL_JoystickNumAxes(self.joy)) ] self.button = [ sdl2.SDL_JoystickGetButton(self.joy, i) for i in range(sdl2.SDL_JoystickNumButtons(self.joy)) ] self.roll = axisScale * self.axis[0] self.pitch = axisScale * self.axis[1] self.throttle = axisScale * self.axis[2] self.yaw = axisScale * self.axis[3] self.flap = 0.0 self.autoEngage = 2 * self.button[0] - 1 # "Soc-Engage-Switch" self.testMode = axisScale * self.axis[4] # "Test-Mode-Switch" self.testSel = axisScale * self.axis[5] # "Test-Select-Switch" self.trig = 2 * self.button[1] - 1 # "Trigger-Switch" self.thrSafe = 2 * self.button[2] - 1 # "Throttle-Safety-Switch" self.baseSel = axisScale * self.axis[ -1] # "Baseline-Select-Switch" (On Windows it came up as 6, on Linux 7...)
def button(self, index=0): state = SDL.SDL_JoystickGetButton(self.raw, index) if state == False: self._pressed[index] = False return False if state == True: if self._pressed[index]: return False else: self._pressed[index] = True return True
def get_gamepad_state(gp): # update joystick info sdl2.SDL_PumpEvents() gp_state = {} for stick in gp['sticks']: gp_state[stick] = scaled_stick_value(gp, gp['sticks'][stick]['id'], gp['sticks'][stick]['invert']) for btn in gp['btns']: gp_state[btn] = sdl2.SDL_JoystickGetButton(gp['gp_object'], gp['btns'][btn]) # This way a pressed button outputs a number equivalent # to a fully bent stick # print(gp_state) return gp_state
def getJoystickState(self): self.result = [] for i in range(self.numAxes): self.axis = sdl2.SDL_JoystickGetAxis(self.joystick, i) self.result.append(self.axis) for i in range(self.numButtons): self.button = sdl2.SDL_JoystickGetButton(self.joystick, i) self.result.append(self.button) self.result[0] = int(self.result[0] / SDL_AXIS_CONVERT) + 100 self.result[1] = -int(self.result[1] / SDL_AXIS_CONVERT) + 100 return self.result
def get_gamepad_state(gp): # update joystick info sdl2.SDL_PumpEvents() gp_state = {} for stick in gp['sticks']: gp_state[stick] = scaled_stick_value(gp, gp['sticks'][stick]['id'], gp['sticks'][stick]['invert']) for btn in gp['btns']: gp_state[btn] = sdl2.SDL_JoystickGetButton(gp['gp_object'], gp['btns'][btn]) #this way a pressed button outputs a number equivalent to a fully bent stick if OCULUS_ENABLED: gp_state['look_h'] = int(Y_AXIS * 10) # scaled_stick_value(gp,2,stick_max=300), gp_state['look_v'] = int(X_AXIS * -1.9) # scaled_stick_value(gp,3) * gp['invert_y'], return gp_state
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
"Hats": hats, "Obj": joy, } joysticks.append(joystick) joysticksSorted = sorted(joysticks, key=lambda k: k["Win ID"]) # Resolve the Sticks (Press Left Trigger) print "\n-- RESOLVING THE STICKS --" testRunning = True print " > Press the LEFT Trigger" while testRunning: for joystick in joysticksSorted: if joystick["Name"] == "T.16000M": sdl2.SDL_JoystickUpdate() if sdl2.SDL_JoystickGetButton(joystick["Obj"], 0): print " Left Stick has Win ID: " + str(joystick["Win ID"]) joystick["Res Name"] = "Left Stick" for otherJoystick in joysticksSorted: if otherJoystick["Name"] == "T.16000M" and otherJoystick[ "Res Name"] == None: otherJoystick["Res Name"] = "Right Stick" print " Right Stick has Win ID: " + str( otherJoystick["Win ID"]) testRunning = False # Label the Control Panel print "\n-- RESOLVING CONTROL PANEL --" for joystick in joysticksSorted: if joystick["Name"] == "Arduino Leonardo": print " Control Panel has Win ID: " + str(joystick["Win ID"])
def is_pressed(self): return sdl2.SDL_JoystickGetButton(self._joystick, self._index) == 1
} while True: sdl2.SDL_JoystickUpdate() # left axis pro['la']['x'] = sdl2.SDL_JoystickGetAxis(js,0) pro['la']['y'] = sdl2.SDL_JoystickGetAxis(js,1) # right axis pro['ra']['x'] = sdl2.SDL_JoystickGetAxis(js,2) pro['ra']['y'] = sdl2.SDL_JoystickGetAxis(js,3) # left trigger axis # 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)
return max(min(maxn, n), minn) # Main loop while True: sdl2.SDL_PumpEvents() # Read controller stick Y-inputs and normalize the value # to a range of -100 to 100 joy_1 = -sdl2.SDL_JoystickGetAxis(joystick, 1) / 327.67 joy_3 = -sdl2.SDL_JoystickGetAxis(joystick, 3) / 327.67 m1 = clamp(joy_1,-100,100) m2 = clamp(joy_3,-100,100) # Check left/right triggers (10,11) for 3rd motor control if sdl2.SDL_JoystickGetButton(joystick, 10): m0 = -30 elif sdl2.SDL_JoystickGetButton(joystick, 11): m0 = 30 else: m0 = 0 # Print values to console (mostly for debugging) print('m0: %d' % m0) print('m1: %d' % m1) print('m2: %d' % m2) # Build the list of three motor commands to send to NXT cmds = [] cmds.append(SetOutputState( 0, motor_on=True, set_power=m0, run_state=RunState.running,
def clamp(n, minn, maxn): return max(min(maxn, n), minn) while True: sdl2.SDL_PumpEvents() # read joystick input and normalise it to a range of -100 to 100 joy_x = (sdl2.SDL_JoystickGetAxis(joystick, 0) - 17268) / 172.68 joy_y = (sdl2.SDL_JoystickGetAxis(joystick, 1) - 17268) / 172.68 # use shoulder buttons on the game pad to make the omnibot rotate around it's axis. turnpower = 0 if sdl2.SDL_JoystickGetButton(joystick, 5): turnpower = 75 if sdl2.SDL_JoystickGetButton(joystick, 4): turnpower = -75 # convert joystick x and y to a direction and power (deviation from the centre) joy_direction = math.atan2(joy_y, joy_x) # in radians joy_power = (joy_x**2 + joy_y**2)**0.5 # pythagoras # building a list of three motor commands to send over cmds = [] for i in range(3): # for each motor the angle has a different offset (0, 120 and 240 degrees) angle = i * 2 * 3.1415 / 3 + joy_direction # motor power calculation. A simple sin. motorpower = math.sin(angle) * joy_power + turnpower
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 ...')
import sdl2 sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK) joystick = sdl2.SDL_JoystickOpen(0) joystick2 = sdl2.SDL_JoystickOpen(1) player = { 'gamepad': joystick2, 'stick_range': 65536, # max stick position - min stick position 'stick_center': 0, 'left_shoulder_btn': 8, 'right_shoulder_btn': 9 } while True: sdl2.SDL_PumpEvents() btns = {} for i in range(15): btns[i] = sdl2.SDL_JoystickGetButton(joystick2, i) joy_x = (sdl2.SDL_JoystickGetAxis(player['gamepad'], 0) - player['stick_center']) * 200 / player['stick_range'] print(joy_x, (sdl2.SDL_JoystickGetAxis(joystick2, 0) - 0) * 200 / 65000, sdl2.SDL_JoystickGetAxis(joystick2, 1), btns) if sdl2.SDL_JoystickGetButton(player['gamepad'], player['left_shoulder_btn']): break
running = 1 while running: sdl2.SDL_PumpEvents() for player in players: # read joystick input and normalise it to a range of -100 to 100 joy_x = (sdl2.SDL_JoystickGetAxis(player['gamepad'], 0) - player['stick_center']) * 200 / player['stick_range'] joy_y = (sdl2.SDL_JoystickGetAxis(player['gamepad'], 1) - player['stick_center'] ) * 200 / player['stick_range'] * player['invert_y'] # use shoulder buttons on the game pad to make the omnibot rotate around it's axis. turnpower = 0 if sdl2.SDL_JoystickGetButton(player['gamepad'], player['right_shoulder_btn']): turnpower = 75 if sdl2.SDL_JoystickGetButton(player['gamepad'], player['left_shoulder_btn']): turnpower = -75 # end the fun if button 1 is pressed. if sdl2.SDL_JoystickGetButton(player['gamepad'], player['abort_btn']): print 'stopping' running = 0 btns = {} for i in range(15): btns[i] = sdl2.SDL_JoystickGetButton(player['gamepad'], i) # print(joy_x, joy_y, turnpower, btns)
isMoving = False sdl2.SDL_Init(sdl2.SDL_INIT_TIMER | sdl2.SDL_INIT_JOYSTICK | sdl2.SDL_INIT_HAPTIC) joystick = sdl2.SDL_JoystickOpen(0) haptic = sdl2.SDL_HapticOpen(0) print("wheel - succesfully opened") efx = sdl2.SDL_HapticEffect(type=sdl2.SDL_HAPTIC_CONSTANT, constant= \ sdl2.SDL_HapticConstant(type=sdl2.SDL_HAPTIC_CONSTANT, direction= \ sdl2.SDL_HapticDirection(type=sdl2.SDL_HAPTIC_CARTESIAN, dir=(0, 0, 0)), \ length=sdl2.SDL_HAPTIC_INFINITY, level=0, attack_length=0, fade_length=0)) effect_id = sdl2.SDL_HapticNewEffect(haptic, efx) sdl2.SDL_HapticRunEffect(haptic, effect_id, 1) sdl2.SDL_HapticSetAutocenter(haptic, 0) sdl2.SDL_HapticSetGain(haptic, 100) # Wykrywanie poczÄ…tkowego biegu: if sdl2.SDL_JoystickGetButton(joystick, 14) == 1: gear = -1 elif sdl2.SDL_JoystickGetButton(joystick, 15) == 1: gear = 1 else: gear = 0 # capture = cv2.VideoCapture('http://192.168.0.11:8080/video') capture = cv2.VideoCapture('http://192.168.0.11:4747/video') # capture = WebcamVideoStream(src='http://192.168.0.11:4747/video').start() on_board_camera = True # mo_cap = cv2.VideoCapture('http://192.168.0.3:4747/video') mo_cap = cv2.VideoCapture(0) motion_capture = True
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 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
self.joysticks = [] 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)