tool/cmd_states.cpp
author Florian Pose <fp@igh-essen.com>
Thu, 24 Jul 2008 07:40:45 +0000
changeset 1139 074caa25365e
parent 1137 a73c0f54be42
permissions -rw-r--r--
Output requested help to stdout, help on invalid usage to stderr.
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/*****************************************************************************
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 * $Id$
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 ****************************************************************************/
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#include <iostream>
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
using namespace std;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
#include "globals.h"
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
/****************************************************************************/
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
const char *help_states =
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    15
    "[OPTIONS] <STATE>\n"
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
    "\n"
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    17
    "Request an application-layer state change for the specified slaves.\n"
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
    "\n"
1137
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    19
    "Arguments:\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    20
    "  STATE can be 'INIT', 'PREOP', 'SAFEOP', or 'OP'\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    21
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    22
    "Command-specific options:\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    23
    "  --slave -s <index>  Positive numerical ring position, or 'all' for\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    24
    "                      all slaves (default).\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    25
    "\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    26
    "Numerical values can be specified either with decimal (no prefix),\n"
a73c0f54be42 Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents: 1136
diff changeset
    27
    "octal (prefix '0') or hexadecimal (prefix '0x') base.\n";
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
/****************************************************************************/
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
void command_states(void)
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
{
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1130
diff changeset
    33
    stringstream err;
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
    string stateStr;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
    uint8_t state;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
    
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
    if (commandArgs.size() != 1) {
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1130
diff changeset
    38
        err << "'" << commandName << "' takes exactly one argument!";
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1130
diff changeset
    39
        throw InvalidUsageException(err);
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
    }
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
    stateStr = commandArgs[0];
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
    transform(stateStr.begin(), stateStr.end(),
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
            stateStr.begin(), (int (*) (int)) std::toupper);
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
    if (stateStr == "INIT") {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
        state = 0x01;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
    } else if (stateStr == "PREOP") {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
        state = 0x02;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
    } else if (stateStr == "SAFEOP") {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
        state = 0x04;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
    } else if (stateStr == "OP") {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
        state = 0x08;
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
    } else {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
        err << "Invalid state '" << commandArgs[0] << "'!";
1136
a0982873d655 Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents: 1130
diff changeset
    56
        throw InvalidUsageException(err);
1126
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
    }
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
    masterDev.open(MasterDevice::ReadWrite);
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    if (slavePosition == -1) {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
        unsigned int i, numSlaves = masterDev.slaveCount();
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
        for (i = 0; i < numSlaves; i++)
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
            masterDev.requestState(i, state);
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    } else {
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
        masterDev.requestState(slavePosition, state);
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
    }
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
}
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
b09fd81894cb Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
/*****************************************************************************/