-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdParser.cpp
More file actions
75 lines (68 loc) · 1.39 KB
/
CmdParser.cpp
File metadata and controls
75 lines (68 loc) · 1.39 KB
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
/*
CmdParser.cpp - Library to react on serial command line commands
Created by Joerg Schmitz-Linneweber, December 18, 2018.
Released into the public domain.
*/
#include "Arduino.h"
#include "CmdParser.h"
CmdParser::CmdParser(parserCB pfunc, boolean prompt, boolean echo)
{
_parserCB = pfunc;
_prompt = prompt;
_echo = echo;
}
void CmdParser::init(boolean verbose)
{
if (verbose) {
Serial.print ("Simple command line parser (v");
Serial.print (_version);
Serial.println (")");
}
if (_prompt) {
Serial.print ("> ");
}
inBuffer[0] = inBuffer[maxBufferLength] = 0;
}
void CmdParser::loop(void) {
byte rc, read;
static boolean newData = false;
static byte idx = 0;
read = 0;
while (read < maxBufferLength && Serial.available() > 0) {
rc = Serial.read();
read++;
if (! newData) {
if (rc == '\n' || rc == '\r' ) {
inBuffer[idx] = 0;
if (idx > 0) {
newData = true;
}
break;
}
else {
if (idx < maxBufferLength) {
inBuffer[idx++] = rc;
}
}
}
}
if (newData) {
if (_echo) {
Serial.println (inBuffer);
}
else {
Serial.println ("");
}
if (0 != _parserCB(inBuffer, idx)) {
Serial.println("Syntax error!");
}
newData = false;
idx = 0;
if (_prompt) {
Serial.print ("> ");
}
}
}
const String CmdParser::version(void) {
return _version;
}