-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSerialCommands.h
300 lines (291 loc) · 10.7 KB
/
SerialCommands.h
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
void displayHDC1080();
void displayHT();
void displaySGP30();
void displayDHT();
void handleSerial() {
memset(msgBuf, 0, BUFF_LENGTH);
int ix = 0;
while (SerialUSB.available() && ix < 255) {
char c = SerialUSB.read();
delay(10);
if (c > 31) msgBuf[ix++] = c;
} msgBuf[ix] = 0;
char c = msgBuf[0]; // Command
if (c == '/') {
c = msgBuf[1];
char c1 = msgBuf[2];
if (c == '>') {
char buff[256];
strcpy(buff, (char*)msgBuf + 2);
#ifdef NEED_SSD1306
oled.print("Sending msg...");
#endif // NEED_SSD1306
prepareJSONPacket(buff);
sendJSONPacket();
#ifdef NEED_SSD1306
oled.println(" done");
#endif // NEED_SSD1306
return;
} else if (c == 'D' && c1 == 'N') {
setDeviceName((char*)msgBuf + 3);
SerialUSB.println("setDeviceName in /DN");
#ifdef NEED_SSD1306
oled.println("New device name:");
oled.println(deviceName);
#endif // NEED_SSD1306
return;
} else if (c == 'A' && c1 == 'P') {
setAutoPing((char*)msgBuf + 3);
return;
} else if (c == 'E' || c == 'e') {
if (c1 == '1') needEncryption = true;
if (c1 == '0') needEncryption = false;
#ifdef NEED_SSD1306
oled.print("Encryption ");
oled.println(c1 == '1' ? "ON" : "OFF");
#endif // NEED_SSD1306
return;
} else if (strcmp((char*)msgBuf, "/HM1") == 0) {
needAuthentification = true;
char buff[64];
SerialUSB.println("needAuthentification set to: ON");
#ifdef NEED_SSD1306
oled.println("HMAC: ON");
#endif // NEED_SSD1306
return;
} else if (strcmp((char*)msgBuf, "/HM0") == 0) {
needAuthentification = false;
SerialUSB.println("needAuthentification set to: OFF");
#ifdef NEED_SSD1306
oled.println("HMAC: OFF");
#endif // NEED_SSD1306
return;
} else if (c == 'R' || c == 'r') {
if (c1 == '1') setPongBack(true);
if (c1 == '0') setPongBack(false);
#ifdef NEED_SSD1306
oled.print("PONG back ");
oled.println(c1 == '1' ? "ON" : "OFF");
#endif // NEED_SSD1306
return;
} else if (c == 'F' && c1 == 'Q') {
setFQ((char*)msgBuf + 3);
return;
} else if (c == 'S' && c1 == 'F') {
setSF((char*)msgBuf + 3);
return;
} else if (c == 'B' && c1 == 'W') {
setBW((char*)msgBuf + 3);
return;
} else if (c == 'C' && c1 == 'R') {
setCR((char*)msgBuf + 3);
return;
} else if (c == 'T' && c1 == '0') {
setTxPower((char*)msgBuf + 3);
return;
} else if (c == 'p' || c == 'P') {
if (c1 < 32) {
sendPing();
return;
} else if (c1 == 'w' || c1 == 'W') {
setPWD((char*)msgBuf + 2);
return;
} else if (c1 == 'b' || c1 == 'B') {
if (msgBuf[3] == '1') {
SerialUSB.println("--> PA_BOOST on");
LoRa.setTxPower(TxPower, 1);
} else if (msgBuf[3] == '0') {
SerialUSB.println("--> PA_BOOST off, RFO pin");
LoRa.setTxPower(TxPower, 0);
}
return;
}
} else if (c == 'O' && c1 == 'C') {
if (msgBuf[3] == '1') {
// OCP ON
OCP_ON = true;
LoRa.idle();
LoRa.writeRegister(REG_OCP, 0b00111111); // MAX OCP
LoRa.receive();
SerialUSB.println("--> OCP Trim on, Max OCP");
return;
} else if (msgBuf[3] == '0') {
// OCP Trim OFF
OCP_ON = false;
LoRa.idle();
LoRa.writeRegister(REG_OCP, 0b00011111); // NO OCP
LoRa.receive();
SerialUSB.println("--> OCP Trim off");
return;
}
} else if (c == 'D' && c1 == 'H') {
#ifdef NEED_BME
displayBME680();
#endif
#ifdef NEED_DHT
displayDHT();
#endif
#ifdef NEED_HDC1080
displayHDC1080();
#endif
#ifdef NEED_SGP30
displaySGP30();
#endif
}
} else {
// SerialUSB.println((char*)msgBuf);
showHelp();
}
}
void showHelp() {
char buff[256];
SerialUSB.println(" +==================+================================+");
sprintf(buff, " |%-18s|%32s|\n", " Command", "Explanation ");
SerialUSB.print(buff);
SerialUSB.println(" +==================+================================+");
sprintf(buff, " |%-18s|%32s|\n", "/DN<max 32 chars>", "Set device name");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " -> right now", deviceName);
SerialUSB.print(buff);
SerialUSB.println(" +==================+================================+");
sprintf(buff, " |%-18s|%32s|\n", "/>xxxxxxxxxxx", "send string xxxxxxxxxxx");
SerialUSB.print(buff);
SerialUSB.println(" +==================+================================+");
SerialUSB.println(" | OPTIONS |");
SerialUSB.println(" +==================+================================+");
sprintf(buff, " |%-18s|%32s|\n", " /E1 or /e1", "turn on encryption");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " /E0 or /e0", "turn off encryption");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " -> right now", needEncryption ? "on" : "off");
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /HM1", "turn on authentication");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " /HM0", "turn off authentication");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " -> right now", needAuthentification ? "on" : "off");
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /PW<32 chars>", "set password [32 chars]");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " /pw", "[exactly 32] (Uses AES256)");
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /R1 or /r1", "turn on PONG back [Reply on]");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " /R0 or /r0", "turn off PONG back [Reply off]");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " -> right now", pongBack ? "on" : "off");
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /FQ<float>", "Set a new LoRa frequency");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " Frequency:", "Between 862 and 1020 MHz (HF)");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%28.3f MHz|\n", " -> right now", (myFreq / 1e6));
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /SF[7-12]", "Set a new LoRa Spreading Factor");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32u|\n", " -> right now", mySF);
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /BW[0-9]", "Set a new LoRa Bandwidth");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", "", "From 0: 7.8 KHz to 9: 500 KHz");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32.3f|\n", " -> right now", BWs[myBW]);
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /CR[5-8]", "Set a new Coding Rate");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32u|\n", " -> right now", myCR);
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /TX[7-17]", "Set a new Tx Power");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32u|\n", " -> right now", TxPower);
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /OC1 or /oc1", "turn on OCP Trim");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " /OC0 or /oc0", "turn off OCP Trim");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " -> right now", OCP_ON ? "on" : "off");
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /PB1 or /pb1", "turn on PA_BOOST");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " /pb0 or /pb0", "turn off PA_BOOST");
SerialUSB.print(buff);
sprintf(buff, " |%-18s|%32s|\n", " -> right now", PA_BOOST ? "on" : "off");
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /AP[0-X]", "Set autoPING OFF (0) or ON");
SerialUSB.print(buff);
if (!needPing) sprintf(buff, " |%-18s|%32s|\n", " -> right now", "OFF");
else {
String np;
np = "ON: " + String(pingFrequency / 60e3, 1) + " mn";
sprintf(buff, " |%-18s|%32s|\n", " -> right now", np.c_str());
}
SerialUSB.print(buff);
SerialUSB.println(" +---------------------------------------------------+");
sprintf(buff, " |%-18s|%32s|\n", " /P or /p", "Send PING packet");
SerialUSB.print(buff);
SerialUSB.println(" +==================+================================+");
SerialUSB.println(" | Anything else | show this help message. |");
SerialUSB.println(" +==================+================================+");
SerialUSB.println(" | NEED_* OPTIONS | |");
SerialUSB.println(" +---------------------------------------------------+");
#ifdef NEED_DEBUG
sprintf(buff, " |%-18s|%32s|\n", " NEED_DEBUG", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_DEBUG", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_EEPROM
sprintf(buff, " |%-18s|%32s|\n", " NEED_EEPROM", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_EEPROM", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_SIDE_I2C
sprintf(buff, " |%-18s|%32s|\n", " NEED_SIDE_I2C", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_SIDE_I2C", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_SSD1306
sprintf(buff, " |%-18s|%32s|\n", " NEED_SSD1306", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_SSD1306", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_DHT
sprintf(buff, " |%-18s|%32s|\n", " NEED_DHT", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_DHT", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_BME
sprintf(buff, " |%-18s|%32s|\n", " NEED_BME", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_BME", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_HDC1080
sprintf(buff, " |%-18s|%32s|\n", " NEED_HDC1080", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_HDC1080", "OFF");
#endif
SerialUSB.print(buff);
#ifdef NEED_CCS811
sprintf(buff, " |%-18s|%32s|\n", " NEED_CCS811", "ON");
#else
sprintf(buff, " |%-18s|%32s|\n", " NEED_CCS811", "OFF");
#endif
SerialUSB.print(buff);
SerialUSB.println(" +==================+================================+");
}