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