-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSerialCommands.ino
34 lines (27 loc) · 1.25 KB
/
SerialCommands.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
#include <CommandParser.h>
typedef CommandParser<> MyCommandParser;
MyCommandParser parser;
void cmd_test(MyCommandParser::Argument *args, char *response) {
Serial.print("string: "); Serial.println(args[0].asString);
Serial.print("double: "); Serial.println(args[1].asDouble);
Serial.print("int64: "); Serial.println((int32_t)args[2].asInt64); // NOTE: on older AVR-based boards, Serial doesn't support printing 64-bit values, so we'll cast it down to 32-bit
Serial.print("uint64: "); Serial.println((uint32_t)args[3].asUInt64); // NOTE: on older AVR-based boards, Serial doesn't support printing 64-bit values, so we'll cast it down to 32-bit
strlcpy(response, "success", MyCommandParser::MAX_RESPONSE_SIZE);
}
void setup() {
Serial.begin(9600);
while (!Serial);
parser.registerCommand("TEST", "sdiu", &cmd_test);
Serial.println("registered command: TEST <string> <double> <int64> <uint64>");
Serial.println("example: TEST \"\\x41bc\\ndef\" -1.234e5 -123 123");
}
void loop() {
if (Serial.available()) {
char line[128];
size_t lineLength = Serial.readBytesUntil('\n', line, 127);
line[lineLength] = '\0';
char response[MyCommandParser::MAX_RESPONSE_SIZE];
parser.processCommand(line, response);
Serial.println(response);
}
}