Ejemplo n.º 1
0
class WhiteBoard:
	running = False
	mouseclicked = False
	calibrating = False	
	
	KEEPLIGHTTIME = 3 #will not consider if we lose the light for x frames
	CLICKMAXTIME = 5 #longest that we will consider as a click for touchpad mode
	CLICKMINTIME = 3 # will not move the pointer for the first x frames after click to help a singleclic with shaky hand
	MAXTIMEBETWEENCLICKDRAG = 5 #...
	
	def __init__(self):
		self.mouse = MouseControl()
		self.XRES,self.YRES = self.mouse.get_screen_resolution()
		self.XRES, self.YRES = float(self.XRES), float(self.YRES)
		self.ABCD = [(0,0),(self.XRES-5,0),(0,self.YRES-5),(self.XRES-5,self.YRES-5)]
		self.perspective = Perspective()
		self.perspective.setdst((0.0,0.0),(self.XRES,0.0),(0.0,self.YRES),(self.XRES,self.YRES))
		self.cursor = Cursor(self)
		self.Options = Options()
		
	def printOut(self,strList,newLine = True):
		for all in strList:
			print all,
		#if newLine: print ""
		print ""
	
	def GetBattery(self):
		try:
			bat = self.wiimote.WiimoteState.Battery
		except AttributeError:
			return None
		if bat: return  bat*100/200
		else: return None
		
	def Connect(self):
		self.wiimote = Wiimote()
		self.OnConnecting()
		if sys.platform != 'win32': self.printOut(["Press 1 and 2 on wiimote"])
		#try:
		self.wiimote.Connect()
		#except:
		#	self.OnNoWiimoteFound()
		#	raise SystemExit
		self.OnConnected()
		self.wiimote.SetLEDs(True,False,False,False)
		self.wiimote.SetReportType(self.wiimote.InputReport.IRAccel,True)
	
	def IsConnected(self):
		try:
			cn = self.wiimote.running
		except:
			return False
		return cn
		
		
	def IsRunning(self):
		return self.running
	
	def OnRun(self): #to be overloaded
		pass
		
	def OnConnected(self): #to be overloaded
		self.printOut(["Battery: ", self.GetBattery(), "%"])
	
	def OnConnecting(self): #to be overloaded
		pass
	
	def OnDisconnect(self): #to be overloaded
		pass
	
	def OnStop(self): #to be overloaded
		pass
		
	def OnCalibrated(self): #to be overloaded
		pass
	
	def OnCalibrating(self):
		pass
		
	def OnNoWiimoteFound(self):
		print "No Wiimote or no Bluetooth found..."
		pass
		
	def OnOptionsChanged(self):
		pass
		
	def Run(self):
		
		self.running = True
		
		keep_light = self.KEEPLIGHTTIME
		x,y = (0,0)
		first_x, first_y = (0,0)
		light_state = False
		timer = 0
		
		self.OnRun()
		
		while self.running:
			if self.wiimote.WiimoteState.ButtonState.A:
				self.Stop()
				continue

			time.sleep(0.030)
			
			if self.wiimote.WiimoteState.IRState.Found1: 
				if not light_state: #if the light appears
					light_state = True
					first_x,first_y = map(int,self.perspective.warp(self.wiimote.WiimoteState.IRState.RawX1, self.wiimote.WiimoteState.IRState.RawY1))
					x,y = first_x, first_y
					timer = 0 #reset timer
					self.cursor.update(light_state, True, (first_x,first_y), (x,y), timer)
					
				else: #if the light is still lit
					x,y = map(int,self.perspective.warp(self.wiimote.WiimoteState.IRState.RawX1, self.wiimote.WiimoteState.IRState.RawY1))
					timer += 1
					keep_light = self.KEEPLIGHTTIME #keep it high while light is seen.
					self.cursor.update(light_state, False, (first_x,first_y), (x,y), timer)
			
			else:
				if keep_light and timer > self.KEEPLIGHTTIME: keep_light -= 1 #keep_light will not prevent cut-off if the light goes out quick
				else:
					if light_state:
						light_state = False
						self.cursor.update(light_state, True, (first_x,first_y), (x,y), timer)
						timer = self.KEEPLIGHTTIME - keep_light #this is the true delay since the light has gone off, not 0
					else:
						timer += 1
						self.cursor.update(light_state, False, (first_x,first_y), (x,y), timer)
						
					
			
	
	def Stop(self):
		self.running = False
		self.OnStop()
		
	def Disconnect(self):
		if self.IsRunning: self.Stop()
		self.wiimote.Disconnect()
		self.OnDisconnect()
		if self.calibrating: raise SystemExit
		del self.wiimote
		
	def Calibrate(self):
		self.Stop()
		self.calibrating = True
		self.OnCalibrating()
		self.printOut(["Point to the top left corner of the screen... "],False)
		self.ABCD[0] = self.WaitForLight()
		self.printOut(["OK"])
		
		self.WaitNoLight()
		self.printOut(["Point to the top right corner of the screen... "],False)
		self.ABCD[1] = self.WaitForLight()
		self.printOut([ "OK"])
		
		self.WaitNoLight()
		self.printOut([ "Point to the bottom left corner of the screen... "],False)
		self.ABCD[2] = self.WaitForLight()
		self.printOut([ "OK"])
		
		self.WaitNoLight()
		self.printOut([ "Point to the bottom right corner of the screen... "],False)
		self.ABCD[3] = self.WaitForLight()
		self.printOut([ "OK"])
	
		self.WaitNoLight()
		
		x0,y0 = self.ABCD[0]
		x1,y1 = self.ABCD[1]
		x2,y2 = self.ABCD[2]
		x3,y3 = self.ABCD[3]
		self.perspective.setsrc((x0,y0),(x1,y1),(x2,y2),(x3,y3))
		
		self.calibrating = False
		self.printOut([ "Calibration complete!"])
		self.OnCalibrated()
		
	def WaitForLight(self):
		dot = False
		while not dot:
			checkdot = self.wiimote.WiimoteState.IRState.RawX1, self.wiimote.WiimoteState.IRState.RawY1
			if self.wiimote.WiimoteState.IRState.Found1:
				dot = checkdot
				return dot
			time.sleep(0.030)
			
	def WaitNoLight(self):
		time.sleep(0.5)
		while 1:
			time.sleep(0.030)
			if self.wiimote.WiimoteState.IRState.Found1 == False:
				return
