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@287: etisserant@287: ## Configuration 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@287: #print "wiimote callback" etisserant@287: for msg in messages: etisserant@287: if msg[0] == cwiid.MESG_IR: etisserant@287: # msg is of the form (cwiid.MESG_IR, (((x, y), size) or None * 4)) etisserant@287: for p in msg[1]: etisserant@287: if p: etisserant@287: pos = p['pos'][0], p['pos'][1] # point is mirrored etisserant@287: #s = max(p['size'], 1) etisserant@287: etisserant@287: last_point = tuple(pos) etisserant@287: #print "last_point",last_point etisserant@287: elif 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@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@287: try: etisserant@287: #if False: etisserant@287: wm = cwiid.Wiimote(wiimote_hwaddr) etisserant@287: if wm is not None: etisserant@287: # each message will contain info about ir and buttons etisserant@287: wm.rpt_mode = cwiid.RPT_IR | cwiid.RPT_BTN # | cwiid.RPT_STATUS etisserant@287: # tell cwiid to use the callback interface and allways send button events etisserant@287: wm.enable(cwiid.FLAG_MESG_IFC etisserant@287: #| cwiid.FLAG_NONBLOCK etisserant@287: | cwiid.FLAG_REPEAT_BTN) etisserant@287: etisserant@287: # specify wich function will manage messages AFTER the other settings etisserant@287: wm.mesg_callback = cback etisserant@287: etisserant@287: # quick check on the wiimote etisserant@287: print "Got Wiimote!" etisserant@287: st = wm.state etisserant@287: for e in st: etisserant@287: print str(e).ljust(8), ">", st[e] etisserant@287: except: etisserant@287: #else: etisserant@287: print "Error with wiimote " + str(wiimote_hwaddr) etisserant@287: etisserant@287: def _runtime_cleanup(): etisserant@287: print "_runtime_cleanup() Called" etisserant@287: runing = 0 etisserant@287: if wm is not None: etisserant@287: wm.close()