forked from Piorosen/SSvJoy-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.h
More file actions
63 lines (54 loc) · 1.21 KB
/
parser.h
File metadata and controls
63 lines (54 loc) · 1.21 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
#ifndef SSVJOY_PARSER_H
#define SSVJOY_PARSER_H
#include <memory>
#include "option.h"
#include "structure.h"
void (*callback)(Type type, byte id, Data data, byte checksum, bool chk);
void (*error)();
char buffer[256] = {0, };
int buffer_idx = 0;
bool read_data = false;
bool compute_checksum(char* data, int size) {
byte check = 0;
for (int i = 0; i < size; i++) {
check ^= data[i];
}
if (check == 0) {
return true;
}
else {
return false;
}
}
// STX : $
// ETX : #
//X $(TYPE)(FLOAT DATA)(CHK 1byte)#
//X CHECKSUM 1 BYTE :
// ALL ^ Compute
void read_next(int value) {
if (buffer_idx > 200) {
error();
memset(buffer, 0, sizeof(buffer));
buffer_idx = 0;
}
else if (value == '$') {
read_data = true;
}
else if (value == '#') {
read_data = false;
ParseResult pr = *(ParseResult*)&buffer[0];
bool chk = compute_checksum(buffer, buffer_idx);
if (chk == false && !DEBUG_FLAG) {
Serial.print("Checksum Error !!!\n");
}else {
callback(pr.type, pr.id, pr.data, pr.checksum, chk);
}
memset(buffer, 0, sizeof(buffer));
buffer_idx = 0;
}
else if (read_data) {
buffer[buffer_idx] = value;
buffer_idx += 1;
}
}
#endif