-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialShell.cpp
More file actions
166 lines (143 loc) · 3.92 KB
/
SerialShell.cpp
File metadata and controls
166 lines (143 loc) · 3.92 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
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
#include "SerialShell.h"
SerialShell::SerialShell(Stream* serial) :
_serial(serial),
_functionCount(0),
_functions(NULL),
_data(NULL),
_len(0)
{
}
SerialShell::~SerialShell()
{
if (_data != NULL) delete _data;
delete _functions;
}
void SerialShell::registerHandler(
char* command,
void (*callback)(Print*, char*))
{
_functionCount++;
HandlerFunction* func_old = _functions;
_functions = new HandlerFunction[_functionCount];
for (int i = 0; i < _functionCount - 1; i++)
{
_functions[i] = func_old[i];
}
_functions[_functionCount - 1].command = command;
_functions[_functionCount - 1].callback = callback;
}
void SerialShell::readFromStream()
{
char len = 0;
if ((len = _serial->available()) > 0)
{
if (_len > 0) _len--;
char* oldData = _data;
_data = new char[_len + len + 1];
if (oldData != NULL)
{
for (int i = 0; i < _len; i++)
{
_data[i] = oldData[i];
}
delete oldData;
}
len = _serial->readBytes(_data + _len, len);
_len = _len + len + 1;
_data[_len - 1] = NULL;
}
}
void SerialShell::update()
{
readFromStream();
if (_data[_len - 2] == '\n')
{
// full command received
for (int i = 0; i < _functionCount; i++)
{
for (int c = 0; c < _len; c++)
{
if (_functions[i].command[c] == NULL
&& (_data[c] == ' ' || _data[c] == '\n'))
{
// match
char* newData = NULL;
int newDataLen = 0;
int nlPos = 0;
for (int cc = c + 1; cc < _len; cc++)
{
if (nlPos > 0)
{
newData[cc - nlPos - 1] = _data[cc];
}
else if (_data[cc] == '\n')
{
if (cc < _len - 2)
{
// multiple commands
nlPos = cc;
newDataLen = _len - cc - 1;
newData = new char[newDataLen + 1];
}
_data[cc] = NULL;
}
}
if (_data[c + 1] == NULL)
{
_functions[i].callback(_serial, NULL);
}
else
{
_functions[i].callback(_serial, _data + c + 1);
}
_len = 0;
delete _data;
_data = NULL;
if (newData != NULL)
{
_data = newData;
_len = newDataLen;
}
}
else if (_data[c] == _functions[i].command[c])
{
// ok, continue
}
else
{
// no match, go to next command
break;
}
}
}
flushTrash();
}
}
void SerialShell::flushTrash()
{
if (_data[_len - 2] == '\n')
{
char *oldData = _data;
int nlPos = 0;
for (int i = 0; i < _len; i++)
{
if (nlPos > 0)
{
_data[i - nlPos - 1] = oldData[i];
}
else if (i < _len -2 && _data[i] == '\n')
{
oldData = _data;
_data = new char[_len - i];
nlPos = i;
}
}
delete oldData;
_len = _len - nlPos - 1;
if (nlPos == 0)
{
_data = NULL;
_len = 0;
}
}
}