view main/robots/cantool/CmdStartPgm.cpp @ 229:79283dc7dc54 main

Add the "start" command, improve the "listen" command.
author Bob Cook <bob@bobcookdev.com>
date Sat, 12 Jul 2014 17:18:55 -0700
parents
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  robots/cantool/CmdStartPgm.cpp
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//
//  Command "start" implementation.
//
//  Copyright (c) 2014 Bob Cook
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
// ----------------------------------------------------------------------------------------

#include <iostream>

#include <Poco/Exception.h>
#include <Poco/NumberFormatter.h>
#include <Poco/NumberParser.h>

#include "Commands.h"

#include "packages/common/can/can_helpers.h"
#include "packages/common/can/can_messages.h"
#include "packages/common/can/can_nodes.h"

#include "packages/linux/can/CANMessage.h"
#include "packages/linux/can/CANMsgProcessor.h"

// ----------------------------------------------------------------------------------------

static void PrintHelp()
{
    std::cerr << "error: start <program>" << std::endl;
    std::cerr << "       where \"program\" may be one of:" << std::endl;
    std::cerr << "              wander" << std::endl;
    std::cerr << "              line" << std::endl;
    std::cerr << "              smbox" << std::endl;
}

// ----------------------------------------------------------------------------------------

void CommandStartPgm( const std::vector< std::string >& args )
{
    if ( args.size() != 2 )
    {
        PrintHelp();
        return;
    }

    uint32_t msgid = can_build_message_id( can_node_odr_manager,
                                           can_node_odr_display,
                                           can_dataid_odr_start_pgm );
    can_data_pgm_selection msg;

    try
    {
        if ( args[ 1 ] == "wander" )
        {
            msg.pgm_id = odr_pgm_wander;
        }
        else if ( args[ 1 ] == "line" )
        {
            msg.pgm_id = odr_pgm_straight_line;
        }
        else if ( args[ 1 ] == "smbox" )
        {
            msg.pgm_id = odr_pgm_small_box;
        }
        else
        {
            throw Poco::SyntaxException( "unrecognized program name", 1 );
        }

    }
    catch ( const Poco::SyntaxException& ex )
    {
        PrintHelp();
        return;
    }

    CANMessage::QueueToSend(
        new CANMessage( msgid, reinterpret_cast< uint8_t* >( &msg ), sizeof( msg ) ) );

    std::cout << "sent start cmd " << args[ 1 ] << std::endl;
}

// ----------------------------------------------------------------------------------------