-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathTextIO.cpp
205 lines (167 loc) · 4.08 KB
/
TextIO.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "./TextIO.h"
#include "Arduino.h"
TextIO::TextIO(Stream& io) : _io(io) {
// nothing to do here
};
TextIO::~TextIO(){
// nothing to do here
};
TextIO& TextIO::operator<<(float value) {
if (sep) {
_io.print(",");
}
_io.print(value, precision);
sep = true;
return *this;
};
TextIO& TextIO::operator<<(uint32_t value) {
if (sep) {
_io.print(",");
}
_io.print(value);
sep = true;
return *this;
};
TextIO& TextIO::operator<<(uint8_t value) {
if (sep) {
_io.print(",");
}
_io.print(value);
sep = true;
return *this;
};
TextIO& TextIO::operator<<(char value) {
_io.print(value);
sep = false;
return *this;
};
TextIO& TextIO::operator<<(Packet value) {
if (value.type==0x00)
_io.println();
else {
_io.print((char)value.type);
}
sep = false;
return *this;
};
TextIO& TextIO::operator<<(Separator value) {
_io.print(value.value);
sep = false;
return *this;
};
uint32_t TextIO::intFromBuffer() {
if (in_sep) {
buffer_index++; // discard the separator
}
if (buffer_index >= buffer_len)
in_sync = false;
uint32_t value = 0;
while (buffer_index < buffer_len) {
char c = buffer[buffer_index];
if (c >= '0' && c <= '9') {
value = value * 10 + (c - '0');
buffer_index++;
}
else {
break;
}
}
return value;
};
TextIO& TextIO::operator>>(float &value) {
if (in_sep) {
buffer_index++; // discard the separator
in_sep = false;
}
if (buffer_index >= buffer_len) {
in_sync = false;
return *this;
}
char c = buffer[buffer_index];
int8_t sign = 1;
if (c == '-') {
buffer_index++;
sign = -1;
}
uint32_t val = 0;
if (c != '.')
val = intFromBuffer();
if (buffer_index < buffer_len) {
c = buffer[buffer_index];
if (c == '.') {
uint8_t pos = ++buffer_index;
uint32_t frac = intFromBuffer();
if (pos < buffer_index) {
value = ((float)val + (float)frac / pow(10, buffer_index - pos)) * sign;
in_sep = true;
}
else
in_sync = false;
return *this;
}
}
value = (float)val * sign;
in_sep = true;
return *this;
};
TextIO& TextIO::operator>>(uint32_t &value) {
value = intFromBuffer();
in_sep = true;
return *this;
};
TextIO& TextIO::operator>>(uint8_t &value) {
value = (uint8_t)intFromBuffer();
in_sep = true;
return *this;
};
TextIO& TextIO::operator>>(Packet &value) {
while (!in_sync && _io.available() > 0) {
char skip = _io.read();
if (skip == '\n' || skip == '\r') {
in_sync = true;
buffer_len = 0;
}
}
if (buffer_index >= buffer_len) {
buffer_len = 0;
buffer_index = 0;
}
int avail = _io.available();
while (in_sync && avail>0) {
uint8_t peek = _io.peek();
if (peek == '\n' || peek == '\r') {
// skip newlines and carriage returns
while (avail>0 && (peek == '\n' || peek == '\r')) {
_io.read(); // discard the \n
avail = _io.available();
if (avail>0)
peek = _io.peek();
}
if (buffer_len>1) {
value.type = buffer[0];
value.payload_size = 0;
in_sep = false;
buffer_index = 1;
return *this;
}
else
buffer_len = 0;
}
else {
if (buffer_len < SIMPLEFOC_TEXTIO_BUFFER_SIZE) {
buffer[buffer_len++] = _io.read();
avail = _io.available();
}
else {
buffer_len = 0;
in_sync = false;
}
}
}
value.type = 0x00;
value.payload_size = 0;
return *this;
};
bool TextIO::is_complete() {
return buffer_index >= buffer_len;
};