-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathBinaryIO.cpp
More file actions
247 lines (193 loc) · 4.86 KB
/
BinaryIO.cpp
File metadata and controls
247 lines (193 loc) · 4.86 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "./BinaryIO.h"
#include <string.h>
BinaryIO::BinaryIO(Stream& io) : _io(io) {
// nothing to do here
};
BinaryIO::~BinaryIO(){
// nothing to do here
};
BinaryIO& BinaryIO::operator<<(float value) {
_buff((uint8_t*)&value, 4);
return *this;
};
BinaryIO& BinaryIO::operator<<(uint32_t value) {
_buff((uint8_t*)&value, 4);
return *this;
};
BinaryIO& BinaryIO::operator<<(uint8_t value) {
_buff(value);
return *this;
};
BinaryIO& BinaryIO::operator<<(char value) {
_buff((uint8_t)value);
return *this;
};
BinaryIO& BinaryIO::operator<<(Packet value) {
if (value.type!=0x00) {
_buff(MARKER_BYTE);
_buff(value.payload_size+1);
_buff(value.type);
}
return *this;
};
BinaryIO& BinaryIO::operator<<(Separator value) {
// separator is ignored in binary mode
return *this;
};
void BinaryIO::_buff(uint8_t* data, uint8_t size) {
for (uint8_t i=0; i<size; i++) {
if (_pos >= BINARYIO_BUFFER_SIZE) {
_flush();
}
_buffer[_pos++] = data[i];
}
};
void BinaryIO::_buff(uint8_t data) {
if (_pos >= BINARYIO_BUFFER_SIZE) {
_flush();
}
_buffer[_pos++] = data;
};
void BinaryIO::_flush() {
if (_pos>0) {
_io.write(_buffer, _pos);
_pos = 0;
}
};
BinaryIO& BinaryIO::operator>>(float &value) {
uint8_t buf[4] = {0};
uint8_t read = 0;
_rx_fill();
while (read < 4 && _rx_available() > 0) {
int byte = _rx_read();
if (byte < 0) {
break;
}
buf[read++] = (uint8_t)byte;
}
memcpy(&value, buf, sizeof(buf));
if (read > 0) {
remaining -= read;
}
return *this;
};
BinaryIO& BinaryIO::operator>>(uint32_t &value) {
uint8_t buf[4] = {0};
uint8_t read = 0;
_rx_fill();
while (read < 4 && _rx_available() > 0) {
int byte = _rx_read();
if (byte < 0) {
break;
}
buf[read++] = (uint8_t)byte;
}
memcpy(&value, buf, sizeof(buf));
if (read > 0) {
remaining -= read;
}
return *this;
};
BinaryIO& BinaryIO::operator>>(uint8_t &value) {
_rx_fill();
int byte = _rx_read();
if (byte >= 0) {
value = (uint8_t)byte;
remaining--;
} else {
value = 0;
}
return *this;
};
PacketIO& BinaryIO::operator>>(Packet& value) {
_rx_fill();
if (_pending) {
if (_rx_available() < _pending_payload) {
value.type = 0x00;
value.payload_size = 0;
return *this;
}
value.type = _pending_type;
value.payload_size = _pending_payload;
remaining = value.payload_size;
_pending = false;
return *this;
}
// Always resync to marker, even if in_sync was true.
while (_rx_available() > 0 && _rx_peek() != MARKER_BYTE) {
in_sync = false;
_rx_read();
}
if (_rx_available() == 0) {
value.type = 0x00;
value.payload_size = 0;
return *this;
}
in_sync = true;
if (_rx_available() < 3) { // size, frame type, payload = 3 bytes minimum frame size
value.type = 0x00;
value.payload_size = 0;
return *this;
}
_rx_read(); // discard the marker
int size_byte = _rx_read();
int type_byte = _rx_read();
if (size_byte <= 0) {
in_sync = false;
value.type = 0x00;
value.payload_size = 0;
return *this;
}
uint8_t payload_size = (uint8_t)(size_byte - 1);
if (payload_size >= BINARYIO_RX_BUFFER_SIZE) {
// Payload cannot fit; drop out of sync and wait for next marker.
in_sync = false;
value.type = 0x00;
value.payload_size = 0;
return *this;
}
if (_rx_available() < payload_size) {
_pending = true;
_pending_type = (uint8_t)type_byte;
_pending_payload = payload_size;
value.type = 0x00;
value.payload_size = 0;
return *this;
}
value.type = (uint8_t)type_byte;
value.payload_size = payload_size;
remaining = value.payload_size;
return *this;
};
bool BinaryIO::is_complete() {
return (remaining <= 0);
};
void BinaryIO::_rx_fill() {
while (_io.available() > 0 && _rx_count < BINARYIO_RX_BUFFER_SIZE) {
int byte = _io.read();
if (byte < 0) {
break;
}
_rx_buffer[_rx_head] = (uint8_t)byte;
_rx_head = (uint8_t)((_rx_head + 1) % BINARYIO_RX_BUFFER_SIZE);
_rx_count++;
}
}
uint8_t BinaryIO::_rx_available() const {
return _rx_count;
}
int BinaryIO::_rx_peek() {
if (_rx_count == 0) {
return -1;
}
return _rx_buffer[_rx_tail];
}
int BinaryIO::_rx_read() {
if (_rx_count == 0) {
return -1;
}
uint8_t value = _rx_buffer[_rx_tail];
_rx_tail = (uint8_t)((_rx_tail + 1) % BINARYIO_RX_BUFFER_SIZE);
_rx_count--;
return value;
}