-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_handler.ino
197 lines (178 loc) · 3.76 KB
/
command_handler.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
#define CMDUNKNOWN 0
#define CMDPING 1 // echo test
#define CMDRANGEA 2
#define CMDRANGEB 3
#define CMDDEBUG 9 // toggle debug flag
#define CMDUSR1 13
#define CMDUSR2 14
#define CMDUSR3 15
#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 CMDDEBUG:
cmd_debug(param);
break;
case CMDRANGEA:
cmd_rangea();
break;
case CMDRANGEB:
cmd_rangeb();
break;
case CMDUNKNOWN:
error("Unknown command");
break;
default:
error("The command has been disabled");
}
if(Serial.available())
serialEvent();
}
void serialEvent2(void)
{
static char 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();
}
byte parse_cmd(int *param)
{
if (!memcmp(cmdBuf,"ping",4))
return CMDPING;
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,"ra",2))
return CMDRANGEA;
if (!memcmp(cmdBuf,"rb",2))
return CMDRANGEB;
return CMDUNKNOWN;
}
int parse_cmd2(char *cmdBuf2, byte cmdLen)
{
if (!memcmp(cmdBuf2,"ping",4))
return cmd_ping();
if (!memcmp(cmdBuf2,"debug",5))
{
if (!memcmp(cmdBuf2+6,"on",2))
return cmd_debug(true);
else if (!memcmp(cmdBuf2+6,"off",3))
return cmd_debug(false);
error("debug: wrong parameters");
return -1;
}
if (!memcmp(cmdBuf2,"ra",2))
return cmd_rangea();
if (!memcmp(cmdBuf2,"rb",2))
return cmd_rangeb();
if (!memcmp(cmdBuf2,"QU",2)) //Quaternion Update command
return cmd_QU((quaternion*)(cmdBuf2+2));
error("Unknown command");
return -1;
}
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);
cmdPos=0;
}
void print_cmdBuf2(char *cmdBuf2, byte cmdLen)
{
Serial.print("Incomming command (");
Serial.print(strlen(cmdBuf2));
Serial.print(", ");
Serial.print(cmdLen);
Serial.print("):\n");
Serial.println(cmdBuf2);
}