-
Notifications
You must be signed in to change notification settings - Fork 0
/
BayEOSBuffer.cpp
451 lines (391 loc) · 9.83 KB
/
BayEOSBuffer.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "BayEOSBuffer.h"
BayEOSBuffer::BayEOSBuffer(void) {
#if SERIAL_DEBUG
// Serial.println("BayEOSBuffer");
#endif
_rtc = NULL;
_framesDiscarded = 0;
}
uint8_t BayEOSBuffer::b_seek(unsigned long pos) {
_pos = pos;
return seek(pos);
}
uint8_t BayEOSBuffer::b_write(const uint8_t b) {
_res = write(b);
_pos++;
if (_pos == _max_length) {
b_seek(0);
}
return _res;
}
uint8_t BayEOSBuffer::b_write(const uint8_t *b, uint8_t length) {
//calculate length for first write
_res = (((unsigned long) (_pos + length)) <= _max_length ?
length : _max_length - _pos);
_res = write(b, _res);
if (_res<0)
return 0; //error!!
_pos += _res;
if (_pos == _max_length) {
b_seek(0);
}
if (_res < length) {
//second write
_res = write(b + _res, length - _res);
if(_res<0)
return 0; //error!!
_pos+=_res;
_res = length;
}
return _res;
}
int BayEOSBuffer::b_read() {
_res = read();
_pos++;
if (_pos == _max_length) {
b_seek(0);
}
return _res;
}
int BayEOSBuffer::b_read(uint8_t *dest, int length) {
//calculate length for first read
_res = (((unsigned long) (_pos + length)) <= _max_length ?
length : _max_length - _pos);
_res = read(dest, _res);
if (_res == -1)
return -1;
_pos += _res;
if (_pos == _max_length) {
b_seek(0);
}
if (_res < length) {
//second read
_res = read(dest + _res, length - _res);
if (_res == -1) return -1;
_pos = _res;
_res = length;
}
return _res;
}
unsigned long BayEOSBuffer::available(void) {
return ((unsigned long) (_max_length + _write_pos - _read_pos))
% _max_length;
}
unsigned long BayEOSBuffer::freeSpace(void) {
if(_read_pos==_write_pos) return _max_length;
return (((unsigned long) (_max_length + _read_pos - _write_pos)) % _max_length);
}
uint8_t BayEOSBuffer::freeSpace(uint8_t length) {
if (_end == _write_pos)
return (_max_length > (unsigned long) length);
#if SERIAL_DEBUG
Serial.print("Space: ");
Serial.print(length);
Serial.print(" ");
Serial.print((((unsigned long) (_max_length + _end - _write_pos)) % _max_length));
Serial.print(" Free: ");
Serial.println((((unsigned long) (_max_length + _end - _write_pos)) % _max_length)> (unsigned long) length);
#endif
return (((unsigned long) (_max_length + _end - _write_pos)) % _max_length)
> (unsigned long) length;
}
int BayEOSBuffer::readPacket(uint8_t *dest) {
return b_read(dest, _packet_length);
}
uint8_t BayEOSBuffer::readBinary(unsigned long pos, uint8_t length,
uint8_t *dest) {
b_seek(pos);
return b_read(dest, length);
}
uint8_t BayEOSBuffer::readBinary(unsigned long pos, unsigned long stop,
uint8_t length, uint8_t *dest) {
b_seek(pos);
unsigned long remaining= ((unsigned long) (_max_length + stop - pos))
% _max_length;
if(remaining<length) length=remaining;
return b_read(dest, length);
}
void BayEOSBuffer::set(unsigned long pos) {
#if SERIAL_DEBUG
Serial.print("Set: ");
Serial.println(pos);
#endif
_read_pos = pos;
_write_pos = pos;
_end = pos;
#if SERIAL_DEBUG
Serial.print(" --end: ");
Serial.print(_end);
Serial.print(" -- write: ");
Serial.print(_write_pos);
Serial.print(" -- read: ");
Serial.println(_read_pos);
#endif
}
void BayEOSBuffer::seekReadPointer(unsigned long pos) {
_read_pos = pos;
}
void BayEOSBuffer::reset(void) {
set(0);
resetStorage();
}
void BayEOSBuffer::skip(void) {
_read_pos=_write_pos;
}
uint8_t BayEOSBuffer::initNextPacket(void) {
return initPacket(_read_pos);
}
uint8_t BayEOSBuffer::initPacket(unsigned long pos) {
uint8_t* p = (uint8_t*) &_millis;
b_seek(pos);
b_read(p, 4);
_packet_length = b_read();
#if SERIAL_DEBUG
Serial.println("init Packet: ");
Serial.print("length: ");
Serial.print(_packet_length);
Serial.print(" --end: ");
Serial.print(_end);
Serial.print(" -- write: ");
Serial.print(_write_pos);
Serial.print(" -- read: ");
Serial.println(_read_pos);
#endif
return _packet_length;
}
void BayEOSBuffer::next(void) {
if (_packet_length == 0) {
#if SERIAL_DEBUG
Serial.println("Packet Length is 0. This should not happen...");
#endif
_read_pos = _write_pos;
} else {
_read_pos += _packet_length + 5;
#if SERIAL_DEBUG
Serial.println("next(): Set _read_pos");
#endif
}
if (_read_pos >= _max_length)
_read_pos -= _max_length;
#if SERIAL_DEBUG
Serial.print("Next Packet: ");
Serial.print("length: ");
Serial.print(_packet_length);
Serial.print(" --end: ");
Serial.print(_end);
Serial.print(" -- write: ");
Serial.print(_write_pos);
Serial.print(" -- read: ");
Serial.println(_read_pos);
#endif
}
uint8_t BayEOSBuffer::addPacket(const uint8_t *payload, uint8_t length) {
uint8_t move_read_pos = 0;
while (!freeSpace(length+5)) {
//Write Pointer überholt END Pointer
if (_end == _read_pos) {
_framesDiscarded = 1;
move_read_pos = 1;
#if SERIAL_DEBUG
Serial.println("Verwerfe Paket");
#endif
}
_end += initPacket(_end) + 5;
if (_end >= _max_length)
_end -= _max_length;
if (move_read_pos)
_read_pos = _end;
}
b_seek(_write_pos);
unsigned long time = getTime();
uint8_t* b = (uint8_t *) &time;
b_write(b, 4);
b_write(length);
b_write(payload, length);
_write_pos += length + 5;
if (_write_pos >= _max_length)
_write_pos -= _max_length;
#if SERIAL_DEBUG
Serial.println();
Serial.print("end: ");
Serial.print(_end);
Serial.print(" -- write: ");
Serial.print(_write_pos);
Serial.print(" -- read: ");
Serial.println(_read_pos);
Serial.println();
#endif
flush();
return length + 5;
}
uint8_t BayEOSBuffer::packetLength(void){
return _packet_length;
}
unsigned long BayEOSBuffer::packetMillis(void){
return _millis;
}
void BayEOSBuffer::setRTC(RTC& rtc,boolean absolute_time) {
_rtc = &rtc;
_absoluteTime=absolute_time;
}
uint8_t BayEOSBuffer::rtc(void){
if(_rtc!=NULL) return 1;
return 0;
}
unsigned long BayEOSBuffer::getTime(void){
if(_rtc!=NULL){
//DateTime now=_rtc->now();
return _rtc->now().get();
}
return millis();
}
unsigned long BayEOSBuffer::writePos(void){ return _write_pos; }
unsigned long BayEOSBuffer::readPos(void){ return _read_pos; }
unsigned long BayEOSBuffer::endPos(void){ return _end; }
unsigned long BayEOSBuffer::length(void){ return _max_length; }
//PROGMEM prog_uint8_t daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
static uint8_t daysInMonth[] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// number of days since 2000/01/01, valid for 2001..2099
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
if (y >= 2000)
y -= 2000;
uint16_t days = d;
for (uint8_t i = 1; i < m; ++i)
days += daysInMonth[(i - 1)];
// days += pgm_read_byte(daysInMonth + i - 1);
if (m > 2 && y % 4 == 0)
++days;
return days + 365 * y + (y + 3) / 4 - 1;
}
static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) {
return ((days * 24L + h) * 60 + m) * 60 + s;
}
////////////////////////////////////////////////////////////////////////////////
// DateTime implementation - ignores time zones and DST changes
// NOTE: also ignores leap seconds, see http://en.wikipedia.org/wiki/Leap_second
DateTime::DateTime(long t) {
ss = t % 60;
t /= 60;
mm = t % 60;
t /= 60;
hh = t % 24;
uint16_t days = t / 24;
uint8_t leap;
for (yOff = 0;; ++yOff) {
leap = yOff % 4 == 0;
if (days < (uint16_t) 365 + leap)
break;
days -= 365 + leap;
}
for (m = 1;; ++m) {
uint8_t daysPerMonth = daysInMonth[(m - 1)]; //pgm_read_byte(daysInMonth + m - 1);
if (leap && m == 2)
++daysPerMonth;
if (days < daysPerMonth)
break;
days -= daysPerMonth;
}
d = days + 1;
}
DateTime::DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
uint8_t min, uint8_t sec) {
if (year >= 2000)
year -= 2000;
yOff = year;
m = month;
d = day;
hh = hour;
mm = min;
ss = sec;
}
static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
// A convenient constructor for using "the compiler's time":
// DateTime now (__DATE__, __TIME__);
// NOTE: using PSTR would further reduce the RAM footprint
DateTime::DateTime(const char* date, const char* time) {
// sample input: date = "Dec 26 2009", time = "12:34:56"
yOff = conv2d(date + 9);
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
switch (date[0]) {
case 'J':
m = date[1] == 'a' ? 1 : m = date[2] == 'n' ? 6 : 7;
break;
case 'F':
m = 2;
break;
case 'A':
m = date[2] == 'r' ? 4 : 8;
break;
case 'M':
m = date[2] == 'r' ? 3 : 5;
break;
case 'S':
m = 9;
break;
case 'O':
m = 10;
break;
case 'N':
m = 11;
break;
case 'D':
m = 12;
break;
}
d = conv2d(date + 4);
hh = conv2d(time);
mm = conv2d(time + 3);
ss = conv2d(time + 6);
}
uint8_t DateTime::dayOfWeek() const {
uint16_t day = get() / SECONDS_PER_DAY;
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
}
long DateTime::get() const {
uint16_t days = date2days(yOff, m, d);
return time2long(days, hh, mm, ss);
}
uint16_t DateTime::year() const { return 2000 + yOff; }
uint8_t DateTime::month() const { return m; }
uint8_t DateTime::day() const { return d; }
uint8_t DateTime::hour() const { return hh; }
uint8_t DateTime::minute() const { return mm; }
uint8_t DateTime::second() const { return ss; }
////////////////////////////////////////////////////////////////////////////////
// RTC_Millis implementation
long RTC_Millis::offset = 0;
void RTC_Millis::adjust(const DateTime& dt) {
last_set=millis();
offset = dt.get();
}
void RTC_Millis::adjust(const DateTime& dt, uint16_t msec) {
last_set=millis()-msec;
offset = dt.get();
}
DateTime RTC_Millis::now() {
if((millis()-last_set)>3456000000){
adjust(now(),(millis()-last_set)%1000);
}
return offset + (millis()-last_set) / 1000;
}
unsigned long RTC_Millis::sec(uint16_t* msec) {
if(msec!=NULL){
*msec=(millis()-last_set)%1000;
}
if((millis()-last_set)>3456000000){
adjust(now(),(millis()-last_set)%1000);
}
return offset + (millis()-last_set) / 1000;
}
DateTime RTC_Millis::get(uint16_t* msec) {
if(msec!=NULL){
*msec=(millis()-last_set)%1000;
}
return now();
}