-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFasterpreter.ino
251 lines (208 loc) · 7.55 KB
/
Fasterpreter.ino
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
#define uart Serial
class Fasterpreter {
protected:
const static int dirCount=3; // Set your number of dirs here
const static int argCount=3; //Set your number of args here
char header;
char trailer;
public:
char source;
String dir[dirCount];
String strArg[argCount];
int intArg[argCount];
bool hasStrArg[argCount];
bool hasIntArg[argCount];
// Use the following if you want to check for args, commands or forwardables
bool hasArgs=false;
bool gotCmd=false;
bool gotFwd = false;
bool shouldPrintData = false;
String forwardable;
String cmd;
// constructor here
Fasterpreter(){
header = '>';
trailer = ')';
cleanInterpreter();
}
Fasterpreter( char head, char trail){
header = head;
trailer = trail;
cleanInterpreter();
}
void readStream(HardwareSerial* inStream,char src ='u'){
if(inStream->available()) {
// inStream->read(); is equivalent to *inStream.read() ------ reads "value pointed to by inStream", it gets the value pointed by the pointer, not its address
char c = inStream->read();
if(c==header) {
String serialTxt = inStream->readStringUntil(trailer);
serialTxt+=trailer;
//uart.println(serialTxt);
source = src;
parse(serialTxt);
}
// if you don't want forwardables feature, just comment the lines below
/*
else if (c=='\"') {
gotFwd=true;
forwardable= inStream->readStringUntil('\"');
// use forwardable here
}
*/
}
}
void printData(){
for(int i=0; i<dirCount; i++) {
uart.print("dir "); uart.print(i); uart.print(":");
uart.println(dir[i]);
}
for(int i=0; i<argCount; i++) {
if(hasStrArg[i]) {
uart.print("strArg "); uart.print(i); uart.print(":");
uart.println(strArg[i]);
}
else {
uart.print("Has no strArg "); uart.print(i);
}
if(hasIntArg[i]) {
uart.print("intArg "); uart.print(i); uart.print(":");
uart.println(intArg[i]);
}
else{
uart.print("Has no intArg "); uart.print(i);
}
}
}
void cleanInterpreter(){
gotFwd=false;
gotCmd=false;
//forwardable="";
/* // clean dirs only if necessary
for(int i=0;i<dirCount;i++){
dir[i]="";
}
*/
for(int i=0; i<argCount; i++) {
//intArg[i]=0;
//strArg[i]=""; // clean String Arguments only if necessary
hasStrArg[i]=false;
hasIntArg[i]=false;
}
}
void parseArgs(String rawTxt){
// parses the function arguments. E.g.:(100,10,-25)
String tmp;
int old_commaIndex =-1;
bool oneMore = true;
for(int i=0; i<argCount; i++) {
int commaIndex = rawTxt.indexOf(',',old_commaIndex+1);
if(commaIndex!=-1) {
tmp= rawTxt.substring(old_commaIndex+1,commaIndex);
char c = tmp.charAt(0);
if(!isDigit(c)&& c!='-'&& c!='+') {
strArg[i] = tmp;
hasStrArg[i] = true;
}
else {
intArg[i] = tmp.toInt();
hasIntArg[i] = true;
}
old_commaIndex = commaIndex;
}
else if(oneMore) {
oneMore=false;
tmp = rawTxt.substring(old_commaIndex+1);
char c = tmp.charAt(0);
if(!isDigit(c)&& c!='-'&& c!='+') {
/*
// removes the last ')', using length()-1 because the index starts at 0
tmp.remove(tmp.length()-1);
*/
hasStrArg[i] = true;
strArg[i] = tmp;
}
else {
intArg[i] = tmp.toInt();
hasIntArg[i] = true;
}
}
}
}
void parse(String fullTxt){
//Eg.: >car.go(100,100,20)
//>car.turn(10)
//display.show(lalalal)
//Obs.: fullTxt must already be without '>', use '>' to detect a command in the desired data stream before calling this.
gotCmd=true;
int argStartIndex = fullTxt.indexOf('(');
int argEndIndex = fullTxt.indexOf(trailer);
String rawTxt = fullTxt.substring(0,argStartIndex);
int old_dotIndex =-1;
bool oneMore = true;
for(int i=0; i<dirCount; i++) {
int dotIndex = rawTxt.indexOf('.',old_dotIndex+1);
if(dotIndex!=-1) {
dir[i] = rawTxt.substring(old_dotIndex+1,dotIndex);
old_dotIndex = dotIndex;
}
else if(oneMore) {
dir[i] = rawTxt.substring(old_dotIndex+1);
oneMore =false;
}
else dir[i] = "";
}
// if there are no arguments, just skip to Interpreter. E.g.: led.red()
if(argStartIndex+1==argEndIndex) {
hasArgs=false;
interpret();
}
else {
String justArgs = fullTxt.substring(argStartIndex+1,argEndIndex);
hasArgs=true;
parseArgs(justArgs);
interpret();
}
}
// This has to be defined later
void interpret();
};
//----------------- end of Fasterpreter class
// Instantiating the class here
Fasterpreter interpreter;
void setup() {
uart.begin(115200);
delay(10);
}
void loop() {
// This method requires the address of (& operator) a HardwareSerial object. E.g: &Serial or &Serial1
// You can also read data in another function and call interpreter.parse(Input_String_without_>_)
interpreter.readStream(&uart);
}
//-------------------------------------- Definition of interpret method here-------------------------------------------
void Fasterpreter::interpret(){
// Useful for debugging or understanding this code
if(shouldPrintData) printData();
// Using strcmp instead of String1==String2 because the first method is faster
if(strcmp(dir[0].c_str(),"wifi")==0) {
if(strcmp(dir[1].c_str(),"led")==0) {
uart.println("command is: wifi.led");
}
}
else if(strcmp(dir[0].c_str(),"math")==0) {
if(strcmp(dir[1].c_str(),"sum")==0) {
uart.print("Result is ");
uart.println(intArg[0]+intArg[1]);
}
}
// try >dna.test(Luke,Vader)
// Using strncmp to compare just n chars, it should be faster for bigger words
else if(strncmp(dir[0].c_str(),"dna",3)==0) {
if(strcmp(dir[1].c_str(),"test")==0){
String tmp = "";
tmp+= strArg[0]; tmp+= ",I'm your father!";
uart.println(tmp);
}
}
// Don't remove this line
cleanInterpreter();
}