Edouard@1157: <?xml version="1.0" encoding="UTF-8" standalone="no"?> Edouard@1157: <PyFile> Edouard@1157: <variables> Edouard@1157: <variable name="WiiNunchuckStickX" type="INT"/> Edouard@1157: <variable name="WiiNunchuckStickY" type="INT"/> Edouard@1185: <variable name="WiiNunchuckAccX" type="INT"/> Edouard@1185: <variable name="WiiNunchuckAccY" type="INT"/> Edouard@1185: <variable name="WiiNunchuckAccZ" type="INT"/> Edouard@1185: <variable name="WiiNunchuckButtons" type="WORD"/> Edouard@1185: <variable name="WiiButtons" type="WORD"/> Edouard@1157: </variables> Edouard@1157: <globals> Edouard@1157: <![CDATA[ Edouard@1157: import cwiid,commands,sys,re,os,time Edouard@1157: Edouard@1157: wiimote = None Edouard@1157: WIIMOTE_ADDR_MODEL = re.compile("((?:[0-9A-F]{2})(?::[0-9A-F]{2}){5})\s*Nintendo") Edouard@1157: nunchuckzero = None Edouard@1157: Edouard@1157: def Wiimote_cback(messages, time): Edouard@1157: global nunchuckzero Edouard@1157: state = dict(messages) Edouard@1157: bts = state.get(cwiid.MESG_BTN, None) Edouard@1157: if bts is not None: Edouard@1157: PLCGlobals.WiiButtons = bts Edouard@1157: nunchuck = state.get(cwiid.MESG_NUNCHUK, None) Edouard@1157: if nunchuck is not None: Edouard@1157: PLCGlobals.WiiNunchuckButtons = nunchuck['buttons'] Edouard@1157: X,Y = nunchuck['stick'] Edouard@1185: PLCGlobals.WiiNunchuckAccX = nunchuck['acc'][cwiid.X] Edouard@1185: PLCGlobals.WiiNunchuckAccY = nunchuck['acc'][cwiid.Y] Edouard@1185: PLCGlobals.WiiNunchuckAccZ = nunchuck['acc'][cwiid.Z] Edouard@1157: if nunchuckzero is None: Edouard@1157: nunchuckzero = X,Y Edouard@1157: (PLCGlobals.WiiNunchuckStickX, Edouard@1157: PLCGlobals.WiiNunchuckStickY) = X-nunchuckzero[0],Y-nunchuckzero[1] Edouard@1157: Edouard@1157: def Connect_Wiimote(connected_callback): Edouard@1157: global wiimote,nunchuckzero Edouard@1157: mac_addr = '' Edouard@1157: try: Edouard@1157: mac_addr = file("wiimac.txt","rt").read() Edouard@1157: except: Edouard@1157: PLCObject.LogMessage("Wiimote MAC unknown, scanning bluetooth") Edouard@1157: output = commands.getoutput("hcitool scan") Edouard@1157: result = WIIMOTE_ADDR_MODEL.search(output) Edouard@1157: if result is not None: Edouard@1157: mac_addr = result.group(1) Edouard@1157: PLCObject.LogMessage("Found Wiimote with MAC %s"%mac_addr) Edouard@1157: file("wiimac.txt","wt").write(mac_addr) Edouard@1157: Edouard@1157: # Connect to wiimote Edouard@1157: if not mac_addr: Edouard@1157: PLCObject.LogMessage("Connection to unknown Wiimote...") Edouard@1157: wiimote = cwiid.Wiimote() Edouard@1157: else: Edouard@1157: PLCObject.LogMessage("Connection to Wiimote %s..."%mac_addr) Edouard@1157: wiimote = cwiid.Wiimote(mac_addr) Edouard@1157: Edouard@1157: if wiimote is not None: Edouard@1157: nunchuckzero = None Edouard@1157: wiimote.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_EXT Edouard@1157: # use the callback interface Edouard@1157: wiimote.mesg_callback = Wiimote_cback Edouard@1157: wiimote.enable(cwiid.FLAG_MESG_IFC) Edouard@1157: connected_callback(mac_addr) Edouard@1157: PLCObject.LogMessage("Wiimote %s Connected"%mac_addr) Edouard@1157: else: Edouard@1157: PLCObject.LogMessage("Wiimote %s not found"%mac_addr) Edouard@1157: os.remove("wiimac.txt") Edouard@1157: connected_callback(None) Edouard@1157: Edouard@1157: def Disconnect_Wiimote(): Edouard@1157: global wiimote Edouard@1157: if wiimote is not None: Edouard@1157: wiimote.disable(cwiid.FLAG_MESG_IFC) Edouard@1157: time.sleep(0.1) Edouard@1157: wiimote.close() Edouard@1157: wiimote = None Edouard@1157: PLCObject.LogMessage("Wiimote disconnected") Edouard@1157: Edouard@1157: ]]> Edouard@1157: </globals> Edouard@1157: <init> Edouard@1157: <![CDATA[ Edouard@1157: ]]> Edouard@1157: </init> Edouard@1157: <cleanup> Edouard@1157: <![CDATA[ Edouard@1157: Disconnect_Wiimote() Edouard@1157: Edouard@1157: ]]> Edouard@1157: </cleanup> Edouard@1157: <start> Edouard@1157: <![CDATA[ Edouard@1157: ]]> Edouard@1157: </start> Edouard@1157: <stop> Edouard@1157: <![CDATA[ Edouard@1157: ]]> Edouard@1157: </stop> Edouard@1157: </PyFile>