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