-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_pile.cpp
More file actions
120 lines (106 loc) · 3.21 KB
/
buffer_pile.cpp
File metadata and controls
120 lines (106 loc) · 3.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
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
/************************************************
This is a library to add a buffer "pile" of data.
The aim is to allow mqtt interruption for a while
without loss of datas
P.Chaumeil 2024
************************************************/
#include "buffer_pile.h"
/****************
PUBLIC FUNCTIONS
****************/
BUFFER_PILE::BUFFER_PILE()
{
buf_CR = 0;
buf_CI = 0;
}
//add a dataset to the buffer pile
void BUFFER_PILE::add_pile(const Ecoset& mydataset)
{
#if BUFFER_DEBUG
Serial.println(F("#_# add to pile dataset"));
//debug_cpt();
#endif
ecobuffer[buf_CI] = mydataset;
inc_CI();
}
//read a dataset from the buffer pile
bool BUFFER_PILE::read_pile(Ecoset& mydataset)
{
if(buf_CR != buf_CI){
mydataset = ecobuffer[buf_CR];
#if BUFFER_DEBUG
Serial.println(F("#_# read dataset from pile done"));
#endif
return true;
}else{
return false;
}
}
//set read index to next item in buffer pile (#item treatment done)
void BUFFER_PILE::next_pile()
{
inc_CR();
#if BUFFER_DEBUG
debug_cpt();
#endif
}
//return number of available space to store datas
byte BUFFER_PILE::available_pile(){
byte avail_pos = NUM_BUFFERED;
if(buf_CI > buf_CR){
avail_pos = NUM_BUFFERED - buf_CI + buf_CR;
return avail_pos;
} else {
avail_pos = buf_CR - buf_CI - 1;
}
return avail_pos;
}
//return true if buffer is too full
byte BUFFER_PILE::alert_full_pile(){
byte nb_item = NUM_BUFFERED - available_pile();
if(nb_item > LIMIT_BUFFERED){
return true;
}
return false;
}
void BUFFER_PILE::debug_cpt(){
Serial.print(F("#_# buf_CI:"));Serial.println(buf_CI);
Serial.print(F("#_# buf_CR:"));Serial.println(buf_CR);
}
//formatting and loading date/time in stack/buffer compatible format
void BUFFER_PILE::get_formatted_datetime_bufferpile(const Ecoset& dataset, char* datetime){
const char template_datetime[] = "%02d/%02d/%02d %02d:%02d:%02d";
char buffer_time[20] = "\0";
snprintf(buffer_time, sizeof(buffer_time), template_datetime, dataset.year, dataset.month, dataset.day, dataset.hour, dataset.minute, dataset.second);
strcpy(datetime, buffer_time);
}
//calculates and returns the UTC time based on the time stored in the dataset and the timezone offset
void BUFFER_PILE::get_formatted_UTC_bufferpile(const Ecoset& dataset, char* utc_datetime){
DateTime data_date(dataset.year,dataset.month,dataset.day,dataset.hour,dataset.minute,dataset.second);
DateTime UTC_date;
int DeltaUTC = abs(dataset.UTCoffset);
if(dataset.UTCoffset < 0){
UTC_date = data_date + TimeSpan(DeltaUTC * 3600);
}else{
UTC_date = data_date - TimeSpan(DeltaUTC * 3600);
}
snprintf(utc_datetime, 20, "%04d-%02d-%02d %02d:%02d:%02d", UTC_date.year(), UTC_date.month(), UTC_date.day(), UTC_date.hour(), UTC_date.minute(), UTC_date.second());
}
/****************
PRIVATE FUNCTIONS
****************/
// buf_CI incrementation
void BUFFER_PILE::inc_CI()
{
buf_CI++;
if(buf_CI >= NUM_BUFFERED){buf_CI = 0;}
//if buffer is full then buffer index to insert has the same value of buffer index to read.
//we replace this last value and offset read index
if(buf_CI == buf_CR){inc_CR();}
}
//buf_CR incrementation
void BUFFER_PILE::inc_CR()
{
buf_CR++;
if(buf_CR >= NUM_BUFFERED){buf_CR = 0;}
}