tests/wiimote/py_ext_0@py_ext/pyfile.xml
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Tue, 02 Oct 2018 16:53:14 +0300
changeset 2416 1ca207782dde
parent 1185 b36755d7c19e
permissions -rw-r--r--
Use predefined constants for PlcStatus instead of string literals
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"/>
1185
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
     6
    <variable name="WiiNunchuckAccX" type="INT"/>
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
     7
    <variable name="WiiNunchuckAccY" type="INT"/>
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
     8
    <variable name="WiiNunchuckAccZ" type="INT"/>
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
     9
    <variable name="WiiNunchuckButtons" type="WORD"/>
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
    10
    <variable name="WiiButtons" type="WORD"/>
1157
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    11
  </variables>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    12
  <globals>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    13
<![CDATA[
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    14
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
    15
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    16
wiimote = None
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    17
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
    18
nunchuckzero = None
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    19
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    20
def Wiimote_cback(messages, time):
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    21
    global nunchuckzero
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    22
    state = dict(messages)
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    23
    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
    24
    if bts is not None:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    25
        PLCGlobals.WiiButtons = bts
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    26
    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
    27
    if nunchuck is not None:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    28
        PLCGlobals.WiiNunchuckButtons = nunchuck['buttons']
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    29
        X,Y = nunchuck['stick']
1185
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
    30
        PLCGlobals.WiiNunchuckAccX = nunchuck['acc'][cwiid.X]
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
    31
        PLCGlobals.WiiNunchuckAccY = nunchuck['acc'][cwiid.Y]
b36755d7c19e Updated wiimote example. Now include nunchuck accel, and buttons are on 16bits
Edouard Tisserant
parents: 1157
diff changeset
    32
        PLCGlobals.WiiNunchuckAccZ = nunchuck['acc'][cwiid.Z]
1157
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    33
        if nunchuckzero is None:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    34
            nunchuckzero = X,Y
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    35
        (PLCGlobals.WiiNunchuckStickX,
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    36
         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
    37
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    38
def Connect_Wiimote(connected_callback):
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    39
    global wiimote,nunchuckzero
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    40
    mac_addr = ''
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    41
    try:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    42
        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
    43
    except:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    44
        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
    45
        output = commands.getoutput("hcitool scan")
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    46
        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
    47
        if result is not None:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    48
            mac_addr = result.group(1)
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    49
            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
    50
            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
    51
 
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    52
    # Connect to wiimote
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    53
    if not mac_addr:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    54
        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
    55
        wiimote = cwiid.Wiimote()
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    56
    else:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    57
        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
    58
        wiimote = cwiid.Wiimote(mac_addr)
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    59
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    60
    if wiimote is not None:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    61
        nunchuckzero = None
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    62
        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
    63
        # use the callback interface
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    64
        wiimote.mesg_callback = Wiimote_cback
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    65
        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
    66
        connected_callback(mac_addr)
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    67
        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
    68
    else:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    69
        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
    70
        os.remove("wiimac.txt")
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    71
        connected_callback(None)
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    72
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    73
def Disconnect_Wiimote():
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    74
  global wiimote
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    75
  if wiimote is not None:
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    76
    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
    77
    time.sleep(0.1)
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    78
    wiimote.close()
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    79
    wiimote = None
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    80
    PLCObject.LogMessage("Wiimote disconnected")
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    81
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    82
]]>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    83
  </globals>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    84
  <init>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    85
<![CDATA[
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
  </init>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    88
  <cleanup>
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
Disconnect_Wiimote()
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    91
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    92
]]>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    93
  </cleanup>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    94
  <start>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    95
<![CDATA[
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    96
]]>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    97
  </start>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    98
  <stop>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
    99
<![CDATA[
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
   100
]]>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
   101
  </stop>
72d14a74c643 Added wiimote input example using python-cwiid and python PLC global variable access
Edouard Tisserant
parents:
diff changeset
   102
</PyFile>