-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFredCom.h
More file actions
374 lines (343 loc) · 11.8 KB
/
FredCom.h
File metadata and controls
374 lines (343 loc) · 11.8 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#ifndef FREDCOM_H
#define FREDCOM_H
#include <stdint.h>
#include "QueueRingBuffer.h"
#include <Arduino.h>
//#define USEFLOAT32
//#define USEFLOAT64
enum varType {
var_uint8_t = 1,
var_uint16_t = 2,
var_uint32_t = 3,
var_uint64_t = 4,
var_int8_t = 5,
var_int16_t = 6,
var_int32_t = 7,
var_int64_t = 8,
var_float32_t = 9,
var_float64_t = 10,
};
struct varSendPackage {
varType type;
union
{
uint8_t var_uint8_t;
uint16_t var_uint16_t;
uint32_t var_uint32_t;
uint64_t var_uint64_t;
int8_t var_int8_t;
int16_t var_int16_t;
int32_t var_int32_t;
int64_t var_int64_t;
#ifdef USEFLOAT32
float32_t var_float32_t;
#else
float var_float32_t;
#endif // USEFLOAT32
#ifdef USEFLOAT64
float64_t var_float64_t;
#else
double var_float64_t;
#endif // USEFLOAT64
};
};
template <uint16_t receiveBufferSize, uint16_t sendBufferSize> class FredCom {
public:
void(*messageCallback)(uint8_t op, QueueRingBuffer<receiveBufferSize>* data);
void(*uint8Callback)(uint8_t val);
void(*uint16Callback)(uint16_t val);
void(*uint32Callback)(uint32_t val);
void(*uint64Callback)(uint64_t val);
void(*int8Callback)(int8_t val);
void(*int16Callback)(int16_t val);
void(*int32Callback)(int32_t val);
void(*int64Callback)(int64_t val);
#ifdef USEFLOAT32
void(*float32Callback)(float32_t val);
#else
void(*float32Callback)(float val);
#endif
#ifdef USEFLOAT64
void(*float64Callback)(float64_t val);
#else
void(*float64Callback)(double val);
#endif
FredCom() {
messageCallback = 0;
in_cobsIsInMessage = false;
in_lastCode = 0;
in_nonZeroBytesRemaining = 0;
out_lastCodeIndex = 0;
out_transmissionID = 0;
}
void setup() {
Serial.write((const uint8_t)0);//make sure that pc gets fresh frame before we send stuff
}
void sendMessage(uint8_t op, uint8_t *data, uint16_t offset, uint8_t len) {
sendBuffer.clear();
sendBuffer.enqueue(1);
out_lastCodeIndex = 0;
uint8_t checksum = 127;
COBSAppend(out_transmissionID);
checksum *= out_transmissionID++;
COBSAppend(len);
checksum *= len;
COBSAppend(op);
checksum *= op;
for (int i = offset; i < len + offset; i++)
{
uint8_t b = data[i];
COBSAppend(b);
checksum *= b;
}
COBSAppend(checksum);
sendBuffer.enqueue(0);
while (sendBuffer.getContentLen() > 0) {
Serial.write(sendBuffer.dequeue());
}
}
void loop() {
int bytes = Serial.available();
for (int i = 0; i < bytes; i++) {
uint8_t b = Serial.read();
if (b > 0) {
if (in_nonZeroBytesRemaining > 0) {
receiveBuffer.enqueue(b);
in_nonZeroBytesRemaining--;
}
else {
in_nonZeroBytesRemaining = b - 1;
if (in_lastCode != 0xFF && in_cobsIsInMessage) { //first byte doesnt mean 0
receiveBuffer.enqueue(0);
}
in_lastCode = b;
}
in_cobsIsInMessage = true;
}
else {
if (in_cobsIsInMessage) { //end of frame
uint8_t checksum = receiveBuffer.pop();
uint8_t tid = receiveBuffer.dequeue();
uint8_t len = receiveBuffer.dequeue();
uint8_t op = receiveBuffer.dequeue();
bool frameValid = true;
uint8_t nackReason = 0;
uint8_t r1, r2;
if (frameValid && len != receiveBuffer.getContentLen()) {//check for correct length
frameValid = false;
r1 = len;
r2 = receiveBuffer.getContentLen();
nackReason = 1;
}
if (frameValid) {
uint8_t testChecksum = 127;
testChecksum *= tid;
testChecksum *= len;
testChecksum *= op;
for (int i = 0; i < len; i++) {
testChecksum *= receiveBuffer.peekBottom(i);
}
if (testChecksum != checksum) {
frameValid = false;
nackReason = 2;
}
}
if (in_nonZeroBytesRemaining != 0) {
frameValid = false;
nackReason = 3;
}
if (frameValid) {
switch (op)
{
case 250:
break;
case 251:
break;
case 252:
break;
case 253:
break;
case 254:
break;
case 255:
{
varSendPackage *pckg = (varSendPackage*)receiveBuffer.getBuffer();
switch (pckg->type)
{
case varType::var_uint8_t:
if (uint8Callback) {
uint8Callback(pckg->var_uint8_t);
}
break;
case varType::var_uint16_t:
if (uint16Callback) {
uint16Callback(pckg->var_uint16_t);
}
break;
case varType::var_uint32_t:
if (uint32Callback) {
uint32Callback(pckg->var_uint32_t);
}
break;
case varType::var_uint64_t:
if (uint64Callback) {
uint64Callback(pckg->var_uint64_t);
}
break;
case varType::var_int8_t:
if (int8Callback) {
int8Callback(pckg->var_int8_t);
}
break;
case varType::var_int16_t:
if (int16Callback) {
int16Callback(pckg->var_int16_t);
}
break;
case varType::var_int32_t:
if (int32Callback) {
int32Callback(pckg->var_int32_t);
}
break;
case varType::var_int64_t:
if (int64Callback) {
int64Callback(pckg->var_int64_t);
}
break;
case varType::var_float32_t:
if (float32Callback) {
float32Callback(pckg->var_float32_t);
}
break;
case varType::var_float64_t:
if (float64Callback) {
float64Callback(pckg->var_float64_t);
}
break;
}
}
break;
default:
if (messageCallback) {
messageCallback(op, &receiveBuffer);
}
break;
}
sendACK(tid);
}
else {
sendNACK(tid, nackReason, r1, r2); //nack transmission id
}//else error in transmission. ignore frame
receiveBuffer.clear();
}
in_cobsIsInMessage = false;
}
}
}
void send(uint8_t val) {
varSendPackage pack;
pack.type = varType::var_uint8_t;
pack.var_uint8_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(uint16_t val) {
varSendPackage pack;
pack.type = varType::var_uint16_t;
pack.var_uint16_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(uint32_t val) {
varSendPackage pack;
pack.type = varType::var_uint32_t;
pack.var_uint32_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(uint64_t val) {
varSendPackage pack;
pack.type = varType::var_uint64_t;
pack.var_uint64_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(int8_t val) {
varSendPackage pack;
pack.type = varType::var_int8_t;
pack.var_int8_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(int16_t val) {
varSendPackage pack;
pack.type = varType::var_int16_t;
pack.var_int16_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(int32_t val) {
varSendPackage pack;
pack.type = varType::var_int32_t;
pack.var_int32_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
void send(int64_t val) {
varSendPackage pack;
pack.type = varType::var_int64_t;
pack.var_int64_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
#ifdef USEFLOAT32
void send(float32_t val) {
#else
void send(float val) {
#endif // USEFLOAT32
varSendPackage pack;
pack.type = varType::var_float32_t;
pack.var_float32_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
#ifdef USEFLOAT64
void send(float64_t val) {
#else
void send(double val) {
#endif // USEFLOAT64
varSendPackage pack;
pack.type = varType::var_float64_t;
pack.var_float64_t = val;
sendMessage(255, (uint8_t*)&pack, 0, sizeof(pack));
}
private:
int in_nonZeroBytesRemaining;
bool in_cobsIsInMessage;
uint8_t in_lastCode;
uint8_t out_transmissionID;
uint16_t out_lastCodeIndex;
QueueRingBuffer<receiveBufferSize> receiveBuffer;
QueueRingBuffer<sendBufferSize> sendBuffer;
void COBSAppend(uint8_t val) {
if (val == 0)
{
out_lastCodeIndex = sendBuffer.getContentLen();
sendBuffer.enqueue(1);
}
else
{
sendBuffer.enqueue(val);
sendBuffer.set(out_lastCodeIndex, sendBuffer.peekBottom(out_lastCodeIndex) + 1);
if (sendBuffer.peekBottom(out_lastCodeIndex) == 0xFF)
{
out_lastCodeIndex = sendBuffer.getContentLen();
sendBuffer.enqueue(1);
}
}
}
void sendACK(uint8_t id) {
sendMessage(250, &id, 0, 1);
}
void sendNACK(uint8_t id, uint8_t reason, uint8_t r1, uint8_t r2) {
uint8_t data[4] = {
id,
reason,
r1,
r2
};
sendMessage(251, data, 0, 4);
}
};
#endif // !FREDCOM_H