def __init__(self,wiiid,index): self.conf = ConfSaver("." + self.__class__.__name__) self.wii = None self.mode = None self.accbase = None self.lastbutton = 0 self.id = wiiid self.index = index self.init_values() self.antbuttons = 0
class Wiimote(): def __init__(self,wiiid,index): self.conf = ConfSaver("." + self.__class__.__name__) self.wii = None self.mode = None self.accbase = None self.lastbutton = 0 self.id = wiiid self.index = index self.init_values() self.antbuttons = 0 def init_values(self): self.mode = self.conf.init_set("mode", cwiid.RPT_BTN |cwiid.RPT_ACC) def init_log(self,parent): self.label = parent + '.' + "WiiCommand_"+ str(self.index) logclass = getLogClass(label = self.label)() self.log = logclass.log def __normalizedata__(self,data): index = 0 nd = [0] *len(data) for adata in data: nd[index] = 1.0*(adata - self.accbase[0][index])/(self.accbase[1][index]-self.accbase[0][index]) index +=1 return nd def get_calibration(self): return self.wii.get_acc_cal(cwiid.EXT_NONE) def set_mode(self): self.__set_rptmode__() self.__set_rcvmode__() def connect(self): #log.info("Connecting to: " + str(self.id)) self.wii = cwiid.Wiimote(self.id) self.wii.led = cwiid.LED1_ON | cwiid.LED3_ON #self.wii.enable(cwiid.FLAG_MOTIONPLUS) #self.wii.enable(cwiid.) self.set_mode() self.accbase = self.wii.get_acc_cal(cwiid.EXT_NONE) self.init_time = time.time() def __set_rcvmode__(self): """Set the way Wii will send data""" #log.info("Setting Receive Mode: " + str(mode)) self.wii.disable(cwiid.FLAG_MESG_IFC) self.wii.enable(cwiid.FLAG_NONBLOCK|cwiid.FLAG_CONTINUOUS) self.wii.enable(cwiid.FLAG_MOTIONPLUS) #FLAG_NONBLOCK #======================================================================= # if rcv_mode == 1: # self.wii.enable(cwiid.FLAG_CONTINUOUS) #======================================================================= def __set_rptmode__(self): """Set """ self.log.info("Setting Report Mode: " + str(self.mode)) #self.wii.rpt_mode = cwiid.RPT_BTN |cwiid.RPT_ACC self.wii.rpt_mode = self.mode def get_rptmode(self): ret = 0 if self.mode & cwiid.RPT_IR: ret |=commons.IR if self.mode & cwiid.RPT_ACC: ret |=commons.ACC if self.mode & cwiid.RPT_MOTIONPLUS: ret |=commons.MOVE return ret def add_rptmode(self,mode): if mode == commons.ACC: mode = cwiid.RPT_ACC if mode == commons.IR: mode = cwiid.RPT_IR if mode == commons.MOVE: mode = cwiid.RPT_MOTIONPLUS self.log.info("Adding Report Mode: " + str(mode)) self.mode |= mode self.conf.setval('mode',self.mode) self.wii.rpt_mode = self.mode def del_rptmode(self,mode): self.log.info("Deleting Report Mode: " + str(mode)) if mode == commons.ACC: mode = cwiid.RPT_ACC if mode == commons.IR: mode = cwiid.RPT_IR if mode == commons.MOVE: mode = cwiid.RPT_MOTIONPLUS self.mode ^= mode self.conf.setval('mode',self.mode) self.wii.rpt_mode = self.mode # cwiid.RPT_ACC | #cwiid.RPT_MOTIONPLUS | #Here we should return a stable value: #(Data_type,(Button Data,time)) #(Data_type,(Acc_Data,time)) def get_value(self): """Get a Value from wiimote""" retdata = {} self.dataant ={} self.data = self.wii.state #self.log.debug("Retreiving Data from Wiimote: " + str(self.id)) #log.info("Info: " + str(self.data)) utime = time.time() - self.init_time retdata['time'] = utime try: if self.mode & cwiid.RPT_BTN: if self.antbuttons != self.data['buttons']: retdata['buttons'] = self.data['buttons'] self.antbuttons = self.data['buttons'] else: retdata['buttons'] = 0 if self.mode & cwiid.RPT_ACC: retdata['acc'] = {} aux = self.__normalizedata__(self.data['acc']) retdata['acc']['AccX'] = aux[0] retdata['acc']['AccY'] = aux[1] retdata['acc']['AccZ'] = aux[2] if self.mode & cwiid.RPT_MOTIONPLUS: retdata['vel'] = {} aux = self.__get_motion_data__() retdata['vel']['Pitch'] = aux[0] retdata['vel']['Roll'] = aux[1] retdata['vel']['Yaw'] = aux[2] #retdata['motion'] = self.get_motion_data() if self.mode & cwiid.RPT_IR: #To return in a dictionary form retdata['IR'] = {} ir = self.data['ir_src'] if ir[0]: retdata['IR']['Pos1X'] = ir[0][0] retdata['IR']['Pos1Y'] = ir[0][1] if ir[1]: retdata['IR']['Pos2X'] = ir[1][0] retdata['IR']['Pos2Y'] = ir[1][1] if ir[2]: retdata['IR']['Pos3X'] = ir[2][0] retdata['IR']['Pos3Y'] = ir[2][1] if ir[3]: retdata['IR']['Pos4X'] = ir[3][0] retdata['IR']['Pos4Y'] = ir[3][1] self.dataant = self.data except KeyError: pass #data = self.wii.get_mesg() #if data['buttons'] or self.lastbutton: #======================================================================= # normalized_acc = self.normalizedata(self.data['acc']) # if self.data['buttons'] != self.lastbutton: # self.lastbutton = self.data['buttons'] # motiondata = self.get_motion_data() # #retdata = (WiiMoteConn.WII_BTN,(self.data['buttons'],self.data['acc'],motiondata,time.time())) # retdata = (self.data['buttons'],normalized_acc,motiondata,utime) # else: # motiondata = self.get_motion_data() # #retdata =(WiiMoteConn.WII_PLUS,(0x0,self.data['acc'],motiondata,time.time())) # retdata =(0x0,normalized_acc,motiondata,utime) # return retdata #======================================================================= #print retdata return retdata def __get_motion_data__(self): try: motiondata = self.data['motionplus']['angle_rate'] except: motiondata = (0,0,0) finally: return motiondata def disconnect(self): self.wii.close() def __del__(self): del(self.conf)