-
Notifications
You must be signed in to change notification settings - Fork 0
/
wr_console.c
489 lines (414 loc) · 9.97 KB
/
wr_console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//*****************************************************************************
//
// wr_console.c - implementataion of the Walking Robot's Console
//
//*****************************************************************************
#include "wr_console.h"
//
// Some standard keys
//
const char WR_CONSOLE_ENTER[] = {'\r','\n',0};
//
// the current base of uart
//
unsigned long ulBase;
//
// return argument with prefix
// for example:
// WRReadLongWithPrefixFormListOfArgs(
// 4, {"bar","foo","-a10","abacaba"},"-a", &answer)
// answer is 10
//
void WRReadLongWithPrefixFormArgs(int argc, char *argv[], char* prefix,long *answer)
{
int prefix_len = strlen(prefix);
for(int i = 0; i < argc; i++)
{
if( strlen(argv[i]) > prefix_len)
{
int cmp = 0; // is equivalent
for(int j = 0; j < prefix_len; j++)
if( argv[i][j] != prefix[j] ) cmp = -1;
if( cmp == 0 )
{
*answer = atol( argv[i] + prefix_len );
return;
}
}
}
}
long WRLongArgWithPrefixFormArgs(int argc, char* argv[], char* prefix, long default_result)
{
long result = default_result;
for(int i = 0; i < argc; i++)
{
if( strcmp(argv[i],prefix) == 0 )
{
if( i+1 < argc )
result = atol( argv[i+1] );
}
}
return result;
}
int WRCommandServsOff(int argc, char *argv[])
{
long mask = WRLongArgWithPrefixFormArgs(argc, argv, "-m", (1<<WR_SERV_COUNT)-1);
for(int i = 0; i < WR_SERV_COUNT; i++)
if( (1<<i)&mask )
{
WRServSetEnable( i, 0 );
}
//WRPWMTimerDisable();
return 0;
}
int WRCommandServsOn(int argc, char *argv[])
{
long mask = WRLongArgWithPrefixFormArgs(argc, argv, "-m", (1<<WR_SERV_COUNT)-1);
for(int i = 0; i < WR_SERV_COUNT; i++)
if( (1<<i)&mask )
{
WRServSetEnable( i, 1 );
}
//WRPWMTimerEnable();
return 0;
}
//
// shortcuts
//
int WRCommandLinkDo(int argc, char *argv[])
{
WRWalkingDo("stop", 0, 0);
long angle = 45;
long delay = 250;
int mask = 15;
if( strcmp(argv[0],"stand2") == 0 )
angle = 30;
if( strcmp(argv[0],"knee") == 0 )
mask = 3;
angle = WRLongArgWithPrefixFormArgs(argc, argv, "-a", angle);
delay = WRLongArgWithPrefixFormArgs(argc, argv, "-d", delay);
mask = WRLongArgWithPrefixFormArgs(argc, argv, "-m", mask);
//
for(int i = 0; i < 4; i++)
if( (1<<i) & mask )
{
WRLegDo( 'a'+i, argv[0], angle, delay );
}
return 0;
}
//
// do smth
//
int WRCommandDo(int argc, char *argv[])
{
WRWalkingDo("stop", 0, 0);
long angle = 45;
long delay = 250;
int mask = 15;
if( strcmp(argv[1],"stand2") == 0 )
angle = 30;
if( strcmp(argv[1],"knee") == 0 )
mask = 3;
angle = WRLongArgWithPrefixFormArgs(argc, argv, "-a", angle);
delay = WRLongArgWithPrefixFormArgs(argc, argv, "-d", delay);
mask = WRLongArgWithPrefixFormArgs(argc, argv, "-m", mask);
//
for(int i = 0; i < 4; i++)
if( (1<<i) & mask )
{
WRLegDo( 'a'+i, argv[1], angle, delay );
}
return 0;
}
int WRCommandLinkGo(int argc, char *argv[])
{
long angle = 30;
long delay = 300;
if( strcmp(argv[0],"left") == 0 || strcmp(argv[0],"right") == 0 )
angle = 15;
angle = WRLongArgWithPrefixFormArgs(argc, argv, "-a", angle);
delay = WRLongArgWithPrefixFormArgs(argc, argv, "-d", delay);
if( angle < 0 || angle > 80 )
return CMDLINE_COMMAND_FAIL;
WRWalkingDo( argv[0], angle, delay );
return 0;
}
//debug go
int WRCommandGo(int argc, char *argv[])
{
if( argc < 1 )
return CMDLINE_WRONG_ARGS;
long state = atol( argv[1] );
WRWalkingTakeState( state );
if( state == 0 )
{
return WRCommandLinkGo( argc-1, argv+1 );
}
return 0;
}
int WRCommandSetAngle(int argc, char *argv[])
{
if( argc < 3 )
return CMDLINE_WRONG_ARGS;
if( argc >= 3 )
{
int id = atoi( argv[1] );
int angle = atoi( argv[2] );
if( argc >= 4 )
{
long delay = atol( argv[3] );
WRServSetAngleWithDelay( id, angle, delay );
} else
WRServSetAngle( id, angle );
}
return 0;
}
int WRCommandSetPeriod(int argc, char *argv[])
{
if( argc != 3 )
return CMDLINE_WRONG_ARGS;
if( argc == 3 )
{
long id = atol( argv[1] );
long period = atol( argv[2] );
WRServSetRatio( id, period );
}
return 0;
}
//
// Code for the "factor:" commands
//
int WRCommandFactorSave(int argc, char *argv[])
{
if( argc != 3 )
return CMDLINE_WRONG_ARGS;
if( argc >= 3 )
{
long id = atol( argv[1] );
long angle = atol( argv[2] );
WRServSaveFactor(id, angle );
}
return 0;
}
int WRCommandFactorDel(int argc, char *argv[])
{
if( argc != 2 )
return CMDLINE_WRONG_ARGS;
if( argc >= 2 )
{
long id = atol( argv[1] );
WRServDeleteFactor(id);
}
return 0;
}
int WRCommandFactorShow(int argc, char *argv[])
{
if( argc != 2 )
return CMDLINE_WRONG_ARGS;
if( argc >= 2 )
{
long id = atol( argv[1] );
char str[10];
UARTStrSend( ulBase, " factor[" );
for(int i = 0; i < WR_SERV_FACTOR_SIZE; i++)
{
//int sprintf ( char * str, const char * format, ... );
sprintf ( str, "%d", wrServs[id].factor[i] );
UARTStrSend( ulBase, str );
UARTStrSend( ulBase, " " );
}
UARTStrSend( ulBase, "]" );
}
return 0;
}
//
// Code for the "save" command.
//
unsigned long pucBuffer[100];
int WRCommandSave(int argc, char *argv[])
{
if( argc != 1 )
return CMDLINE_WRONG_ARGS;
WRServSaveSettings();
return 0;
}
//
// Code for the "load" command.
//
int WRCommandLoad(int argc, char *argv[])
{
if( argc != 1 )
return CMDLINE_WRONG_ARGS;
WRServLoadSettings();
return 0;
}
//
//
//WRMeasure
//
int WRMeasure(int argc, char *argv[])
{
SendPingToSonar();
return 0;
}
void WRSendDistance(int Distance)
{
char DIS[20];
LongToStr(DIS,19,Distance);
UARTStrSend( ulBase, DIS );
}
//
// help - show help information about commands
//
int WRCommandHelp(int argc, char *argv[])
{
tCmdLineEntry *pCmdEntry = &g_sCmdTable[0];
if( argc == 1 )
{
UARTStrSend( ulBase, " list of abalible command:" );
UARTStrSend( ulBase, WR_CONSOLE_ENTER );
while(pCmdEntry->pcCmd)
{
UARTStrSend( ulBase, pCmdEntry->pcCmd );
UARTStrSend( ulBase, " : " );
if( pCmdEntry->pcHelp )
UARTStrSend( ulBase, pCmdEntry->pcHelp );
else
UARTStrSend( ulBase, "no info" );
UARTStrSend( ulBase, WR_CONSOLE_ENTER );
pCmdEntry++;
}
return 0;
}
if( argc == 2 )
{
while(pCmdEntry->pcCmd)
{
if( strcmp(argv[1], pCmdEntry->pcCmd) == 0)
{
UARTStrSend( ulBase, " : " );
if( pCmdEntry->pcHelp )
UARTStrSend( ulBase, pCmdEntry->pcHelp );
else
UARTStrSend( ulBase, "no info" );
return 0;
}
pCmdEntry++;
}
UARTStrSend( ulBase, " no such command" );
return 0;
}
return CMDLINE_WRONG_ARGS;
}
//
// The table of commands supported by WRConsole.
//
tCmdLineEntry g_sCmdTable[] =
{
{ "help", WRCommandHelp, "help [command] - manual about this command" },
{ "fsave", WRCommandFactorSave, "factor: save current position of serv $1 as $2 angle" },
{ "fdel", WRCommandFactorDel, "factor: erase all factors of servo $1" },
{ "fshow", WRCommandFactorShow, "factor: show all factros of servo $1" },
{ "save", WRCommandSave, "save current servs settings: factors etc." },
{ "load", WRCommandLoad, "load settings of servos" },
{ "seta", WRCommandSetAngle, "move serv $1 to position with $2 angle" },
{ "setp", WRCommandSetPeriod, "move serv $1 to postition with $2 period of pwm" },
{ "on", WRCommandServsOn, "turn on voltage" },
{ "off", WRCommandServsOff, "turn off voltage" },
{ "do", WRCommandDo, "" },
{ "go", WRCommandGo, "" },
{ "stand", WRCommandLinkDo, "" },
{ "stand2", WRCommandLinkDo, "" },
{ "sit", WRCommandLinkDo, "" },
{ "sit2", WRCommandLinkDo, "" },
{ "sit3", WRCommandLinkDo, "" },
{ "knee", WRCommandLinkDo, "" },
{ "forward", WRCommandLinkGo, "" },
{ "back", WRCommandLinkGo, "" },
{ "right", WRCommandLinkGo, "" },
{ "left", WRCommandLinkGo, "" },
{ "measure", WRMeasure, "" },
{ 0, 0, 0 }
};
//
// The main buffer of the WRConsole
//
unsigned char wrConsoleBuffer[256];
long wrConsoleBufferLen;
//
// Initialization of WRConsole
//
void WRConsoleInit(void){
wrConsoleBufferLen = 0;
}
void WRConsoleHandler()
{
}
//
// Handler of console
//
void WRConsoleCharPut(unsigned long _ulBase, unsigned char currentChar)
{
ulBase = _ulBase;
//
//if current key is the end of command
//
if( currentChar == 13 )
{
//
//try to execute this command
//
int result = CmdLineProcess( (char*)wrConsoleBuffer );
//
//write the result of this command
//
switch (result){
case CMDLINE_BAD_CMD:
UARTStrSend(ulBase, ">BAD CMD " );
break;
case CMDLINE_TOO_MANY_ARGS:
UARTStrSend(ulBase, ">TOO MANY ARGS" );
break;
case CMDLINE_WRONG_ARGS:
UARTStrSend(ulBase, ">WRONG ARGS " );
break;
case CMDLINE_COMMAND_FAIL:
UARTStrSend(ulBase, ">FAIL " );
break;
default:
UARTStrSend(ulBase, ">OK " );
break;
}
//
// Erase the console buffer
//
wrConsoleBuffer[0] = 0;
wrConsoleBufferLen = 0;
//
// Send to the console enter
//
UARTStrSend(ulBase, WR_CONSOLE_ENTER);
return;
}
//
// Otherwise, put current char to the buffer
//
if( isprint(currentChar) )
{
wrConsoleBuffer[ wrConsoleBufferLen++ ] = currentChar;
wrConsoleBuffer[ wrConsoleBufferLen ] = 0;
//
// Print it
//
UARTCharPutNonBlocking(ulBase, currentChar );
}
//
// if key is backspace
//
if( currentChar == 0x7f )
{
if( wrConsoleBufferLen > 0 )
wrConsoleBuffer[ --wrConsoleBufferLen ] = 0;
UARTCharPutNonBlocking(ulBase, currentChar );
}
}