-
Notifications
You must be signed in to change notification settings - Fork 1
/
jrCMD.cpp
331 lines (262 loc) · 7.59 KB
/
jrCMD.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
#define DEBUG
//#define ARDUINO_MEGA
#include "jrCMD.h"
#include "jr_dynarray.h"
#include "jr.h"
#include <string.h>
#ifdef __AVR__
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#endif
#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
/*
byte commandCount=0;
ParserCmd *Commands;
struct ParserParam {
char c[CMD_PARAMS][CMD_FOO+1];
int i[CMD_PARAMS];
};
typedef bool jrCommandFoo(ParserParam *p);
struct ParserCmd {
char command[CMD_FOO+1];
jrCommandFoo *foo;
};
*/
int JRcmd::add(char const *cmd,jrCommandFoo *_foo) {
ParserCmd newItem;
JR_PRINTF("JRcmd::add ");JR_POINTER(cmd); JR_PRINTF(" ");JR_PRINTLN(cmd);
//strcpy(newItem.command,cmd);
newItem.command=cmd;
newItem.foo=_foo;
newItem.progmem=false;
jrDYN;jrDYN_Add(parserCmd,newItem,parserCmdCount,sizeof(ParserCmd));
return parserCmdCount;
}
/*
int JRcmd::addF(char const *cmd,jrCommandFoo *_foo) {
ParserCmd newItem;
strcpy_P(newItem.command,cmd);
JR_PRINTF("JRcmd::addF ");JR_POINTER(cmd); JR_PRINTF(" ");JR_PRINTLN(newItem.command);
//newItem.command=*cmd;
newItem.foo=_foo;
jrDYN;jrDYN_Add(parserCmd,newItem,parserCmdCount,sizeof(ParserCmd));
return parserCmdCount;
}
*/
int JRcmd::add(const __FlashStringHelper* cmd,jrCommandFoo *_foo) {
ParserCmd newItem;
//strcpy_P(newItem.command,(const char*) cmd);
newItem.command=(const char*) cmd;
newItem.foo=_foo;
newItem.progmem=true;
jrDYN;jrDYN_Add(parserCmd,newItem,parserCmdCount,sizeof(ParserCmd));
JR_PRINTF("JRcmd::addF ");JR_POINTER(cmd); JR_POINTER(_foo);JR_PRINTF(" ");JR_PRINTLN(cmd);
}
//__FlashStringHelper*
// --------------------------- default commands ------------------------
/*
bool cmd_pin2(ParserParam *p1) {
JR_PRINTLNF("cmd_pin2");
ParserParam p=*p1;
digitalWrite(p.i[1],(p.i[2]==0)?LOW:HIGH);
JR_PRINTV("port",p.i[1]);
JR_PRINTV("val",(p.i[2]==0)?LOW:HIGH);
JR_LN;
};
*/
int JRcmd::standard() {
JR_PRINTLNF("JRcmd::standard");
add(F("MAIN") ,&jr_cmd_main);
add(F("PIN") ,&jr_cmd_pin);
// add(F("PIN2") ,&cmd_pin2);
add(F("GETA") ,&jr_cmd_getA);
add(F("GETD") ,&jr_cmd_getD);
add(F("SHOW") ,&jr_cmd_show);
add(F("RESET"),&jr_cmd_reset);
add(F("SOFT") ,&jr_cmd_softreset);
add(F("CLEAN"),&jr_cmd_clean);
return parserCmdCount;
}
bool jr_cmd_pin(ParserParam *p1) {
JR_PRINTLNF("jr_cmd_pin");
ParserParam p=*p1;
digitalWrite(p.i[1],(p.i[2]==0)?LOW:HIGH);
JR_PRINTV("port",p.i[1]);
JR_PRINTV("val",(p.i[2]==0)?LOW:HIGH);
JR_LN;
};
bool jr_cmd_show(ParserParam *p1) {
//bool jr_cmd_show(ParserParam *p1){
JR_PRINTLNF("jr_cmd_show");
ParserParam p=*p1;
JR_PRINTV(F("LOW"),LOW);
JR_PRINTV(F("HIGH"),HIGH);
#ifdef __AVR__
JR_PRINTV(F("A0"),A0);
JR_PRINTV(F("A7"),A7);
#endif
#ifdef ESP8266
#endif
JR_PRINTV(F("CMD_BUF"),CMD_BUF);
JR_LN;
};
bool jr_cmd_reset(ParserParam *p1) {
//bool jr_cmd_reset(ParserParam *p1){
JR_PRINTLNF("jr_cmd_reset");
ParserParam p=*p1;
//wdt_enable(WDTO_8S); // https://ariverpad.wordpress.com/2012/02/26/resetting-the-arduino-through-software-for-fun-and-profit/
//https://www.arduino.cc/en/Hacking/Bootloader https://www.forums.adafruit.com/viewtopic.php?f=8&t=15435
#ifndef ESP8266
#endif
};
bool jr_cmd_softreset(ParserParam *p1) {
JR_PRINTLNF("jr_cmd_softreset");
};
bool jr_cmd_getA(ParserParam *p1){
//bool jr_cmd_getA(ParserParam *p1){
JR_PRINTLNF("jr_cmd_getA");
ParserParam p=*p1;
if (p.i[1]<14) {p.i[1]+=A0;}
JR_PRINTV(p.i[1],analogRead(p.i[1]));
};
bool jr_cmd_getD(ParserParam *p1) {
//bool jr_cmd_getD(ParserParam *p1){
JR_PRINTLNF("jr_cmd_getD");
ParserParam p=*p1;
JR_PRINTV(p.i[1],digitalRead(p.i[1]));
};
bool jr_cmd_clean(ParserParam *p1) {
//bool jr_cmd_getD(ParserParam *p1){
JR_PRINTLNF("jr_cmd_clean");
ParserParam p=*p1;
if (!p.i[1]) p.i[1]=20;
for (int i=0;i<p.i[1];i++) { JR_LN;}
};
bool jr_cmd_main(ParserParam *p1){
JR_PRINTLNF("jr_cmd_main");
ParserParam p=*p1;
JR_LN; JR_PRINTF("A: ");
for (int i=0;i<=8;i++) {
JR_PRINTV(i,analogRead(i+A0));
//if (i%5==3) { JR_LN;}
}
JR_LN;
#ifdef ARDUINO_MEGA
for (int i=0;i<54;i++) {
#endif
#ifndef ARDUINO_MEGA
for (int i=0;i<20;i++) {
#endif
if (i%10==0) { JR_LN; JR_PRINTF("0: ");}
if (i<10) { JR_PRINTF(" ");}
JR_PRINTV(i,digitalRead(i));
}
JR_LN;JR_LN;
};
// --------------------------- parse ------------------------
int JRcmd::parse(char *bufor,byte bufor_used){
ParserParam cmd;
if (bufor == NULL || bufor_used>=CMD_BUF) {
return -1;
}
//clear cmd
for (int l=0;l<CMD_PARAMS;l++) {
for (int k=0;k<CMD_FOO+1;k++) {
cmd.c[l][k]=0;
}
cmd.i[l]=0;
}
for (int k=0;k<CMD_BUF+1;k++) {
cmd.raw[k]=0;
}
int k=0;int j=0;
bufor[bufor_used]=' '; // for processing last params
{for (int i=0;i<bufor_used+1;i++) {
if (j>=CMD_PARAMS) break;
if (bufor[i]==' ' || k>=CMD_FOO) {
cmd.c[j][k]=0;
cmd.i[j]=atoi(cmd.c[j]);
k=0;j++;
} else {
cmd.c[j][k++]=bufor[i];
cmd.c[j][k]=0;
}
};
}
bufor[bufor_used]=0;
JR_PRINTLN(F("Receive Command:"));//
JR_PRINT(F("POS INT STR : "));
JR_PRINTLN(bufor);
for (int l=0;l<CMD_PARAMS;l++) {
JR_PRINTDEC(l);JR_PRINTF(" ");
JR_PRINTDEC(cmd.i[l]);JR_PRINTF(" ");
JR_PRINTLN(cmd.c[l]);
}
//find parserCmd
JR_PRINTLN(F("ParserCmd"));
for (int l=0;l<parserCmdCount;l++) {
//JR_VF(l);JR_PRINT(cmd.c[0]);JR_PRINTF("-");JR_PRINT(parserCmd[l].command);JR_LN;
if ((!parserCmd[l].progmem && strcmp(cmd.c[0],parserCmd[l].command)==0)
||
(parserCmd[l].progmem && strcmp_P(cmd.c[0],parserCmd[l].command)==0)) {
strcpy(cmd.raw,bufor);
JR_PRINTLN(F("EXECUTE ---- START ----"));//
JR_PRINTLN(bufor);
parserCmd[l].foo(&cmd);
JR_PRINTLN(F("EXECUTE ---- END ----"));//
return l;
}
}
return -2;
}
JRcmd::JRcmd() {
;
}
// --------------------------- Stream proceed ------------------------
int JRcmd::proceed(Stream *serial){
static char bufor[CMD_BUF]="";
// UART initialize?
static byte bufor_pos=0;
//DEBUG_PRINT();
if (serial == NULL) {
return false;
}
while (serial->available()>0 && bufor_pos<CMD_BUF) {
char c=serial->read();
// JR_VD(bufor_pos);JR_VD(c);
if ((c>='0' && c<='9') || (c>='A' && c<='Z')) {
bufor[bufor_pos++]=c;
bufor[bufor_pos]=0;
continue;
}
if ((c>='a' && c<='z')) {
bufor[bufor_pos++]=c-32;
continue;
}
if (bufor_pos && c==' ' && bufor[bufor_pos-1]!=' ') {
bufor[bufor_pos++]=c;
// bufor_pos++;
continue;
}
// JR_LN;
if (byte(c)==10 || byte(c)==13 || byte(c)==0) {
if (parse(bufor,bufor_pos)<0) {
//no command found (yet)
//print help with avaiable registered function
JR_PRINTF("Available functions:");
for (int l=0;l<parserCmdCount;l++) {
JR_PRINTF(" ");
if (parserCmd[l].progmem) JR_PRINT((const __FlashStringHelper*) parserCmd[l].command);
else JR_PRINT(parserCmd[l].command);
}
JR_LN;
}
//clear buffer
bufor_pos=0;
bufor[0]=0;
for (int l=0;l<CMD_BUF;l++) {
bufor[l]=0;
}
}
}
}