-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADCLogger.cpp
More file actions
100 lines (82 loc) · 2.29 KB
/
ADCLogger.cpp
File metadata and controls
100 lines (82 loc) · 2.29 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
#include "ADCLogger.h"
#include <sstream>
#include <iomanip>
ADCLogger::ADCLogger() {
bufferSize = 100; // TBD make this configurable ?
sampleInterval = 60;
units = "Min";
sampleNext = sampleInterval * 60; // seconds before next sample
buffer.resize(bufferSize);
head = 0;
count = 0;
}
void ADCLogger::clear() {
head = 0;
count = 0;
std::fill(buffer.begin(), buffer.end(), 0);
}
void ADCLogger::writeSample(int value) {
buffer[head] = value;
head = (head + 1) % bufferSize;
if (count < bufferSize) {
++count;
}
}
std::vector<int> ADCLogger::readSamples() const {
std::vector<int> samples;
samples.reserve(count);
size_t start = (head + bufferSize - count) % bufferSize;
for (size_t i = 0; i < count; ++i) {
samples.push_back(buffer[(start + i) % bufferSize]);
}
return samples;
}
uint16_t ADCLogger::toJSON(char* buf, uint16_t sz) const {
auto samples = readSamples();
/*
std::ostringstream json;
json << "{";
json << "\"DataInt\":" << sampleInterval << ",";
json << "\"TimeToNext\":" << sampleNext << ",";
json << "\"Values\":[";
for (size_t i = 0; i < samples.size(); ++i) {
json << samples[i];
if (i < samples.size() - 1)
json << ",";
}
json << "]";
json << "}";
return json.str();
*/
int len = snprintf(buf, sz, "{\"DataInt\":%d,\"TimeToNext\":%d,\"Values\":[", sampleInterval, sampleNext);
for (size_t i = 0; i < samples.size() && len < sz - 8; ++i) {
int written = snprintf(buf + len, sz - len, "%d%s", samples[i], (i < samples.size() - 1) ? "," : "");
len += written;
}
len += snprintf(buf + len, sz - len, "]}");
return len;
}
void ADCLogger::setSampleInterval(uint32_t value) {
sampleInterval = value;
resetTimeToNext();
}
uint32_t ADCLogger::getSampleInterval() const {
return sampleInterval;
}
void ADCLogger::resetTimeToNext() {
sampleNext = sampleInterval * 60; // seconds before next sample;
}
void ADCLogger::decTimeToNext() {
sampleNext -= 1;
}
uint32_t ADCLogger::timeToNext() const {
return sampleNext;
}
void ADCLogger::setUnits(std::string str) {
}
std::string ADCLogger::getUnits() const {
return units;
}
size_t ADCLogger::getSampleCount() const {
return count;
}