etisserant@287: #!/usr/bin/env python etisserant@287: ################################################################################ etisserant@287: # # etisserant@287: # This program is free software: you can redistribute it and/or modify # etisserant@287: # it under the terms of the GNU General Public License as published by # etisserant@287: # the Free Software Foundation, either version 3 of the License, or # etisserant@287: # (at your option) any later version. # etisserant@287: # # etisserant@287: # This program is distributed in the hope that it will be useful, # etisserant@287: # but WITHOUT ANY WARRANTY; without even the implied warranty of # etisserant@287: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # etisserant@287: # GNU General Public License for more details. # etisserant@287: # # etisserant@287: # You should have received a copy of the GNU General Public License # etisserant@287: # along with this program. If not, see . # etisserant@287: # # etisserant@287: ################################################################################ etisserant@287: etisserant@287: import cwiid etisserant@301: import sys etisserant@301: from threading import Thread etisserant@287: ## Configuration etisserant@301: wm = None etisserant@287: wiimote_hwaddr = '' # Use your address to speed up the connection proccess etisserant@287: #wiimote_hwaddr = '00:19:1D:5D:5D:DC' etisserant@287: etisserant@287: last_point = (0,0) etisserant@287: btA = 0 etisserant@287: btB = 0 etisserant@287: etisserant@287: def cback(messages): etisserant@287: '''Wiimote callback managing method etisserant@287: Recieves a message list, each element is different, see the libcwiid docs''' etisserant@287: global btA, btB, last_point etisserant@301: #print messages etisserant@287: #print "wiimote callback" etisserant@287: for msg in messages: etisserant@301: # if msg[0] == cwiid.MESG_IR: etisserant@301: # # msg is of the form (cwiid.MESG_IR, (((x, y), size) or None * 4)) etisserant@301: # for p in msg[1]: etisserant@301: # if p: etisserant@301: # pos = p['pos'][0], p['pos'][1] # point is mirrored etisserant@301: # #s = max(p['size'], 1) etisserant@301: # etisserant@301: # last_point = tuple(pos) etisserant@301: # #print "last_point",last_point etisserant@301: if msg[0] == cwiid.MESG_BTN: etisserant@287: # msg is of the form (cwiid.MESG_BTN, cwiid.BTN_*) etisserant@287: if msg[1] & cwiid.BTN_A: etisserant@287: btA = 1 etisserant@287: #print "btA = 1" etisserant@287: else: etisserant@287: btA = 0 etisserant@287: #print "btA = 0" etisserant@287: etisserant@287: if msg[1] & cwiid.BTN_B: etisserant@287: btB = 1 etisserant@287: #print "btB = 1" etisserant@287: else: etisserant@287: btB = 0 etisserant@301: elif msg[0] == cwiid.MESG_NUNCHUK: etisserant@301: #sbb = msg[1]['buttons'] etisserant@301: last_point = msg[1]['stick'] etisserant@301: #ssx = msg[1]['stick'][0] etisserant@301: #ssy = msg[1]['stick'][1] etisserant@301: #msg[1]['acc'][0] etisserant@301: #msg[1]['acc'][1] etisserant@301: #msg[1]['acc'][2] etisserant@287: #print "btB = 0" etisserant@287: #elif msg[0] == cwiid.MESG_STATUS: etisserant@287: # # msg is of the form (cwiid.MESG_BTN, { 'status' : value, ... }) etisserant@287: # print msg[1] etisserant@287: etisserant@301: def Connect_Wiimote(frameobj): etisserant@301: global wm,wiimote_hwaddr etisserant@301: try: etisserant@301: #if False: etisserant@301: print "Press 1+2 Now !!!!" etisserant@301: sys.stdout.flush() etisserant@301: wm = cwiid.Wiimote(wiimote_hwaddr) etisserant@301: if wm is not None: etisserant@301: # each message will contain info about ir and buttons etisserant@301: wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_EXT # | cwiid.RPT_STATUS | cwiid.RPT_IR | etisserant@301: # tell cwiid to use the callback interface and allways send button events etisserant@301: wm.enable(cwiid.FLAG_MESG_IFC) etisserant@301: #| cwiid.FLAG_NONBLOCK etisserant@301: #| cwiid.FLAG_REPEAT_BTN) etisserant@287: etisserant@301: # specify wich function will manage messages AFTER the other settings etisserant@301: wm.mesg_callback = cback etisserant@287: etisserant@301: # quick check on the wiimote etisserant@301: print "Got Wiimote!" etisserant@301: frameobj.label_1.SetLabel("Got Wiimote !") etisserant@301: st = wm.state etisserant@301: for e in st: etisserant@301: print str(e).ljust(8), ">", st[e] etisserant@301: except: etisserant@301: #else: etisserant@301: print "Error with wiimote " + str(wiimote_hwaddr) etisserant@301: frameobj.label_1.SetLabel("Wiimote NOK") etisserant@301: sys.stdout.flush() etisserant@301: etisserant@287: def _runtime_cleanup(): etisserant@287: if wm is not None: etisserant@287: wm.close() etisserant@301: etisserant@301: def start_manu(self,evt): etisserant@301: self.label_1.SetLabel("press 1+2 now !!!") etisserant@301: wx.CallAfter(Connect_Wiimote, self) etisserant@301: evt.Skip() etisserant@301: HMIFrame.start_manu = start_manu etisserant@301: etisserant@301: def _runtime_begin(): etisserant@301: pass etisserant@301: #wx.CallAfter(Connect_Wiimote) etisserant@301: #Thread(target=Connect_Wiimote).start() etisserant@301: etisserant@301: #create_frame() etisserant@301: #wx.Yield()