-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRingBuffer.cpp
More file actions
166 lines (152 loc) · 4.46 KB
/
RingBuffer.cpp
File metadata and controls
166 lines (152 loc) · 4.46 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
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
#include "GrowduinoFirmware.h"
#include "RingBuffer.h"
void buffer_cleanup(int * buffer, int buf_len, int start, int end) {
//fills parts of buffer with minvalue so we can put gaps in graph.
//int buf_len = sizeof(&buffer)/sizeof(int);
if (start != 0 || end != -1) { //Do not write to SERIAL on initial cleanup, as its not initialised yet
#ifdef DEBUG_RB_DATA
SERIAL.print(F("Cleanup:"));
SERIAL.print(start);
SERIAL.print(F(" - "));
SERIAL.print(end);
SERIAL.print(F(", size:"));
SERIAL.println(buf_len);
#endif
}
if (end > buf_len || start == end) {
return;
}
if (end == -1) {
end = buf_len;
}
if (start > end) { // when cleanup is over the end
buffer_cleanup(buffer, buf_len, start, buf_len);
start = 0;
}
for (int buf_idx = start; buf_idx < end; buf_idx ++) {
buffer[buf_idx] = MINVALUE;
}
}
int buffer_load(int * buffer, char * buf_name, aJsonObject * data) {
aJsonObject * buff;
aJsonObject * data_item;
int i, i_end;
int item;
char * dValue;
#ifdef DEBUG_RB_DATA
SERIAL.print(F("Loading buffer "));
SERIAL.println(buf_name);
#endif
buff = aJson.getObjectItem(data, buf_name);
if (!buff) {
#ifdef DEBUG_RB_DATA
SERIAL.println(F("json contains no related data"));
#endif
i_end = -1;
} else {
i_end = aJson.getArraySize(buff);
#ifdef DEBUG_RB_DATA
SERIAL.print(F("Array size: "));
SERIAL.println(i_end);
#endif
for (i = 0; i < i_end; i++) {
data_item = aJson.getArrayItem(buff, i);
dValue = aJson.print(data_item);
item = atoi(dValue);
buffer[i] = item;
free(dValue);
}
i_end--;
}
#ifdef DEBUG_RB_DATA
SERIAL.print(F("Loaded buffer "));
SERIAL.print(buf_name);
SERIAL.print(F(". Stored "));
SERIAL.print(i_end);
SERIAL.println(F(" values."));
#endif
return i_end;
}
int buffer_avg(int * buffer, int buf_len) {
int count = 0;
long sum = 0;
//int buf_len = sizeof(&buffer)/sizeof(int);
for (int i = 0; i < buf_len; i++) {
if (buffer[i] != MINVALUE) {
count++;
sum += buffer[i];
}
}
int average = sum / count;
return average;
}
void buffer_printjson(int * buffer, int buf_len, char * buf_name, int index, bool full, Stream * output) {
// dump json of buffer into stream
int buf_idx;
//int buf_len = sizeof(&buffer)/sizeof(int);
#ifdef DEBUG_RB_DATA
SERIAL.println(F("Debug: printjson"));
#endif
output->print("\"");
output->print(buf_name);
output->print("\":[");
if (full) {
for (int j = 0; j < buf_len; j++) {
if (j > 0) output->print(",");
if ((j + index + 1) < buf_len) { // put oldest sample at dynamic_buffer[0]
buf_idx = j + index + 1;
} else {
buf_idx = j + index + 1 - buf_len;
}
if ((buf_idx >= 0) && (buf_idx < buf_len)) {
output->print(buffer[buf_idx], DEC);
} else {
#ifdef DEBUG_RB_DATA
SERIAL.print(F("Err: read outside ringbuffer j:"));
SERIAL.print(j, DEC);
SERIAL.print(F(" index:"));
SERIAL.print(index, DEC);
SERIAL.print(F(" buf_len:"));
SERIAL.print(buf_len, DEC);
SERIAL.println();
#endif
}
}
} else {
for (int j = 0; j <= index; j++) { //print only values since start of buffer
if (j > 0) output->print(",");
output->print(buffer[j], DEC);
}
}
output->print("]");
#ifdef DEBUG_RB_DATA
SERIAL.println(F("Debug: printjson done"));
#endif
}
bool buffer_store(int * buffer, int buf_len, int value, int new_position, int old_position) {
// store 'value' into 'buffer' at 'old_position' position. Clean values between 'old_position' and last stored postition ('new_position')
#ifdef DEBUG_RB_DATA
SERIAL.print(F("Storing "));
SERIAL.print(value);
SERIAL.print(F(", new_position "));
SERIAL.print(new_position);
SERIAL.print(F(" with old_position "));
SERIAL.println(old_position);
#endif
//int buf_len = sizeof(&buffer)/sizeof(int);
//stay inside buffer
//this allows us to log sequence longer than buffer, for example using
//ordinary timestamp or day-seconds
while (new_position >= buf_len) {
new_position = new_position % buf_len;
}
//purge skipped values. Do not purge if we advanced by one or if we write
//to the same place again
//(needed for recalculating average for last day and month value)
if (old_position != new_position - 1 && old_position != new_position) {
buffer_cleanup(buffer, buf_len, old_position + 1, new_position);
}
buffer[new_position] = value;
old_position = new_position;
return true;
}