26#include <ArduinoJson.h>
32namespace grmcdorman::device
52 template<
typename T, u
int8_t N,
int unset = 0,
int zero = 0>
86 return count == 0 ?
unset_value : std::accumulate(&last_reading_set[0], &last_reading_set[count],
zero_value) /
static_cast<float>(count);
98 last_reading = new_value;
99 last_reading_set[current_index] = new_value;
100 current_index = (current_index + 1) % N;
101 if (data_read_first < N)
105 last_sample_time = millis();
115 return data_read_first > 0;
127 return data_read_first;
137 return millis() - last_sample_time;
147 static const char average_string[] PROGMEM =
"average";
148 static const char last_string[] PROGMEM =
"last";
149 static const char sample_count_string[] PROGMEM =
"sample_count";
150 static const char sample_age_string[] PROGMEM =
"sample_age_ms";
152 DynamicJsonDocument json(128);
162 T last_reading_set[N];
163 uint32_t current_index = 0;
164 uint8_t data_read_first = 0;
165 uint32_t last_sample_time = 0;
A class to handle accumulating values.
Definition: Accumulator.h:54
void new_reading(T new_value)
Record a new reading.
Definition: Accumulator.h:96
bool has_accumulation() const
Return a value indicate whether data has been accumulated.
Definition: Accumulator.h:113
static constexpr uint8_t average_points
The number of readings for the rolling average.
Definition: Accumulator.h:58
uint8_t get_sample_count() const
Get the number of samples used for the average.
Definition: Accumulator.h:125
T get_last_reading() const
Get the current value.
Definition: Accumulator.h:70
float get_current_average() const
Get the current rolling average.
Definition: Accumulator.h:83
static constexpr T zero_value
The zero value.
Definition: Accumulator.h:57
static constexpr T unset_value
The unset value.
Definition: Accumulator.h:56
DynamicJsonDocument as_json() const
Get the values in standard JSON.
Definition: Accumulator.h:145
uint32_t get_last_sample_age() const
Get the last sample age, in milliseconds.
Definition: Accumulator.h:135
T value_type
The value type.
Definition: Accumulator.h:59