tests/wiimote/py_ext_0@py_ext/pyfile.xml
changeset 1157 72d14a74c643
child 1185 b36755d7c19e
equal deleted inserted replaced
1156:9708ed2a4ac2 1157:72d14a74c643
       
     1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
       
     2 <PyFile>
       
     3   <variables>
       
     4     <variable name="WiiNunchuckStickX" type="INT"/>
       
     5     <variable name="WiiNunchuckStickY" type="INT"/>
       
     6     <variable name="WiiNunchuckButtons" type="BYTE"/>
       
     7     <variable name="WiiButtons" type="BYTE"/>
       
     8   </variables>
       
     9   <globals>
       
    10 <![CDATA[
       
    11 import cwiid,commands,sys,re,os,time
       
    12 
       
    13 wiimote = None
       
    14 WIIMOTE_ADDR_MODEL = re.compile("((?:[0-9A-F]{2})(?::[0-9A-F]{2}){5})\s*Nintendo")
       
    15 nunchuckzero = None
       
    16 
       
    17 def Wiimote_cback(messages, time):
       
    18     global nunchuckzero
       
    19     state = dict(messages)
       
    20     bts = state.get(cwiid.MESG_BTN, None)
       
    21     if bts is not None:
       
    22         PLCGlobals.WiiButtons = bts
       
    23     nunchuck = state.get(cwiid.MESG_NUNCHUK, None)
       
    24     if nunchuck is not None:
       
    25         PLCGlobals.WiiNunchuckButtons = nunchuck['buttons']
       
    26         X,Y = nunchuck['stick']
       
    27         if nunchuckzero is None:
       
    28             nunchuckzero = X,Y
       
    29         (PLCGlobals.WiiNunchuckStickX,
       
    30          PLCGlobals.WiiNunchuckStickY) = X-nunchuckzero[0],Y-nunchuckzero[1]
       
    31 
       
    32 def Connect_Wiimote(connected_callback):
       
    33     global wiimote,nunchuckzero
       
    34     mac_addr = ''
       
    35     try:
       
    36         mac_addr = file("wiimac.txt","rt").read()
       
    37     except:
       
    38         PLCObject.LogMessage("Wiimote MAC unknown, scanning bluetooth")
       
    39         output = commands.getoutput("hcitool scan")
       
    40         result = WIIMOTE_ADDR_MODEL.search(output)
       
    41         if result is not None:
       
    42             mac_addr = result.group(1)
       
    43             PLCObject.LogMessage("Found Wiimote with MAC %s"%mac_addr)
       
    44             file("wiimac.txt","wt").write(mac_addr)
       
    45  
       
    46     # Connect to wiimote
       
    47     if not mac_addr:
       
    48         PLCObject.LogMessage("Connection to unknown Wiimote...")
       
    49         wiimote = cwiid.Wiimote()
       
    50     else:
       
    51         PLCObject.LogMessage("Connection to Wiimote %s..."%mac_addr)
       
    52         wiimote = cwiid.Wiimote(mac_addr)
       
    53 
       
    54     if wiimote is not None:
       
    55         nunchuckzero = None
       
    56         wiimote.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_EXT
       
    57         # use the callback interface
       
    58         wiimote.mesg_callback = Wiimote_cback
       
    59         wiimote.enable(cwiid.FLAG_MESG_IFC)
       
    60         connected_callback(mac_addr)
       
    61         PLCObject.LogMessage("Wiimote %s Connected"%mac_addr)
       
    62     else:
       
    63         PLCObject.LogMessage("Wiimote %s not found"%mac_addr)
       
    64         os.remove("wiimac.txt")
       
    65         connected_callback(None)
       
    66 
       
    67 def Disconnect_Wiimote():
       
    68   global wiimote
       
    69   if wiimote is not None:
       
    70     wiimote.disable(cwiid.FLAG_MESG_IFC)
       
    71     time.sleep(0.1)
       
    72     wiimote.close()
       
    73     wiimote = None
       
    74     PLCObject.LogMessage("Wiimote disconnected")
       
    75 
       
    76 ]]>
       
    77   </globals>
       
    78   <init>
       
    79 <![CDATA[
       
    80 ]]>
       
    81   </init>
       
    82   <cleanup>
       
    83 <![CDATA[
       
    84 Disconnect_Wiimote()
       
    85 
       
    86 ]]>
       
    87   </cleanup>
       
    88   <start>
       
    89 <![CDATA[
       
    90 ]]>
       
    91   </start>
       
    92   <stop>
       
    93 <![CDATA[
       
    94 ]]>
       
    95   </stop>
       
    96 </PyFile>