def readAxis(axis): """Takes axis as an argument (4 axis- X, Y, Z and R), and returns the value at that instance.""" #Note: TECHNICALLY 6 axis - there is also a U and V axis if isConnected(): return j.get_axis_position(0, j.__getattribute__(j, axis)) #this is lazy and may not work; TEST! else: pass
def update(self): self.count += 1 try: j.update() #self.y.emit(readAxis('Y') * -4) y = readAxis('Y') * -4 x = readAxis("X") * -4 #x_val = random.randint(0, 100) #self.x.emit(x_val) #self.z.emit(readAxis('Z') * -4) #z = readAxis('Z') * -4 #self.u.emit(readAxis('U') * -4) #u = readAxis('U') * -4 #self.r.emit(readAxis('R') * -4) #r = readAxis('R') * -4 #self.v.emit(readAxis('V') * -4) #v = readAxis('Z') * -4 b0 = readButton(0) #On Rock Candy, corresponds to Button A b4 = readButton(4) #On Rock Candy, corresponds to left bumper b5 = readButton(5) #On Rock Candy, corresponds to right bumper b3 = readButton(3) #on rock candy, Y button b8 = readButton(8) #XY-joystick button on Rock Candy Controller #Here we doctor the values on the controller z = 0 x = -x y = -y sol1 = 0 sol2 = 0 if b4: #temporary workaround to the fact Z is retarded on my controller z = 100 elif b5: z = -100 if b8: x = x / 2 y = y / 2 if b0: sol1 = 100 if b3: sol2 = 100 ## self.conn.send( "{\"X\":%d,\"Y\":%d,\"Z\":%d,\"S1\":%d,\"S2\":%d}~" % (x, y, z, sol1, sol2)) ##HEREIN LIES THE PROBLEM?! #self.conn.send("{\X\":0,\"Y\":0,\"Z\":0}~") time.sleep(0.1) # output = self.conn.recv(2048) # if '#' in output: # raise Exception("LOST MEANINGFUL CONNECTION WITH SERVER.") except: print "ERROR CAUGHT." self.server.listen(1) print "RECONNECTING...." self.conn, self.address = self.server.accept() if self.count % 5 == 0: print "PRODUCED VALUES : {\"X\":%d,\"Y\":%d,\"Z\":%d} " % (x, y, z)
def __init__(self, UI): self.Joystick = 0 self.deadZone = 15 self.minValue = 0 self.maxValue = 400 foundController = False UI.textwrite( 50, 250, "Looking for controller, press A to choose the controller", 10, 10, 10, 50) UI.update() while not foundController: for i in range(0, 5): Joystick.update() if Joystick.is_connected(i): Joystick.update() if Joystick.is_button_pressed(i, A): foundController = True self.joystick = i UI.shouldQuit() UI.textdelete( 50, 250, "Looking for controller, press A to choose the controller", 50) UI.textwrite(450, 250, "Found " + str(self.joystick), 10, 10, 10, 50) UI.update() time.sleep(2) UI.textdelete(450, 250, "Found " + str(self.joystick), 50)
R_JOYSTICK_CLICK_HEX = 0x400 if platform == "linux" or platform == "linux2": L_JOYSTICK_CLICK = 9 R_JOYSTICK_CLICK = 10 WIN_BUTTON = 8 #loop to find the correct controller joystick = 0 foundController = False print "looking for controller, press A to chose the controller" while not foundController: for i in range(0, 5): Joystick.update() if Joystick.is_connected(i): Joystick.update() if Joystick.is_button_pressed(i, A): foundController = True joystick = i print "found controller ", i print "buttons: ", Joystick.get_button_count(i) # used inside the class, not necessary to call from outside this class, use the other calls def getAxis(joyStickNumber, axis): size = maxValue - minValue return ((applyDeadZone(Joystick.get_axis_position(joyStickNumber, axis)) /
def update(self): self.count += 1 try: j.update() #self.y.emit(readAxis('Y') * -4) y = readAxis('Y') * -4 x = readAxis("X") * -4 z = readAxis("Z") * -4 #x_val = random.randint(0, 100) #self.x.emit(x_val) #self.z.emit(readAxis('Z') * -4) #z = readAxis('Z') * -4 #self.u.emit(readAxis('U') * -4) #u = readAxis('U') * -4 #self.r.emit(readAxis('R') * -4) #r = readAxis('R') * -4 #self.v.emit(readAxis('V') * -4) #v = readAxis('Z') * -4 b0 = readButton(0) #On Rock Candy, corresponds to Button A b4 = readButton(4) #On Rock Candy, corresponds to left bumper b5 = readButton(5) #On Rock Candy, corresponds to right bumper b3 = readButton(3) #on rock candy, Y button b8 = readButton(8) #XY-joystick button on Rock Candy Controller #Here we doctor the values on the controller z = -z #used to be z = 0, is z = z with xbox controller x=-x y=-y #power budget fix #x = x/1.5 #y = y/1.5 #z = z/1.5 sol1 = 0 sol2 = 0 if b4: #temporary workaround to the fact Z is retarded on my controller z = 100 elif b5: z = -100 if b8: # revert to b8 x = x/2 y = y/2 if b0: sol1 = 100 if b3: sol2 = 100 ## self.conn.send("{\"X\":%d,\"Y\":%d,\"Z\":%d,\"S1\":%d,\"S2\":%d}~"%(x, y, z, sol1, sol2)) ##HEREIN LIES THE PROBLEM?! #self.conn.send("{\X\":0,\"Y\":0,\"Z\":0}~") time.sleep(0.1) # output = self.conn.recv(204 # if '#' in output: # raise Exception("LOST MEANINGFUL CONNECTION WITH SERVER.") except: print "ERROR CAUGHT." self.server.listen(1) print "RECONNECTING...." self.conn, self.address = self.server.accept() if self.count % 5 == 0: print "PRODUCED VALUES : {\"X\":%d,\"Y\":%d,\"Z\":%d} "%(x, y, z)
def isConnected(): """Returns True if joystick is connected.""" return j.is_connected(0)
def getAxis(joyStickNumber, axis): size = maxValue - minValue return ((applyDeadZone(Joystick.get_axis_position(joyStickNumber, axis))/(100.0-deadZone)) * size) - minValue
def isConnected(): return Joystick.is_connected(joystick)
def update(): Joystick.update()
def update(self): Joystick.update()
def getNumButtons(self): return Joystick.get_button_count(self.joystick)
def getButton(self, button): return Joystick.is_button_pressed(self.joystick, button)
def isConnected(self): return Joystick.is_connected(self.joystick)
def getNumButtons(): return Joystick.get_button_count(joystick)
def getButton(button): return Joystick.is_button_pressed(joystick, button)
def getAxis(self, joyStickNumber, axis): size = self.maxValue - self.minValue return ((self.applyDeadZone( Joystick.get_axis_position(joyStickNumber, axis)) / (100.0 - self.deadZone)) * size) - self.minValue
def getAxis(joyStickNumber, axis): size = maxValue - minValue return ((applyDeadZone(Joystick.get_axis_position(joyStickNumber, axis)) / (100.0 - deadZone)) * size) - minValue
R_JOYSTICK_CLICK_HEX = 0x400 if platform == "linux" or platform == "linux2": L_JOYSTICK_CLICK = 9 R_JOYSTICK_CLICK = 10 WIN_BUTTON = 8 #loop to find the correct controller joystick = 0; foundController = False print "looking for controller, press A to chose the controller" while not foundController: for i in range(0,5): Joystick.update() if Joystick.is_connected(i): Joystick.update() if Joystick.is_button_pressed(i, A): foundController = True joystick = i print "found controller ", i print "buttons: ", Joystick.get_button_count(i) # used inside the class, not necessary to call from outside this class, use the other calls def getAxis(joyStickNumber, axis): size = maxValue - minValue return ((applyDeadZone(Joystick.get_axis_position(joyStickNumber, axis))/(100.0-deadZone)) * size) - minValue
def readButton(button): """Takes a button value (buttons 1-8) and returns whether or not it is pushed.""" if isConnected(): return j.is_button_pressed(0, button) else: pass
def GUIrun(): background.blit(text, textpos) screen.blit(background, (0, 0)) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: quit() while not foundController: GUIrun() for i in range(0,5): Joystick.update() if Joystick.is_connected(i): Joystick.update() if Joystick.is_button_pressed(i, A): foundController = True joystick = i writeonscreen = "Found controller " + str(i) background.fill((250, 250, 250)) font = pygame.font.Font(None, 50) text = font.render(writeonscreen, 1, (10, 10, 10)) textpos = text.get_rect() textpos.centerx = background.get_rect().centerx textpos.centery = background.get_rect().centery background.blit(text, textpos)