-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.cpp
469 lines (373 loc) · 14.3 KB
/
cmd.cpp
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
//-------------------------------------------------------------------------//
/////////////////////////////////////////////////////////////////////////////
// +---------------------------------------------------------------------+ //
// | Detekce prikazu a parametru | //
// | ------------------- | //
// | Verze 1.23 Build 050523 | //
// | Tomas Hujer (c) 2005 | //
// +---------------------------------------------------------------------+ //
// | Detekuje sekvencni prikazy podle tabulky prikazu v parametru | //
// | CMD_TestProc(*CMDTab, Get_Char). CMDTab je pointer na tabulku | //
// | Get_Char je promenna kde se sekvencne objevuje testovany znak. | //
// | Funkce prochazi tabulkou a pri kazdem znaku se rozhoduje podle | //
// | kodu a parametru v tabulce jaka bude nasledujici akce, pokud | //
// | sekvence znaku prosla cestu v tabulce az k ExitCode, je tato | //
// | hodnota vracena. Podle navratove hodnoty mohou byt v hlavnim | //
// | programu volany prislusne funkce. | //
// +---------------------------------------------------------------------+ //
// //
// Version upgrade: //
// 1.20 ... //
// 1.22 050525 Pridany funkce IsHex a CMD_GetParHex //
// 1.23 050609 Pridany cGoNodFar3 (index + 768) //
// cGoNodFar4 (index + 1024) //
// //
/////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------//
//#define DEBUG_MODE
#include "cmd.h"
#define CMD_ParIx CMD_Par[0] // Asociace s nultym bytem v bufferu
unsigned char CMD_Par[64]; // Buffer parametru
unsigned char CMD_NumOfPar=0; // Pocet parametru
unsigned char CMD_WaitPar=0;
unsigned char CMD_WaitRet=0;
unsigned char CMD_Space=0; // Detekce a vynechani mezer
unsigned int CMDTab_ix=0; // Index v tabulce prikazu
//----------------------------------------------------------------------------------
// Prevede znak na velky
unsigned char UpChar(unsigned char Char) {
if((Char>='a') && (Char<='z')) {
return(Char & (255-32));
} else {
return(Char);
}
}
#ifdef DEBUG_MODE
// Vrati nazev makra jako ukazatel na text
unsigned char *ActionText(unsigned char Action)
{ switch(Action)
{ case cGoNod : return("GoNod");
case cExitCode : return("ExitCode");
case cWaitPar : return("WaitPar");
case cResetIx : return("ResetIx");
case cExitRet : return("ExitRet");
case cResetRun : return("RstRun");
case cGoNodFar : return("GoNodFar");
case cGoNodFar2: return("GoNodFar2");
case cGoNodFar3: return("GoNodFar3");
case cGoNodFar4: return("GoNodFar4");
case cRptRstIx : return("RptRstIx");
}
return("Undefined");
}
#endif
// Je-li znak cislo, pismeno, nebo znamenko vrati 1
unsigned char IsPrint(unsigned char Char)
{ if(((Char >= 'A') && (Char <= 'Z')) ||
((Char >= 'a') && (Char <= 'z')) ||
((Char >= '0') && (Char <= '9')) ||
(Char=='+') || (Char=='-') || (Char=='#'))
return(1);
else return(0);
}
// Je-li znak cislice vrati 1
unsigned char IsNum(unsigned char Char) {
if((Char >= '0') && (Char <= '9'))
return(1);
else
return(0);
}
// Je-li znak hexa cislice vrati 1
unsigned char IsHex(unsigned char Char) {
if(((Char >= '0') && (Char <= '9')) ||
((UpChar(Char) >= 'A') && (UpChar(Char) <= 'F')))
return(1);
else
return(0);
}
// Vynuluje buffer na ukazateli
void ResetBuffer(unsigned char *Buf, unsigned int Bytes) {
unsigned char i;
for(i=0; i<Bytes; i++) {
Buf[i]=0;
}
}
// Vrati index v bufferu parametru
//unsigned char CMD_GetParIx()
//{ return(CMD_ParIx);
//}
// Vrati ukazatel na n-ty parametr
unsigned char *CMD_GetPar(unsigned char Index) {
unsigned char i;
unsigned char ct;
if(CMD_ParIx >= sizeof(CMD_Par)) {
CMD_ParIx=(sizeof(CMD_Par)-1);
}
i=1;
ct=0;
while((i < CMD_ParIx) && (ct!=Index)) {
if(ct==Index) {
break;
}
if(!CMD_Par[i]) {
ct++;
}
i++;
}
return(CMD_Par + i);
}
// Vrati cislo zkonvertovane z n-teho parametru
unsigned int CMD_GetParNum(unsigned char Index, unsigned char Omez) {
unsigned char Num[5];
unsigned char IxNum,IxPar;
unsigned int DestInt;
unsigned int Nasob;
unsigned char Ch;
for(IxNum=0; IxNum<5; IxNum++) {
Num[IxNum]=0; // Nuluj buffer
}
// Nacti cislice do bufferu
IxNum=0;
IxPar=0;
while(IxNum<5) {
Ch = CMD_GetPar(Index)[IxPar];
if(!Ch) {
break;
}
if(!IsNum(Ch)) {
IxPar++;
} else {
Num[IxNum]=Ch; IxNum++; IxPar++;
}
}
// IxNum...PocetCislic
if((Omez) && (Omez<IxNum)) {
IxNum=Omez; // Omezeni poctu cislic
}
// Vypocitej integer ze znaku v parametru
Nasob = 1; // Nastav pocatecniho nasobitele
DestInt = 0; // Nuluj vysledne cislo
while(IxNum) {
DestInt += (Num[IxNum-1]-'0') * Nasob; // pocitej
Nasob *= 10;
IxNum--;
}
return(DestInt);
}
// Vrati hexa cislo zkonvertovane z n-teho parametru
unsigned int CMD_GetParHex(unsigned char Index, unsigned char Omez) {
unsigned char Num[5];
unsigned char IxNum,IxPar;
unsigned int DestInt;
unsigned int Nasob;
unsigned char Ch;
for(IxNum=0; IxNum<5; IxNum++) {
Num[IxNum]=0; // Nuluj buffer
}
// Nacti cislice do bufferu
IxNum=0;
IxPar=0;
while(IxNum<5) {
Ch = CMD_GetPar(Index)[IxPar];
if(!Ch) {
break;
}
if(!IsHex(Ch)) {
IxPar++;
} else {
Num[IxNum] = UpChar(Ch);
IxNum++;
IxPar++;
}
}
// IxNum...PocetCislic
if((Omez) && (Omez < IxNum)) {
IxNum=Omez; // Omezeni poctu cislic
}
// Vypocitej integer ze znaku v parametru
Nasob=1; // Nastav pocatecniho nasobitele
DestInt=0; // Nuluj vysledne cislo
while(IxNum) {
if(Num[IxNum-1]<='9') {
DestInt += (Num[IxNum-1]-'0') * Nasob; // pocitej
} else {
DestInt += ((Num[IxNum-1]-'A')+10) * Nasob; // pocitej
}
Nasob *= 16;
IxNum--;
}
return(DestInt);
}
// Porovnava znak v Get_Char se znaky v tabulce CMDTab, ridi se makry v tabulce
unsigned char CMD_TestProc(unsigned char *CMDTab, unsigned char Get_Char) {
unsigned char Repeat;
unsigned char Out;
#define CMD_Char CMDTab[CMDTab_ix]
#define CMD_Yes_Action CMDTab[CMDTab_ix+1]
#define CMD_Yes_Par CMDTab[CMDTab_ix+2]
#define CMD_No_Action CMDTab[CMDTab_ix+3]
#define CMD_No_Par CMDTab[CMDTab_ix+4]
Repeat=1;
Out=0;
#ifdef DEBUG_MODE
Serial.printf("Test char %c, adress of CMD table: %p\r\n",Get_Char,CMDTab);
#endif
if(!CMD_WaitPar) { // Pokud se neceka na parametr
while(Repeat) { // Dokud nastaveno repeat, provadej...
Repeat=0;
#ifdef DEBUG_MODE
Serial.printf("\r\nChar: %c vs %c Index: %d \r\n",Get_Char,CMDTab[CMDTab_ix],CMDTab_ix);
Serial.printf("ActionYes: %s, Par: %d\r\n",ActionText(CMD_Yes_Action),CMD_Yes_Par);
Serial.printf("ActionNo : %s, Par: %d\r\n",ActionText(CMD_No_Action),CMD_No_Par);
#endif
if(UpChar(Get_Char) == CMDTab[CMDTab_ix]) { // Zde se znak shoduje
#ifdef DEBUG_MODE
Serial.printf("%c",CMDTab[CMDTab_ix]);
#endif
switch(CMD_Yes_Action) {
case cGoNod:
CMDTab_ix += CMD_Yes_Par; // Pripocti hodnotu param.Yes
break;
case cExitCode:
Out=CMD_Yes_Par;
CMDTab_ix=0;
break;
case cWaitPar:
CMD_WaitPar = 1;
CMD_WaitRet = CMD_Yes_Par;
CMDTab_ix=0;
CMD_Space=0;
CMD_ParIx=1;
ResetBuffer(CMD_Par+1,sizeof(CMD_Par)-1);
break;
case cResetIx:
CMDTab_ix=0;
break;
case cExitRet:
Out = CMD_Yes_Par;
break;
case cResetRun:
CMDTab_ix=0;
CMD_ParIx=0;
break;
case cGoNodFar:
CMDTab_ix+=(CMD_Yes_Par + 0x100);
break;
case cGoNodFar2:
CMDTab_ix += (CMD_Yes_Par + 0x200);
break;
case cGoNodFar3:
CMDTab_ix+=(CMD_Yes_Par + 0x300);
break;
case cGoNodFar4:
CMDTab_ix+=(CMD_Yes_Par + 0x400);
break;
case cRptRstIx:
CMDTab_ix=0; CMD_ParIx=0; Repeat=1;
break;
}
} else { // Zde se znak neshoduje
switch(CMD_No_Action) {
case cGoNod: // Pripocti hodnotu par.No
CMDTab_ix += CMD_No_Par; Repeat=1;
break;
case cExitCode:
Out=CMD_No_Par;
CMDTab_ix = 0;
break;
case cWaitPar:
CMD_WaitPar = 1;
CMD_WaitRet = CMD_No_Par;
CMDTab_ix = 0;
CMD_Space = 0;
CMD_ParIx = 1;
ResetBuffer(CMD_Par,sizeof(CMD_Par));
break;
case cResetIx:
CMDTab_ix = 0;
CMD_ParIx = 0;
Repeat = 0;
break;
case cExitRet:
Out = CMD_No_Par;
break;
case cResetRun:
CMDTab_ix = 0;
break;
case cGoNodFar:
CMDTab_ix += (CMD_No_Par + 0x100);
Repeat = 1;
break;
case cGoNodFar2:
CMDTab_ix += (CMD_No_Par + 0x200); Repeat=1;
break;
case cGoNodFar3:
CMDTab_ix += (CMD_No_Par + 0x300); Repeat=1;
break;
case cGoNodFar4:
CMDTab_ix += (CMD_No_Par + 0x400);
Repeat=1;
break;
case cRptRstIx:
CMDTab_ix = 0;
CMD_ParIx = 0;
Repeat = 1;
break;
}
}
}
} else { // Ukladej parametr
switch(Get_Char) {
case ';':
case '@':
case '*':
case '#':
case 10:
case 13:
if(CMD_ParIx < sizeof(CMD_Par)) { // Ukonci ukladani par.
CMD_Par[CMD_ParIx]=0;
CMD_ParIx++;
CMD_NumOfPar++;
}
CMDTab_ix=0;
CMD_WaitPar=0;
CMD_Space=0;
Out=CMD_WaitRet;
break;
case '/': // Dalsi parametr
case '.':
case ':':
case ',':
case ' ':
if(CMD_ParIx < sizeof(CMD_Par)) { // Pokud je jeste misto v bufferu
if(CMD_ParIx > 1) { // a index je vetsi nez 1
if(!CMD_Space) { // a znak neni mezera
CMD_Par[CMD_ParIx]=0; // Zapis do bufferu konec x.parametru
CMD_ParIx++; // Dalsi znak
CMD_NumOfPar++; // Inc pocet parametru
}
} else {
CMD_NumOfPar=0; // jinak nuluj pocet parametru
}
}
break;
case '"':
break;
default:
if(CMD_ParIx < sizeof(CMD_Par)) {
if(IsPrint(Get_Char)) {
CMD_Par[CMD_ParIx] = Get_Char;
} else {
CMD_Par[CMD_ParIx] = 0;
CMDTab_ix = 0;
CMD_WaitPar = 0;
Out=CMD_WaitRet;
}
CMD_ParIx++;
CMD_Space = 0;
}
break;
}
}
return(Out);
}