-
Notifications
You must be signed in to change notification settings - Fork 1
/
TeleInfo.cpp
263 lines (221 loc) · 5.1 KB
/
TeleInfo.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
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
#include "Arduino.h"
#include "TeleInfo.h"
TeleInfo::TeleInfo(Stream* serial)
{
this->_cptSerial = serial;
_frame[0] = '\0';
}
/*
TeleInfo::TeleInfo(uint8_t rxPin,uint8_t txPin) : _serial(rxPin,txPin){
//_cptSerial = &_serial;
_frame[0] = '\0';
}
*/
void TeleInfo::setDebug(boolean debug){
_isDebug = debug;
}
const char * TeleInfo::getStringVal(const char * label){
int i = 0;
char * res = NULL;
while((strcmp(label,_label[i]) != 0) && i<_dataCount){
i++;
}
if(i >= _dataCount){
res = NULL;
}else{
res = _data[i];
}
return res;
}
long TeleInfo::getLongVal(const char * label){
const char * stringVal = getStringVal(label);
long res;
if(stringVal == NULL){
res = -1;
}else{
res = atol(stringVal);
}
return res;
}
void TeleInfo::printAllToSerial(){
for(int i = 0 ; i< _dataCount ; i++){
Serial.print(_label[i]);
Serial.print(" => ");
Serial.println(_data[i]);
}
}
boolean TeleInfo::available(){
return _isAvailable;
}
void TeleInfo::process(){
char caractereRecu ='\0';
while (_cptSerial->available() && !_isAvailable) {
// checksum is enough...
//if(_cptSerial->overflow()){
//_frameIndex = 0;
//if(_isDebug){
// Serial.println("overflow");
//}
//}
caractereRecu = _cptSerial->read() & 0x7F;
if(_isDebug){
Serial.print(caractereRecu,HEX);
Serial.print(" ");
if(caractereRecu == 0x20 || caractereRecu == 0x0A || caractereRecu == 0x0D || caractereRecu == 0x03)
Serial.println("");
}
//"Start Text 002" - frame start
if(caractereRecu == 0x02){
_frameIndex = 0;
}
//TODO check _ frame overflow !!!
_frame[_frameIndex] = caractereRecu;
_frameIndex++;
// "EndText 003" - frame end
if(caractereRecu == 0x03 && _frame[0] == 0x02){
_frame[_frameIndex]='\0';
_frameIndex++;
if(_isDebug){
Serial.println("");
Serial.println("*** will try to decode a new frame");
////Serial.println(_frame);
//Serial.println("*** END frame ***");
}
_isAvailable = readFrame();
}
}
}
void TeleInfo::resetAvailable(){
_isAvailable = false;
}
void TeleInfo::resetAll(){
_frameIndex=0;
_dataCount = 0;
_isAvailable = false;
}
void TeleInfo::begin()
{
//_cptSerial->begin(1200);
resetAll();
}
boolean TeleInfo::isChecksumValid(char *label, char *data, char checksum)
{
unsigned char sum = 32 ; // Somme des codes ASCII du message + un espace
int i ;
for (i=0; i < strlen(label); i++) sum = sum + label[i] ;
for (i=0; i < strlen(data); i++) sum = sum + data[i] ;
sum = (sum & 63) + 32 ;
return ( sum == checksum);
}
/**
* Read a label in 'label' buffer. Start reading at beginInder in frame buffer.
* return index pointing just after the label
*/
int TeleInfo::readLabel(int beginIndex, char* label){
int i = beginIndex;
int j=0;
while(_frame[i] != 0x20 && j < LABEL_MAX_SIZE){
label[j] = _frame[i];
j++;
i++;
}
if(j == LABEL_MAX_SIZE+1){
return -1;
}else{
label[j] = '\0';
i++;
return i;
}
}
int TeleInfo::readData(int beginIndex, char *data){
int i = beginIndex;
int j=0;
while(_frame[i] != 0x20 && j < DATA_MAX_SIZE){
data[j] = _frame[i];
j++;
i++;
}
if(j == DATA_MAX_SIZE+1){
return -1;
}else{
data[j] = '\0';
i++;
return i;
}
}
boolean TeleInfo::readFrame(){
if(_isDebug) Serial.println("will read a frame");
int j=0;
int lineIndex = 0;
boolean frameOk = true;
//start i at 1 to skip first char 0x02 (start byte)
int i=1;
while(_frame[i] != 0x03 && lineIndex < LINE_MAX_COUNT){
if(_frame[i] != 0x0A){
if(_isDebug){
Serial.print(_frame[i], HEX);
Serial.println(" frame KO 0A -- ");
}
frameOk = false;
break;
}
i++;
i = readLabel(i,_label[lineIndex]);
if(i<0) {
if(_isDebug){
Serial.println("frame KO label");
}
frameOk = false;
break;
}
if(_isDebug) {
Serial.print("label=");
Serial.println(_label[lineIndex]);
}
i = readData(i,_data[lineIndex]);
if(i<0) {
if(_isDebug) Serial.println("frame KO data");
frameOk = false;
break;
}
if(_isDebug) {
Serial.print("data=");
Serial.println(_data[lineIndex]);
}
char checksum = _frame[i];
i++;
if(_isDebug) {
Serial.print("checksum is ");
Serial.println(checksum, HEX);
}
if(_frame[i] != 0x0D){
if(_isDebug) Serial.println(" -frame KO 0D");
frameOk = false;
break;
}
i++;
if(!isChecksumValid(_label[lineIndex],_data[lineIndex],checksum)){
if(_isDebug) Serial.println("frame KO checksum");
frameOk = false;
break;
}
lineIndex++;
}
if(lineIndex >= LINE_MAX_COUNT){
if(_isDebug) Serial.println("frame KO LINE_MAX_COUNT");
frameOk = false;
}else{
_dataCount = lineIndex;
}
if(!frameOk){
_dataCount = 0;
}
if(_isDebug) {
if(!frameOk){
Serial.println("frame KO ");
}else{
Serial.println("frame OK !!! ");
}
}
return frameOk;
}