-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNerveSerial.cpp
168 lines (140 loc) · 2.57 KB
/
NerveSerial.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
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
167
168
#include "NerveSerial.h"
NerveSerial::NerveSerial(Stream *serial)
{
m_serial = serial;
}
void NerveSerial::set_commands(NerveCommand_t *commands)
{
m_commands = commands;
}
void NerveSerial::check_read()
{
if (read()) {
process();
clear();
}
}
int NerveSerial::read()
{
int b;
b = m_serial->read();
if (b == -1)
return 0;
m_read_buffer[m_rpos] = b;
if (b == '\n' || b == '\r') {
//m_read_buffer[m_rpos + 1] = '\0';
m_read_buffer[m_rpos] = '\0';
m_avail = 1;
}
if (m_rpos < NS_BUFFER_SIZE)
m_rpos++;
return m_avail;
}
void NerveSerial::clear()
{
m_avail = 0;
m_rpos = 0;
m_replied = 0;
}
void NerveSerial::process()
{
char i;
if (!m_avail)
return;
/*
for (i = 0; i < m_rpos && m_read_buffer[i] != '\0'; i++)
if (m_read_buffer[i] == '?')
m_read_buffer[i] = '\0';
m_nargs = 0;
m_args[0] = ++i;
for (; i < m_rpos && m_read_buffer[i] != '\0'; i++) {
if (m_read_buffer[i] == '&') {
m_read_buffer[i] = '\0';
}
else if (m_read_buffer[i] == '=') {
m_args[m_nargs] = i + 1;
if (++m_nargs >= NS_MAX_ARGS)
break;
}
}
*/
m_nargs = 0;
for (i = 0; i < m_rpos && m_read_buffer[i] != '\0'; i++) {
if (m_read_buffer[i] == ' ') {
m_read_buffer[i] = '\0';
m_args[m_nargs] = i + 1;
if (++m_nargs >= NS_MAX_ARGS)
break;
}
}
for (i = 0; m_commands[i].name != 0; i++) {
if (!strcmp(m_read_buffer, m_commands[i].name)) {
if (m_nargs >= m_commands[i].args_min) {
(*m_commands[i].func)();
// TODO reencode and send back
start_reply();
m_serial->write('\n');
return;
}
}
}
if (m_commands[i].func) {
(*m_commands[i].func)();
}
}
void NerveSerial::start_reply()
{
if (m_replied)
return;
m_replied = 1;
m_serial->print(m_read_buffer);
}
void NerveSerial::print(char *str)
{
m_serial->print(str);
}
void NerveSerial::print_arg(char *str)
{
m_serial->write(' ');
m_serial->print(str);
}
void NerveSerial::print(long num, char format)
{
m_serial->print(num, format);
}
void NerveSerial::print_arg(long num, char format)
{
m_serial->write(' ');
m_serial->print(num, format);
}
void NerveSerial::send(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
for (int i = 0; fmt[i] != '\0'; i++) {
if (fmt[i] == '%') {
switch (fmt[i + 1]) {
case 's': {
char *str = va_arg(args, char *);
m_serial->print(str);
break;
}
case 'x': {
int num = va_arg(args, int);
m_serial->print(num, HEX);
break;
}
case '%': {
m_serial->write('%');
break;
}
default:
break;
}
i++;
}
else
m_serial->write(fmt[i]);
}
va_end(args);
}