forked from madsci1016/Arduino-EasyTransfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoftEasyTransfer.cpp
122 lines (101 loc) · 2.91 KB
/
SoftEasyTransfer.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
#include "SoftEasyTransfer.h"
#if ARDUINO > 22
//Captures address and size of struct
void SoftEasyTransfer::begin(uint8_t * ptr, uint8_t length, SoftwareSerial *theSerial){
rx_len = 0;
rx_array_inx = 0;
address = ptr;
size = length;
_serial = theSerial;
//dynamic creation of rx parsing buffer in RAM
rx_buffer = (uint8_t*) malloc(size);
}
#else
//Captures address and size of struct
void SoftEasyTransfer::begin(uint8_t * ptr, uint8_t length, NewSoftSerial *theSerial){
rx_len = 0;
rx_array_inx = 0;
address = ptr;
size = length;
_serial = theSerial;
//dynamic creation of rx parsing buffer in RAM
rx_buffer = (uint8_t*) malloc(size);
}
#endif
#if ARDUINO > 22
//Sends out struct in binary, with header, length info and checksum
void SoftEasyTransfer::sendData(){
uint8_t CS = size;
_serial->write(0x06);
_serial->write(0x85);
_serial->write(size);
for(int i = 0; i<size; i++){
CS^=*(address+i);
_serial->write(*(address+i));
}
_serial->write(CS);
}
#else
//Sends out struct in binary, with header, length info and checksum
void SoftEasyTransfer::sendData(){
uint8_t CS = size;
_serial->print(0x06, BYTE);
_serial->print(0x85, BYTE);
_serial->print(size, BYTE);
for(int i = 0; i<size; i++){
CS^=*(address+i);
_serial->print(*(address+i), BYTE);
}
_serial->print(CS, BYTE);
}
#endif
boolean SoftEasyTransfer::receiveData(){
//start off by looking for the header bytes. If they were already found in a previous call, skip it.
if(rx_len == 0){
//this size check may be redundant due to the size check below, but for now I'll leave it the way it is.
if(_serial->available() >= 3){
//this will block until a 0x06 is found or buffer size becomes less then 3.
while(_serial->read() != 0x06) {
//This will trash any preamble junk in the serial buffer
//but we need to make sure there is enough in the buffer to process while we trash the rest
//if the buffer becomes too empty, we will escape and try again on the next call
if(_serial->available() < 3)
return false;
}
if (_serial->read() == 0x85){
rx_len = _serial->read();
//make sure the binary structs on both Arduinos are the same size.
if(rx_len != size){
rx_len = 0;
return false;
}
}
}
}
if(rx_len != 0){
while(_serial->available() && rx_array_inx <= rx_len){
rx_buffer[rx_array_inx++] = _serial->read();
}
if(rx_len == (rx_array_inx-1)){
//seem to have got whole message
//last uint8_t is CS
calc_CS = rx_len;
for (int i = 0; i<rx_len; i++){
calc_CS^=rx_buffer[i];
}
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
memcpy(address,rx_buffer,size);
rx_len = 0;
rx_array_inx = 0;
return true;
}
else{
//failed checksum, need to clear this out anyway
rx_len = 0;
rx_array_inx = 0;
return false;
}
}
}
return false;
}