Ejemplo n.º 2
0
class WhiteBoard:
    running = False
    mouseclicked = False
    calibrating = False

    KEEPLIGHTTIME = 3  #will not consider if we lose the light for x frames
    CLICKMAXTIME = 5  #longest that we will consider as a click for touchpad mode
    CLICKMINTIME = 3  # will not move the pointer for the first x frames after click to help a singleclic with shaky hand
    MAXTIMEBETWEENCLICKDRAG = 5  #...

    def __init__(self):
        self.mouse = MouseControl()
        self.XRES, self.YRES = self.mouse.get_screen_resolution()
        self.XRES, self.YRES = float(self.XRES), float(self.YRES)
        self.ABCD = [(0, 0), (self.XRES - 5, 0), (0, self.YRES - 5),
                     (self.XRES - 5, self.YRES - 5)]
        self.perspective = Perspective()
        self.perspective.setdst((0.0, 0.0), (self.XRES, 0.0), (0.0, self.YRES),
                                (self.XRES, self.YRES))
        self.cursor = Cursor(self)
        self.Options = Options()

    def printOut(self, strList, newLine=True):
        for all in strList:
            print all,
        #if newLine: print ""
        print ""

    def GetBattery(self):
        try:
            bat = self.wiimote.WiimoteState.Battery
        except AttributeError:
            return None
        if bat: return bat * 100 / 200
        else: return None

    def Connect(self):
        self.wiimote = Wiimote()
        self.OnConnecting()
        if sys.platform != 'win32': self.printOut(["Press 1 and 2 on wiimote"])
        #try:
        self.wiimote.Connect()
        #except:
        #   self.OnNoWiimoteFound()
        #   raise SystemExit
        self.OnConnected()
        self.wiimote.SetLEDs(True, False, False, False)
        self.wiimote.SetReportType(self.wiimote.InputReport.IRAccel, True)

    def IsConnected(self):
        try:
            cn = self.wiimote.running
        except:
            return False
        return cn

    def IsRunning(self):
        return self.running

    def OnRun(self):  #to be overloaded
        pass

    def OnConnected(self):  #to be overloaded
        self.printOut(["Battery: ", self.GetBattery(), "%"])

    def OnConnecting(self):  #to be overloaded
        pass

    def OnDisconnect(self):  #to be overloaded
        pass

    def OnStop(self):  #to be overloaded
        pass

    def OnCalibrated(self):  #to be overloaded
        pass

    def OnCalibrating(self):
        pass

    def OnNoWiimoteFound(self):
        print "No Wiimote or no Bluetooth found..."
        pass

    def OnOptionsChanged(self):
        pass

    def Run(self):

        self.running = True

        keep_light = self.KEEPLIGHTTIME
        x, y = (0, 0)
        first_x, first_y = (0, 0)
        light_state = False
        timer = 0

        self.OnRun()

        while self.running:
            if self.wiimote.WiimoteState.ButtonState.A:
                self.Stop()
                continue

            time.sleep(0.030)

            if self.wiimote.WiimoteState.IRState.Found1:
                if not light_state:  #if the light appears
                    light_state = True
                    first_x, first_y = map(
                        int,
                        self.perspective.warp(
                            self.wiimote.WiimoteState.IRState.RawX1,
                            self.wiimote.WiimoteState.IRState.RawY1))
                    x, y = first_x, first_y
                    timer = 0  #reset timer
                    self.cursor.update(light_state, True, (first_x, first_y),
                                       (x, y), timer)

                else:  #if the light is still lit
                    x, y = map(
                        int,
                        self.perspective.warp(
                            self.wiimote.WiimoteState.IRState.RawX1,
                            self.wiimote.WiimoteState.IRState.RawY1))
                    timer += 1
                    keep_light = self.KEEPLIGHTTIME  #keep it high while light is seen.
                    self.cursor.update(light_state, False, (first_x, first_y),
                                       (x, y), timer)

            else:
                if keep_light and timer > self.KEEPLIGHTTIME:
                    keep_light -= 1  #keep_light will not prevent cut-off if the light goes out quick
                else:
                    if light_state:
                        light_state = False
                        self.cursor.update(light_state, True,
                                           (first_x, first_y), (x, y), timer)
                        timer = self.KEEPLIGHTTIME - keep_light  #this is the true delay since the light has gone off, not 0
                    else:
                        timer += 1
                        self.cursor.update(light_state, False,
                                           (first_x, first_y), (x, y), timer)

    def Stop(self):
        self.running = False
        self.OnStop()

    def Disconnect(self):
        if self.IsRunning: self.Stop()
        self.wiimote.Disconnect()
        self.OnDisconnect()
        if self.calibrating: raise SystemExit
        del self.wiimote

    def Calibrate(self):
        self.Stop()
        self.calibrating = True
        self.OnCalibrating()
        self.printOut(["Point to the top left corner of the screen... "],
                      False)
        self.ABCD[0] = self.WaitForLight()
        self.printOut(["OK"])

        self.WaitNoLight()
        self.printOut(["Point to the top right corner of the screen... "],
                      False)
        self.ABCD[1] = self.WaitForLight()
        self.printOut(["OK"])

        self.WaitNoLight()
        self.printOut(["Point to the bottom left corner of the screen... "],
                      False)
        self.ABCD[2] = self.WaitForLight()
        self.printOut(["OK"])

        self.WaitNoLight()
        self.printOut(["Point to the bottom right corner of the screen... "],
                      False)
        self.ABCD[3] = self.WaitForLight()
        self.printOut(["OK"])

        self.WaitNoLight()

        x0, y0 = self.ABCD[0]
        x1, y1 = self.ABCD[1]
        x2, y2 = self.ABCD[2]
        x3, y3 = self.ABCD[3]
        self.perspective.setsrc((x0, y0), (x1, y1), (x2, y2), (x3, y3))

        self.calibrating = False
        self.printOut(["Calibration complete!"])
        self.OnCalibrated()

    def WaitForLight(self):
        dot = False
        while not dot:
            checkdot = self.wiimote.WiimoteState.IRState.RawX1, self.wiimote.WiimoteState.IRState.RawY1
            if self.wiimote.WiimoteState.IRState.Found1:
                dot = checkdot
                return dot
            time.sleep(0.030)

    def WaitNoLight(self):
        time.sleep(0.5)
        while 1:
            time.sleep(0.030)
            if self.wiimote.WiimoteState.IRState.Found1 == False:
                return