-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistory.h
90 lines (81 loc) · 1.74 KB
/
History.h
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
#ifndef HISTORY_H
#define HISTORY_H
#include <String.h>
#include <ArduinoJson.h>
#include "CircularBuffer.h"
template<typename T, size_t N>
class HistoryEntry
{
public:
unsigned long timestamp;
T values[N];
};
template<typename T, size_t N, size_t size>
class History
{
public:
History(const char * names[N])
{
for (int i=0; i<N; i++)
mNames[i] = names[i];
}
void push(const HistoryEntry<T, N> & entry)
{
mBuffer.push(entry);
}
void populateJson(JsonDocument & doc, unsigned long fromTimestamp) const
{
try
{
size_t len = mBuffer.length();
for (size_t i = 0; i<len; i++)
{
const HistoryEntry<T, N> & entry = mBuffer.get(i);
if (entry.timestamp < fromTimestamp)
continue;
doc["timestamps"][i] = entry.timestamp;
for (size_t j = 0; j<N; j++)
doc[mNames[j]][i] = entry.values[j];
}
}
catch (OutOfBoundsException& e)
{
Serial.println(e.what());
}
}
void printTo(Print &stream, unsigned long fromTimestamp, char separator) const
{
stream.print("Timestamp");
for (size_t j = 0; j<N; j++)
{
stream.print(separator);
stream.print(mNames[j]);
}
stream.println();
try
{
size_t len = mBuffer.length();
for (size_t i = 0; i<len; i++)
{
const HistoryEntry<T, N> & entry = mBuffer.get(i);
if (entry.timestamp < fromTimestamp)
continue;
stream.print(entry.timestamp);
for (size_t j = 0; j<N; j++)
{
stream.print(separator);
stream.print(entry.values[j]);
}
stream.println();
}
}
catch (OutOfBoundsException& e)
{
Serial.println(e.what());
}
}
private:
CircularBuffer<HistoryEntry<T, N>, size> mBuffer;
const char * mNames[N];
};
#endif /* HISTORY_H */