Subversion Repositories Projects

Rev

Rev 211 | Rev 216 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
211 dhylands 1
/****************************************************************************
2
*
3
*   Copyright (c) 2009 Dave Hylands     <dhylands@gmail.com>
4
*
5
*   This program is free software; you can redistribute it and/or modify
6
*   it under the terms of the GNU General Public License version 2 as
7
*   published by the Free Software Foundation.
8
*
9
*   Alternatively, this software may be distributed under the terms of BSD
10
*   license.
11
*
12
*   See README and COPYING for more details.
13
*
14
****************************************************************************/
15
/**
16
*
17
*   @file   bioloid.cpp
18
*
19
*   @brief  Implements a PC version of the bioloid command line interface
20
*           which allows bioloid commands to be sent to either a real
21
*   bioloid bus, or a simulated one.
22
*
23
****************************************************************************/
24
 
213 dhylands 25
#if defined( AVR )
26
#define USE_COMMAND_LINE    0
27
#else
28
#define USE_COMMAND_LINE    1
29
#endif
30
 
211 dhylands 31
// ---- Include Files -------------------------------------------------------
32
 
33
#include <stdio.h>
34
#include <stdlib.h>
213 dhylands 35
 
36
#if USE_COMMAND_LINE
211 dhylands 37
#include <getopt.h>
213 dhylands 38
#endif
211 dhylands 39
 
40
#include "Log.h"
41
#include "BioloidBus.h"
42
#include "BioloidDevice.h"
43
#include "BioloidCommandLine.h"
44
#include "bioloid-reg-servo.h"
213 dhylands 45
#include "SerialPort.h"
46
#include "SerialBus.h"
211 dhylands 47
 
48
// ---- Public Variables ----------------------------------------------------
49
// ---- Private Constants and Types -----------------------------------------
50
 
213 dhylands 51
#define DEFAULT_BAUD    "1000000"
211 dhylands 52
 
53
#if defined( linux )
54
#   define  DEFAULT_PORT    "ttyUSB0"
55
#else
56
#   define  DEFAULT_PORT    "com1"
57
#endif
58
 
59
// ---- Private Variables ---------------------------------------------------
60
 
61
enum
62
{
63
    // Options assigned a single character code can use that charater code
64
    // as a short option.
65
 
66
    OPT_BAUD        = 'b',
67
    OPT_DEBUG       = 'd',
68
    OPT_PORT        = 'p',
69
    OPT_VERBOSE     = 'v',
70
 
71
    // Options from this point onwards don't have any short option equivalents
72
 
73
    OPT_FIRST_LONG_OPT   = 0x80,
74
 
75
    OPT_HELP,
76
};
77
 
213 dhylands 78
#if USE_COMMAND_LINE
211 dhylands 79
static struct option gOption[] =
80
{
81
    { "baud",       required_argument,  NULL,       OPT_BAUD },
82
    { "debug",      no_argument,        NULL,       OPT_DEBUG },
83
    { "help",       no_argument,        NULL,       OPT_HELP },
84
    { "port",       required_argument,  NULL,       OPT_PORT },
85
    { "verbose",    no_argument,        NULL,       OPT_VERBOSE },
86
    { NULL }
87
};
213 dhylands 88
#endif
211 dhylands 89
 
213 dhylands 90
SerialPort      gSerialPort;
91
SerialBus       gSerialBus;
92
BioloidDevice   gDev( &gSerialBus, 1 );
211 dhylands 93
 
94
static BLD_DevType_t    gDevType[] =
95
{
96
    { "servo",   BLD_gServoReg },
97
    { NULL, NULL },
98
};
99
 
100
// ---- Private Function Prototypes -----------------------------------------
101
 
102
// ---- Functions -----------------------------------------------------------
103
 
104
/***************************************************************************/
105
/**
106
*   Print out the program usage.
107
*/
108
 
