def __init__(self,*addresses,**raw ): ## Accepts an arbitrary number of wiimote addresses as an argument ## Currently, any remotes past the first two are ignored, as the IR ## parser doesn't know what to do with them. Neither do I actually... self.adrs=addresses if len(self.adrs) ==1 and type(self.adrs[0]) == list: self.adrs = self.adrs[0] if len(self.adrs) ==0 : self.adrs = Wiimote3dTracker.search() self.wiimotes= [Wiimote(adr) for adr in self.adrs] self.coordinateTracker = CoordinateTrackerFactory(len(self.wiimotes),raw = raw) self.irParser = IRparser() self.polarListeners = [] self.cartesianListeners = [] ## set up the stuff needed for threading: threading.Thread.__init__ (self)
class Wiimote3dTracker(threading.Thread): useNormalisation = False def __init__(self,*addresses,**raw ): ## Accepts an arbitrary number of wiimote addresses as an argument ## Currently, any remotes past the first two are ignored, as the IR ## parser doesn't know what to do with them. Neither do I actually... self.adrs=addresses if len(self.adrs) ==1 and type(self.adrs[0]) == list: self.adrs = self.adrs[0] if len(self.adrs) ==0 : self.adrs = Wiimote3dTracker.search() self.wiimotes= [Wiimote(adr) for adr in self.adrs] self.coordinateTracker = CoordinateTrackerFactory(len(self.wiimotes),raw = raw) self.irParser = IRparser() self.polarListeners = [] self.cartesianListeners = [] ## set up the stuff needed for threading: threading.Thread.__init__ (self) def connect(self, listOfAddresses = None): ## Tries to connect to each address and returns true if everything went ok. ## listOfAddresses is an optional list of addresses to connect to print "Connecting to wiimotes..." if listOfAddresses: self.wiimotes= [Wiimote(adr) for adr in listOfAddresses] result = not False in [ wm.connect() for wm in self.wiimotes] if result: for i in range(len(self.wiimotes)): self.wiimotes[i].setLEDinBinary(i) return result ## searchForWiimotes() returns a function. ## classmethod means search is 'static' i,e. no instance ## of a Wiimote3dTracker is needed search = classmethod(searchForWiimotes()) def disconnect(self): # Disconnects from all wiimotes. self._Thread__stop for wm in self. wiimotes: wm.disconnect() print "Disconnecting" def run(self,pause = 0.004): while 1: self.refresh() time.sleep(pause) def refresh(self): ## refresh retrieves the data from each wiimote, parses them and ## sends them to whoever is listening via the refresh method. ## if any listener returns false, then we disconnect and quit. if all([wm.updated for wm in self.wiimotes]): data = [wm.getData() for wm in self.wiimotes] xys1, xys2 = self.irParser.parseWiiData( data ) if xys1 and xys2: self.coordinateTracker.process( xys1, xys2 ) result = True if len(self.cartesianListeners) > 0: pos1,pos2 = self.coordinateTracker.getListOfCartesianCoordinates() pos1,pos2 = self.scale(pos1),self.scale(pos2) for l in self.cartesianListeners: result &= (l.refresh(pos1,pos2) != False) ## The '!= False' allows listeners to return None (no return) ## as None != False if len(self.polarListeners) > 0: midInPolar = self.coordinateTracker.getMidpointInPolar() midInCart = self.coordinateTracker.getMidpointInCartesian() midInCart = self.scale( midInCart ) tilt = self.coordinateTracker.getTilt() yaw = self.coordinateTracker.getYaw() for l in self.polarListeners: result &= (l.refresh(midInPolar,midInCart,yaw,tilt) != False) ## The '!= False' allows listeners to return None (no return) ## as None != False ## if one of the listeners wants us to exit, then exit. if not result:# or self.irParser.checkButtonA(data): self.disconnect() import sys sys.exit() def scale(self,p): return p def calibrate(self,(minX,minY,minZ),(maxX,maxY,maxZ),duration = 5,integer = True):