forked from Uberi/Arduino-HardwareBLESerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLine.ino
78 lines (62 loc) · 2.45 KB
/
CommandLine.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
#include <HardwareBLESerial.h>
#include <CommandParser.h>
// ADVANCED EXAMPLE: demonstration of how HardwareBLESerial can work together with CommandParser to provide a commandline interface over BLE (NOTE: requires CommandParser to be installed separately)
/////////////
// Globals //
/////////////
// HardwareBLESerial instance
HardwareBLESerial &bleSerial = HardwareBLESerial::getInstance();
// CommandParser instance
typedef CommandParser<> MyCommandParser;
MyCommandParser parser;
/////////////////////////////////////
// CommandParser command callbacks //
/////////////////////////////////////
int positionX = 0, positionY = 0;
void cmd_move(MyCommandParser::Argument *args, char *response) {
positionX = args[0].asInt64;
positionY = args[1].asInt64;
Serial.print("MOVING "); Serial.print(args[0].asInt64); Serial.print(" "); Serial.println(args[1].asInt64);
snprintf(response, MyCommandParser::MAX_RESPONSE_SIZE, "moved to %d, %d", positionX, positionY);
}
void cmd_jump(MyCommandParser::Argument *args, char *response) {
Serial.println("JUMPING!");
snprintf(response, MyCommandParser::MAX_RESPONSE_SIZE, "jumped at %d, %d", positionX, positionY);
}
void cmd_say(MyCommandParser::Argument *args, char *response) {
Serial.print("SAYING "); Serial.println(args[0].asString);
snprintf(response, MyCommandParser::MAX_RESPONSE_SIZE, "said %s at %d, %d", args[0].asString, positionX, positionY);
}
//////////////////
// Main program //
//////////////////
void setup() {
Serial.begin(9600);
while (!Serial);
if (!bleSerial.beginAndSetupBLE("CommandLine")) {
while (true) {
Serial.println("failed to initialize HardwareBLESerial!");
delay(1000);
}
}
parser.registerCommand("move", "ii", &cmd_move); // two int64_t arguments
Serial.println("Registered command: move X Y");
parser.registerCommand("jump", "", &cmd_jump); // no arguments
Serial.println("Registered command: jump");
parser.registerCommand("say", "s", &cmd_say); // one string argument
Serial.println("Registered command: say \"SOMETHING\"");
}
void loop() {
// this must be called regularly to perform BLE updates
bleSerial.poll();
// read and process incoming commands
while (bleSerial.availableLines() > 0) {
// read line
char line[128];
bleSerial.readLine(line, 128);
// process line with CommandParser
char response[MyCommandParser::MAX_RESPONSE_SIZE]; parser.processCommand(line, response);
bleSerial.println(response);
}
delay(500);
}