213 dhylands 109
#if USE_COMMAND_LINE
211 dhylands 110
void Usage()
111
{
112
    fprintf( stderr, "Usage: bioloid <option(s)> <file>\n" );
113
    fprintf( stderr, "  Send commands to bioloid devices\n" );
114
    fprintf( stderr, "\n" );
115
    fprintf( stderr, "  -b, --baud=baud   Set the baudrate used\n" );
116
    fprintf( stderr, "  -p, --port=name   Set the serial port to use\n" );
117
    fprintf( stderr, "  -d, --debug       Enable debug features\n" );
118
    fprintf( stderr, "  -h, --help        Display this message\n" );
119
    fprintf( stderr, "  -v, --verbose     Turn on verbose messages\n" );
120
 
121
} // Usage
213 dhylands 122
#endif
211 dhylands 123
 
124
//***************************************************************************
125
/**
126
*   main
127
*/
128
 
129
int main( int argc, char **argv )
130
{
213 dhylands 131
#if USE_COMMAND_LINE
211 dhylands 132
    char                shortOptsStr[ sizeof( gOption ) / sizeof( gOption[ 0 ] ) + 1 ];
133
    char               *shortOpts = shortOptsStr;
134
    struct option      *scanOpt;
135
    int                 opt;
136
    int                 arg;
213 dhylands 137
    const char         *baudStr = DEFAULT_BAUD;
211 dhylands 138
    const char         *portStr = DEFAULT_PORT;
213 dhylands 139
#endif
211 dhylands 140
    char                line[ 80 ];
141
    BioloidCommandLine  cmdLine;
142
 
213 dhylands 143
#if USE_COMMAND_LINE
211 dhylands 144
    // Figure out the short options from our options structure
145
 
146
    for ( scanOpt = gOption; scanOpt->name != NULL; scanOpt++ )
147
    {
148
        if (( scanOpt->flag == NULL ) && ( scanOpt->val < OPT_FIRST_LONG_OPT ))
149
        {
150
            *shortOpts++ = (char)scanOpt->val;
151
 
152
            if ( scanOpt->has_arg != no_argument )
153
            {
154
                *shortOpts++ = ':';
155
            }
156
        }
157
    }
158
    *shortOpts++ = '\0';
159
 
160
    // Parse the command line options
161
 
162
    while (( opt = getopt_long( argc, argv, shortOptsStr, gOption, NULL )) != -1 )
163
    {
164
        switch ( opt )
165
        {
166
            case 0:
167
            {
168
                // getopt_long returns 0 for entries where flag is non-NULL
169
 
170
                break;
171
            }
172
 
173
            case OPT_BAUD:
174
            {
175
                baudStr = optarg;
176
                break;
177
            }
178
 
179
            case OPT_DEBUG:
180
            {
181
                gDebug = 1;
182
                break;
183
            }
184
 
185
            case OPT_PORT:
186
            {
187
                portStr = optarg;
188
                break;
189
            }
190
 
191
            case OPT_VERBOSE:
192
            {
193
                gVerbose = 1;
194
                break;
195
            }
196
 
197
            case '?':
198
            default:
199
            {
200
                fprintf( stderr, "opt:%d\n", opt );
201
            }
202
            case OPT_HELP:
203
            {
204
                Usage();
205
                exit( 1 );
206
            }
207
        }
208
    }
209
    argc -= optind;
210
    argv += optind;
211
 
212
    LogDebug( "Debug enabled\n" );
213
    LogVerbose( "Verbose enabled\n" );
214
    LogVerbose( "Port: %s Baud: %s\n", portStr, baudStr );
213 dhylands 215
#endif
211 dhylands 216
 
213 dhylands 217
    if ( !gSerialPort.Open( portStr, baudStr ))
218
    {
219
        LogError( "Unable to open serial port '%s'", portStr );
220
        exit( 1 );
221
    }
222
    gSerialBus.SetSerialPort( &gSerialPort );
223
    gSerialBus.SetDebug( gDebug != 0 );
224
 
211 dhylands 225
    cmdLine.RegisterDevices( gDevType );
226
 
213 dhylands 227
    cmdLine.SetBus( &gSerialBus );
228
 
211 dhylands 229
    printf( "> " );
230
    while ( fgets( line, sizeof( line ), stdin ) != NULL )
231
    {
232
        fflush( stdout );
233
        if ( !cmdLine.ProcessLine( line ))
234
        {
235
            break;
236
        }
237
 
238
        printf( "> " );
239
    }
240
 
241
    exit( 0 );
242
    return 0;
243
}
244
 
245