Added wiimote input example using python-cwiid and python PLC global variable access
authorEdouard Tisserant
Thu, 16 May 2013 17:40:32 +0900
changeset 1157 72d14a74c643
parent 1156 9708ed2a4ac2
child 1158 193e6cd9010f
Added wiimote input example using python-cwiid and python PLC global variable access
tests/wiimote/beremiz.xml
tests/wiimote/plc.xml
tests/wiimote/py_ext_0@py_ext/baseconfnode.xml
tests/wiimote/py_ext_0@py_ext/pyfile.xml
tests/wiimote/wxglade_hmi@wxglade_hmi/baseconfnode.xml
tests/wiimote/wxglade_hmi@wxglade_hmi/hmi.wxg
tests/wiimote/wxglade_hmi@wxglade_hmi/pyfile.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/beremiz.xml	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<BeremizRoot URI_location="LOCAL://">
+  <TargetType/>
+</BeremizRoot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/plc.xml	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,167 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://www.plcopen.org/xml/tc6.xsd"
+         xsi:schemaLocation="http://www.plcopen.org/xml/tc6.xsd"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns:xhtml="http://www.w3.org/1999/xhtml">
+  <fileHeader companyName="Unknown"
+              productName="Unnamed"
+              productVersion="1"
+              creationDateTime="2012-09-12T23:30:19"/>
+  <contentHeader name="Unnamed"
+                 modificationDateTime="2013-05-16T17:17:49">
+    <coordinateInfo>
+      <fbd>
+        <scaling x="5" y="5"/>
+      </fbd>
+      <ld>
+        <scaling x="0" y="0"/>
+      </ld>
+      <sfc>
+        <scaling x="0" y="0"/>
+      </sfc>
+    </coordinateInfo>
+  </contentHeader>
+  <types>
+    <dataTypes/>
+    <pous>
+      <pou name="main" pouType="program">
+        <interface>
+          <localVars>
+            <variable name="x">
+              <type>
+                <INT/>
+              </type>
+            </variable>
+            <variable name="y">
+              <type>
+                <INT/>
+              </type>
+            </variable>
+            <variable name="b1">
+              <type>
+                <BYTE/>
+              </type>
+            </variable>
+            <variable name="b0">
+              <type>
+                <BYTE/>
+              </type>
+            </variable>
+          </localVars>
+          <externalVars>
+            <variable name="WiiNunchuckStickX">
+              <type>
+                <INT/>
+              </type>
+            </variable>
+            <variable name="WiiNunchuckStickY">
+              <type>
+                <INT/>
+              </type>
+            </variable>
+            <variable name="WiiNunchuckButtons">
+              <type>
+                <BYTE/>
+              </type>
+            </variable>
+            <variable name="WiiButtons">
+              <type>
+                <BYTE/>
+              </type>
+            </variable>
+          </externalVars>
+        </interface>
+        <body>
+          <FBD>
+            <outVariable localId="1" height="30" width="20">
+              <position x="345" y="40"/>
+              <connectionPointIn>
+                <relPosition x="0" y="15"/>
+                <connection refLocalId="3">
+                  <position x="345" y="55"/>
+                  <position x="295" y="55"/>
+                </connection>
+              </connectionPointIn>
+              <expression>x</expression>
+            </outVariable>
+            <inVariable localId="3" height="30" width="150">
+              <position x="145" y="40"/>
+              <connectionPointOut>
+                <relPosition x="150" y="15"/>
+              </connectionPointOut>
+              <expression>WiiNunchuckStickX</expression>
+            </inVariable>
+            <inVariable localId="4" height="30" width="150">
+              <position x="145" y="100"/>
+              <connectionPointOut>
+                <relPosition x="150" y="15"/>
+              </connectionPointOut>
+              <expression>WiiNunchuckStickY</expression>
+            </inVariable>
+            <inVariable localId="5" height="30" width="155">
+              <position x="115" y="175"/>
+              <connectionPointOut>
+                <relPosition x="155" y="15"/>
+              </connectionPointOut>
+              <expression>WiiNunchuckButtons</expression>
+            </inVariable>
+            <inVariable localId="6" height="30" width="90">
+              <position x="180" y="225"/>
+              <connectionPointOut>
+                <relPosition x="90" y="15"/>
+              </connectionPointOut>
+              <expression>WiiButtons</expression>
+            </inVariable>
+            <outVariable localId="8" height="30" width="20">
+              <position x="345" y="100"/>
+              <connectionPointIn>
+                <relPosition x="0" y="15"/>
+                <connection refLocalId="4">
+                  <position x="345" y="115"/>
+                  <position x="295" y="115"/>
+                </connection>
+              </connectionPointIn>
+              <expression>y</expression>
+            </outVariable>
+            <outVariable localId="9" height="30" width="30">
+              <position x="345" y="175"/>
+              <connectionPointIn>
+                <relPosition x="0" y="15"/>
+                <connection refLocalId="5">
+                  <position x="345" y="190"/>
+                  <position x="270" y="190"/>
+                </connection>
+              </connectionPointIn>
+              <expression>b1</expression>
+            </outVariable>
+            <outVariable localId="10" height="30" width="30">
+              <position x="345" y="225"/>
+              <connectionPointIn>
+                <relPosition x="0" y="15"/>
+                <connection refLocalId="6">
+                  <position x="345" y="240"/>
+                  <position x="270" y="240"/>
+                </connection>
+              </connectionPointIn>
+              <expression>b0</expression>
+            </outVariable>
+          </FBD>
+        </body>
+        <documentation>
+<![CDATA[]]>
+        </documentation>
+      </pou>
+    </pous>
+  </types>
+  <instances>
+    <configurations>
+      <configuration name="config">
+        <resource name="resource1">
+          <task name="tsk1" interval="T#1ms" priority="0">
+            <pouInstance name="inst1" typeName="main"/>
+          </task>
+        </resource>
+      </configuration>
+    </configurations>
+  </instances>
+</project>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/py_ext_0@py_ext/baseconfnode.xml	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<BaseParams Name="py_ext_0" IEC_Channel="1"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/py_ext_0@py_ext/pyfile.xml	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<PyFile>
+  <variables>
+    <variable name="WiiNunchuckStickX" type="INT"/>
+    <variable name="WiiNunchuckStickY" type="INT"/>
+    <variable name="WiiNunchuckButtons" type="BYTE"/>
+    <variable name="WiiButtons" type="BYTE"/>
+  </variables>
+  <globals>
+<![CDATA[
+import cwiid,commands,sys,re,os,time
+
+wiimote = None
+WIIMOTE_ADDR_MODEL = re.compile("((?:[0-9A-F]{2})(?::[0-9A-F]{2}){5})\s*Nintendo")
+nunchuckzero = None
+
+def Wiimote_cback(messages, time):
+    global nunchuckzero
+    state = dict(messages)
+    bts = state.get(cwiid.MESG_BTN, None)
+    if bts is not None:
+        PLCGlobals.WiiButtons = bts
+    nunchuck = state.get(cwiid.MESG_NUNCHUK, None)
+    if nunchuck is not None:
+        PLCGlobals.WiiNunchuckButtons = nunchuck['buttons']
+        X,Y = nunchuck['stick']
+        if nunchuckzero is None:
+            nunchuckzero = X,Y
+        (PLCGlobals.WiiNunchuckStickX,
+         PLCGlobals.WiiNunchuckStickY) = X-nunchuckzero[0],Y-nunchuckzero[1]
+
+def Connect_Wiimote(connected_callback):
+    global wiimote,nunchuckzero
+    mac_addr = ''
+    try:
+        mac_addr = file("wiimac.txt","rt").read()
+    except:
+        PLCObject.LogMessage("Wiimote MAC unknown, scanning bluetooth")
+        output = commands.getoutput("hcitool scan")
+        result = WIIMOTE_ADDR_MODEL.search(output)
+        if result is not None:
+            mac_addr = result.group(1)
+            PLCObject.LogMessage("Found Wiimote with MAC %s"%mac_addr)
+            file("wiimac.txt","wt").write(mac_addr)
+ 
+    # Connect to wiimote
+    if not mac_addr:
+        PLCObject.LogMessage("Connection to unknown Wiimote...")
+        wiimote = cwiid.Wiimote()
+    else:
+        PLCObject.LogMessage("Connection to Wiimote %s..."%mac_addr)
+        wiimote = cwiid.Wiimote(mac_addr)
+
+    if wiimote is not None:
+        nunchuckzero = None
+        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)
+    else:
+        PLCObject.LogMessage("Wiimote %s not found"%mac_addr)
+        os.remove("wiimac.txt")
+        connected_callback(None)
+
+def Disconnect_Wiimote():
+  global wiimote
+  if wiimote is not None:
+    wiimote.disable(cwiid.FLAG_MESG_IFC)
+    time.sleep(0.1)
+    wiimote.close()
+    wiimote = None
+    PLCObject.LogMessage("Wiimote disconnected")
+
+]]>
+  </globals>
+  <init>
+<![CDATA[
+]]>
+  </init>
+  <cleanup>
+<![CDATA[
+Disconnect_Wiimote()
+
+]]>
+  </cleanup>
+  <start>
+<![CDATA[
+]]>
+  </start>
+  <stop>
+<![CDATA[
+]]>
+  </stop>
+</PyFile>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/wxglade_hmi@wxglade_hmi/baseconfnode.xml	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<BaseParams Name="wxglade_hmi" IEC_Channel="0"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/wxglade_hmi@wxglade_hmi/hmi.wxg	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,53 @@
+<?xml version="1.0"?>
+<!-- generated by wxGlade 0.6.4 on Thu May 16 13:24:16 2013 -->
+
+<application path="" name="" class="" option="0" language="python" top_window="wxglade_hmi" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0" indent_amount="4" indent_symbol="space" source_extension=".cpp" header_extension=".h">
+    <object class="Class_wxglade_hmi" name="wxglade_hmi" base="EditFrame">
+        <style>wxDEFAULT_FRAME_STYLE</style>
+        <title>frame_1</title>
+        <object class="wxStaticBoxSizer" name="sizer_1" base="EditStaticBoxSizer">
+            <orient>wxVERTICAL</orient>
+            <label>Wiimote Test</label>
+            <object class="sizeritem">
+                <flag>wxEXPAND</flag>
+                <border>0</border>
+                <option>0</option>
+                <object class="wxButton" name="button_1" base="EditButton">
+                    <label>Connect Wiimote</label>
+                    <events>
+                        <handler event="EVT_BUTTON">OnConnectButton</handler>
+                    </events>
+                </object>
+            </object>
+            <object class="sizeritem">
+                <flag>wxEXPAND</flag>
+                <border>0</border>
+                <option>0</option>
+                <object class="wxButton" name="button_2" base="EditButton">
+                    <label>Disconnect Wiimote</label>
+                    <events>
+                        <handler event="EVT_BUTTON">OnDisconnectButton</handler>
+                    </events>
+                </object>
+            </object>
+            <object class="sizeritem">
+                <flag>wxEXPAND</flag>
+                <border>0</border>
+                <option>0</option>
+                <object class="wxStaticText" name="label_1" base="EditStaticText">
+                    <attribute>1</attribute>
+                    <label>Status :</label>
+                </object>
+            </object>
+            <object class="sizeritem">
+                <flag>wxEXPAND</flag>
+                <border>0</border>
+                <option>0</option>
+                <object class="wxStaticText" name="label_2" base="EditStaticText">
+                    <attribute>1</attribute>
+                    <label>no status</label>
+                </object>
+            </object>
+        </object>
+    </object>
+</application>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/wiimote/wxglade_hmi@wxglade_hmi/pyfile.xml	Thu May 16 17:40:32 2013 +0900
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<PyFile>
+  <variables/>
+  <globals>
+<![CDATA[
+from threading import Thread
+
+def OnConnectButton(self, event):
+  def OnWiiConnected(mac_addr):
+    self.label_2.SetLabel(
+      "Wiimote %s connected"%mac_addr 
+      if mac_addr else 
+      "Wiimote connection failed !")
+
+  def WiiConnected(mac_addr):
+    wx.CallAfter(OnWiiConnected,mac_addr)
+
+  Thread(target = Connect_Wiimote, args = (WiiConnected,)).start()
+  self.label_2.SetLabel("Press wiimote 1+2")
+  event.Skip()
+
+def OnDisconnectButton(self, event):
+  Disconnect_Wiimote()
+  self.label_2.SetLabel("Wiimote disconnected")
+  event.Skip()
+
+]]>
+  </globals>
+  <init>
+<![CDATA[
+]]>
+  </init>
+  <cleanup>
+<![CDATA[
+]]>
+  </cleanup>
+  <start>
+<![CDATA[
+]]>
+  </start>
+  <stop>
+<![CDATA[
+]]>
+  </stop>
+</PyFile>