forked from clearwater/gaugette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.cpp
executable file
·122 lines (106 loc) · 2.23 KB
/
Command.cpp
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
/*
Copyright (c) 2011 Guy Carpenter
*/
#include <Arduino.h>
#include "Command.h"
#define CMDBUF_LEN 40
void Command::skipWhitespace(char **line, int *len)
{
while (*len > 0 && **line == ' ') {
(*len)--;
(*line)++;
}
}
boolean Command::parseCommand(char **line, int *len, char *cmd)
{
boolean result = false;
skipWhitespace(line, len);
if (*len > 0) {
*cmd = **line;
(*len)--;
(*line)++;
result = true;
}
return result;
}
boolean Command::parseInteger(char **line, int *len, value_t *value)
{
boolean result = false;
*value = 0;
while (*len>0 && **line>='0' && **line<='9') {
*value = *value * 10 + (**line - '0');
(*len)--;
(*line)++;
result = true;
}
return result;
}
boolean Command::parseTuple(char **line, int *len, value_t *tuple)
{
boolean result = false;
skipWhitespace(line, len);
for (int i=0; i<COMMAND_TUPLE_LEN; i++) {
if (parseInteger(line, len, tuple+i+1)) {
result = true;
tuple[0] = i+1;
if (*len>0 && **line == ',') {
// found comma, so more integers expected
(*len)--;
(*line)++;
result = false; // need another int to succeed
}
else {
break;
}
}
else {
break;
}
}
return result;
}
boolean Command::parseLine(char *line, int len)
{
boolean result = false;
if (parseCommand(&line, &len, &command) &&
parseTuple(&line, &len, address) &&
parseTuple(&line, &len, value)) {
result = true;
}
else {
Serial.println("$Error Invalid command syntax");
}
return result;
}
boolean Command::parseInput()
{
boolean result = false;
static char line[CMDBUF_LEN]; // NOT zero terminated
static int len = 0;
if (Serial.available()) {
char c = Serial.read();
if (c==10 || c==13) {
result = parseLine(line, len);
len = 0;
}
else if (len < CMDBUF_LEN) {
line[len++] = c;
}
return result;
}
}
void Command::dump()
{
Serial.print(command);
Serial.print(" ");
for (int i=0;i<address[0];i++) {
if (i>0) Serial.print(",");
Serial.print(address[i+1]);
}
Serial.print(" ");
for (int i=0;i<value[0];i++) {
if (i>0) Serial.print(",");
Serial.print(value[i+1]);
}
Serial.println();
}