tool/CommandPdos.cpp
changeset 2589 2b9c78543663
parent 1968 4f682084c643
--- a/tool/CommandPdos.cpp	Thu Sep 06 14:21:02 2012 +0200
+++ b/tool/CommandPdos.cpp	Mon Nov 03 15:20:05 2014 +0100
@@ -29,6 +29,7 @@
 
 #include <iostream>
 #include <iomanip>
+#include <cstring>
 using namespace std;
 
 #include "CommandPdos.h"
@@ -51,7 +52,8 @@
         << endl
         << getBriefDescription() << endl
         << endl
-        << "The information is displayed in three layers, which are" << endl
+        << "For the default skin (see --skin option) the information" << endl
+        << "is displayed in three layers, which are" << endl
         << "indented accordingly:" << endl
         << endl
         << "1) Sync managers - Contains the sync manager information" << endl
@@ -77,10 +79,16 @@
         << "information can either originate from the SII or from the" << endl
         << "CoE communication area." << endl
         << endl
+        << "The \"etherlab\" skin outputs a template configuration" << endl
+        << "for EtherLab's generic EtherCAT slave block." << endl
+        << endl
         << "Command-specific options:" << endl
         << "  --alias    -a <alias>" << endl
         << "  --position -p <pos>    Slave selection. See the help of" << endl
         << "                         the 'slaves' command." << endl
+        << "  --skin     -s <skin>   Choose output skin. Possible values are"
+        << endl
+        << "                         \"default\" and \"etherlab\"." << endl
         << endl
         << numericInfo();
 
@@ -95,26 +103,47 @@
     SlaveList slaves;
     SlaveList::const_iterator si;
     bool showHeader, multiMaster;
-    
+
     if (args.size()) {
         stringstream err;
         err << "'" << getName() << "' takes no arguments!";
         throwInvalidUsageException(err);
     }
 
-	masterIndices = getMasterIndices();
-    multiMaster = masterIndices.size() > 1;
-    MasterIndexList::const_iterator mi;
-    for (mi = masterIndices.begin();
-            mi != masterIndices.end(); mi++) {
-        MasterDevice m(*mi);
-        m.open(MasterDevice::Read);
-        slaves = selectedSlaves(m);
-        showHeader = multiMaster || slaves.size() > 1;
-
-        for (si = slaves.begin(); si != slaves.end(); si++) {
-            listSlavePdos(m, *si, showHeader);
-        }
+    if (getSkin().empty() || getSkin() == "default") {
+        masterIndices = getMasterIndices();
+        multiMaster = masterIndices.size() > 1;
+        MasterIndexList::const_iterator mi;
+        for (mi = masterIndices.begin();
+                mi != masterIndices.end(); mi++) {
+            MasterDevice m(*mi);
+            m.open(MasterDevice::Read);
+            slaves = selectedSlaves(m);
+            showHeader = multiMaster || slaves.size() > 1;
+
+            for (si = slaves.begin(); si != slaves.end(); si++) {
+                listSlavePdos(m, *si, showHeader);
+            }
+        }
+    }
+    else if (getSkin() == "etherlab") {
+        masterIndices = getMasterIndices();
+        MasterIndexList::const_iterator mi;
+        for (mi = masterIndices.begin();
+                mi != masterIndices.end(); mi++) {
+            MasterDevice m(*mi);
+            m.open(MasterDevice::Read);
+            slaves = selectedSlaves(m);
+
+            for (si = slaves.begin(); si != slaves.end(); si++) {
+                etherlabConfig(m, *si);
+            }
+        }
+    }
+    else {
+        stringstream err;
+        err << "Invalid skin '" << getSkin() << "'!";
+        throwInvalidUsageException(err);
     }
 }
 
@@ -130,7 +159,7 @@
     ec_ioctl_slave_sync_pdo_t pdo;
     ec_ioctl_slave_sync_pdo_entry_t entry;
     unsigned int i, j, k;
