-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSerial.ino
311 lines (293 loc) · 6 KB
/
Serial.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
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
#define CMDUNKNOWN 0
#define CMDPING 1 // echo test
#define CMDGYRO 2
#define CMDACCEL 3
#define CMDLAMP 4
#define CMDZERO 5
#define CMDTAKEOFF 6
#define CMDLAND 7
#define CMDEMERG 8
#define CMDDEBUG 9 // toggle debug flag
#define CMDSETA 10
#define CMDSETB 11
#define CMDSETC 12
#define CMDUSR1 13
#define CMDUSR2 14
#define CMDUSR3 15
#define CMDUSR4 16
#define CMDUSR5 17
#define CMDINCR 18
#define CMDDECR 19
#define cmdBufLen 127
char cmdBuf[cmdBufLen]={0};
byte cmdPos=0;
//char cmdBuf2[cmdBufLen]={0};
//byte cmdPos2=0;
void serialEvent(void)
{
byte cmd;
int param=0;
while(Serial.available())
{
cmdBuf[cmdPos++]=(char)Serial.read();
if ((cmdBuf[cmdPos-1]=='\n') || (cmdBuf[cmdPos-1]=='\r'))
break;
if (cmdPos==cmdBufLen)
{
error("The command is too long");
Serial.flush();
clear_cmdBuf();
break;
}
}
if (cmdPos==0 || ((cmdBuf[cmdPos-1]!='\n') && (cmdBuf[cmdPos-1]!='\r')))
return;
if(debug) print_cmdBuf();
cmd=parse_cmd(¶m);
clear_cmdBuf();
if(debug)
{
Serial.print("Command: ");
Serial.print((int)cmd);
Serial.print(", Params: ");
Serial.print(param);
Serial.print(".\n");
}
switch(cmd)
{
case CMDPING:
cmd_ping();
break;
case CMDGYRO:
cmd_gyro();
break;
case CMDACCEL:
cmd_accel();
break;
case CMDLAMP:
cmd_lamp(param);
break;
case CMDZERO:
cmd_zero();
break;
case CMDTAKEOFF:
cmd_takeoff();
break;
case CMDLAND:
cmd_land();
break;
case CMDEMERG:
cmd_emerg();
break;
case CMDDEBUG:
cmd_debug(param);
break;
case CMDSETA:
cmd_seta(param);
break;
case CMDSETB:
cmd_setb(param);
break;
case CMDSETC:
cmd_setc(param);
break;
case CMDUSR1:
cmd_usr1(param);
break;
case CMDUSR2:
cmd_usr2();
break;
case CMDUSR3:
cmd_usr3();
break;
case CMDUSR4:
cmd_usr4(param);
break;
case CMDUSR5:
cmd_usr5();
break;
case CMDINCR:
cmd_incr();
break;
case CMDDECR:
cmd_decr();
break;
case CMDUNKNOWN:
error("Unknown command");
break;
default:
error("The command has been disabled");
}
if(Serial.available())
serialEvent();
}
void serialEvent2(void)
{
static byte cmdLen=0;
static char cmdBuf2[cmdBufLen]={0};
byte cmdPos2=0;
char rc;
while(Serial2.available())
{
rc=(char)Serial2.read();
if(cmdLen<0)
{
if(rc=='\n')
cmdLen--;
else
cmdLen=-1;
if(cmdLen==-4)
cmdLen=0;
continue;
}
cmdBuf2[cmdPos2++]=rc;
if(cmdPos2==1)
cmdLen=cmdBuf2[0]+3;
if(cmdLen<4 || cmdLen>25)
{
cmdLen=-1;
cmdPos2=0;
error("invalid command len");
continue;
}
if (cmdPos2-1==cmdLen)
break;
}
if (cmdPos2-1!=cmdLen)
return;
if(cmdBuf2[cmdPos2-1]!='\n' || cmdBuf2[cmdPos2-2]!='\n' || cmdBuf2[cmdPos2-3]!='\n')
{
error("bad command trailer");
cmdLen=-1;
cmdPos2=0;
if(Serial2.available())
serialEvent2();
return;
}
// if(debug) print_cmdBuf2(cmdBuf2, cmdLen);
parse_cmd2(cmdBuf2+1, cmdLen-4);
cmdPos2=0;
if(Serial2.available())
serialEvent2();
}
void serialEvent3(void)
{
while(Serial3.available())
if(gps.encode(Serial3.read()))
gps_int();
}
byte parse_cmd(int *param)
{
if (!memcmp(cmdBuf,"ping",4))
return CMDPING;
if (!memcmp(cmdBuf,"gyro",4))
return CMDGYRO;
if (!memcmp(cmdBuf,"accel",5))
return CMDACCEL;
if (!memcmp(cmdBuf,"lamp",4))
{
if (!memcmp(cmdBuf+5,"on",2))
*param=HIGH;
else if (!memcmp(cmdBuf+5,"off",3))
*param=LOW;
else
return CMDUNKNOWN;
return CMDLAMP;
}
if (!memcmp(cmdBuf,"zero",4)||!memcmp(cmdBuf,"0",1))
return CMDZERO;
if (!memcmp(cmdBuf,"takeoff",7))
return CMDTAKEOFF;
if (!memcmp(cmdBuf,"land",4))
return CMDLAND;
if (!memcmp(cmdBuf,"emerg",5))
return CMDEMERG;
if (!memcmp(cmdBuf,"debug",5))
{
if (!memcmp(cmdBuf+6,"on",2))
*param=true;
else if (!memcmp(cmdBuf+6,"off",3))
*param=false;
else
return CMDUNKNOWN;
return CMDDEBUG;
}
if (!memcmp(cmdBuf,"a ",2))
{
sscanf(cmdBuf+2,"%d",param);
return CMDSETA;
}
if (!memcmp(cmdBuf,"b ",2))
{
sscanf(cmdBuf+2,"%d",param);
return CMDSETB;
}
if (!memcmp(cmdBuf,"c ",2))
{
sscanf(cmdBuf+2,"%d",param);
return CMDSETC;
}
if (!memcmp(cmdBuf,"x ",2))
{
sscanf(cmdBuf+2,"%d",param);
return CMDUSR1;
}
if (!memcmp(cmdBuf,"y ",2))
{
sscanf(cmdBuf+2,"%d",param);
return CMDUSR4;
}
if (!memcmp(cmdBuf,"d",1))
return CMDUSR2;
if (!memcmp(cmdBuf,"c",1))
return CMDUSR5;
if (!memcmp(cmdBuf,"f",1))
return CMDUSR3;
if (!memcmp(cmdBuf,"i",1))
return CMDINCR;
if (!memcmp(cmdBuf,"k",1))
return CMDDECR;
if (cmdBuf[0]=='e' && cmdBuf[1]!='d')
return CMDEMERG;
return CMDUNKNOWN;
}
int parse_cmd2(char *cmdBuf2, byte cmdLen)
{
if (!memcmp(cmdBuf2,"ping",4))
return cmd_ping2();
if (!memcmp(cmdBuf2,"RU",2)) //Range Update command
return cmd_RU((fixed*)(cmdBuf2+2));
if (!memcmp(cmdBuf2,"WU",2)) //Range Update command
return cmd_WU((fixed*)(cmdBuf2+2));
if (!memcmp(cmdBuf2,"LU",2)) //Landing Update command
return cmd_LU((fixed*)(cmdBuf2+2), (fixed*)(cmdBuf2+2+sizeof(fixed)));
error("Unknown command");
return 0;
}
void print_cmdBuf(void)
{
Serial.print("Incomming command (");
Serial.print(strlen(cmdBuf));
Serial.print("):\n"); // a smiley
Serial.println(cmdBuf);
}
void clear_cmdBuf (void)
{
memset(cmdBuf,'\0',cmdBufLen);
//for (int i=0; i<cmdBufLen; i++)
// cmdBuf[i]=0;
cmdPos=0;
}
/*void print_cmdBuf2(void)
{
Serial.print("Incomming command (");
Serial.print(strlen(cmdBuf2));
Serial.print("):\n"); // a smiley
Serial.println(cmdBuf2);
}
void clear_cmdBuf2(void)
{
memset(cmdBuf2,'\0',cmdBufLen);
cmdPos2=0;
}
*/