-    
+
     if (showHeader && slave.sync_count)
         cout << "=== Master " << m.getIndex()
             << ", Slave " << slave.position << " ===" << endl;
@@ -177,4 +206,111 @@
     }
 }
 
+/****************************************************************************/
+
+void CommandPdos::etherlabConfig(
+        MasterDevice &m,
+        const ec_ioctl_slave_t &slave
+        )
+{
+    ec_ioctl_slave_sync_t sync;
+    ec_ioctl_slave_sync_pdo_t pdo;
+    ec_ioctl_slave_sync_pdo_entry_t entry;
+    unsigned int i, j, k;
+
+    cout << "%" << endl
+        << "% Master " << m.getIndex() << ", Slave " << slave.position;
+    if (strlen(slave.order)) {
+        cout << ", \"" << slave.order << "\"";
+    }
+    cout << endl
+        << "%" << endl
+        << "function rv = slave" << slave.position << "()" << endl << endl;
+
+    cout << "% Slave configuration" << endl << endl;
+
+    cout << "rv.SlaveConfig.vendor = " << slave.vendor_id << ";" << endl
+        << "rv.SlaveConfig.product = hex2dec('"
+        << hex << setfill('0') << setw(8) << slave.product_code << "');"
+        << endl
+        << "rv.SlaveConfig.description = '" << slave.order << "';" << endl
+        << "rv.SlaveConfig.sm = { ..." << endl;
+
+    /* slave configuration */
+    for (i = 0; i < slave.sync_count; i++) {
+        m.getSync(&sync, slave.position, i);
+
+        cout << "    {" << dec << i << ", "
+            << (sync.control_register & 0x04 ? 0 : 1) << ", {" << endl;
+
+        for (j = 0; j < sync.pdo_count; j++) {
+            m.getPdo(&pdo, slave.position, i, j);
+
+            cout << "        {hex2dec('" <<
+                hex << setfill('0') << setw(4) << pdo.index << "'), ["
+                << endl;
+
+            for (k = 0; k < pdo.entry_count; k++) {
+                m.getPdoEntry(&entry, slave.position, i, j, k);
+
+                cout << "            hex2dec('"
+                    << hex << setfill('0') << setw(4) << entry.index
+                    << "'), hex2dec('"
+                    << setw(2) << (unsigned int) entry.subindex
+                    << "'), " << dec << setfill(' ') << setw(3)
+                    << (unsigned int) entry.bit_length
+                    << "; ..." << endl;
+            }
+
+            cout << "            ]}, ..." << endl;
+        }
+        cout << "        }}, ..." << endl;
+    }
+
+    cout << "    };" << endl << endl;
+
+    cout << "% Port configuration" << endl << endl;
+
+    unsigned int input = 1, output = 1;
+    for (i = 0; i < slave.sync_count; i++) {
+        m.getSync(&sync, slave.position, i);
+
+        for (j = 0; j < sync.pdo_count; j++) {
+            m.getPdo(&pdo, slave.position, i, j);
+
+            for (k = 0; k < pdo.entry_count; k++) {
+                m.getPdoEntry(&entry, slave.position, i, j, k);
+
+                if (!entry.index) {
+                    continue;
+                }
+
+                string dir;
+                unsigned int port;
+
+                if (sync.control_register & 0x04) {
+                    dir = "input";
+                    port = input++;
+                }
+                else {
+                    dir = "output";
+                    port = output++;
+                }
+
+                stringstream var;
+                var << "rv.PortConfig." << dir << "(" << dec << port
+                    << ")";
+
+                cout << var.str() << ".pdo = ["
+                    << i << ", " << j << ", " << k << ", 0];" << endl;
+                cout << var.str() << ".pdo_data_type = "
+                    << 1000 + (unsigned int) entry.bit_length
+                    << ";" << endl << endl;
+            }
+        }
+    }
+
+    cout << "end" << endl;
+}
+
 /*****************************************************************************/