esp8266_web_settings beta
Public Types | Public Member Functions | Static Public Attributes | List of all members
grmcdorman::device::Accumulator< T, N, unset, zero > Class Template Reference

A class to handle accumulating values. More...

#include <grmcdorman/device/Accumulator.h>

Public Types

typedef T value_type
 The value type.
 

Public Member Functions

get_last_reading () const
 Get the current value. More...
 
float get_current_average () const
 Get the current rolling average. More...
 
void new_reading (T new_value)
 Record a new reading. More...
 
bool has_accumulation () const
 Return a value indicate whether data has been accumulated. More...
 
uint8_t get_sample_count () const
 Get the number of samples used for the average. More...
 
uint32_t get_last_sample_age () const
 Get the last sample age, in milliseconds. More...
 
DynamicJsonDocument as_json () const
 Get the values in standard JSON. More...
 

Static Public Attributes

static constexpr T unset_value = unset
 The unset value.
 
static constexpr T zero_value = zero
 The zero value.
 
static constexpr uint8_t average_points = N
 The number of readings for the rolling average.
 

Detailed Description

template<typename T, uint8_t N, int unset = 0, int zero = 0>
class grmcdorman::device::Accumulator< T, N, unset, zero >

A class to handle accumulating values.

This provides tracking for a reading, giving both the most recent reading and the current running average.

The number of readings required for a running average is configurable, and is not time-based. It defaults to 10 readings.

The intent is that readings are accumulated until a reading is published, at which time reset is called to begin accumulating anew.

Template Parameters
TThe type being accumulated; must be an arithmetic type, e.g. int32_t or float.
unsetThe unset value. Defaults to T(), which is likely to be zero.
zeroThe zero value; defaults to 0.

Member Function Documentation

◆ as_json()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
DynamicJsonDocument grmcdorman::device::Accumulator< T, N, unset, zero >::as_json ( ) const
inline

Get the values in standard JSON.

Returns
DynamicJsonDocument containing values.

◆ get_current_average()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
float grmcdorman::device::Accumulator< T, N, unset, zero >::get_current_average ( ) const
inline

Get the current rolling average.

This is the rolling average for the last N readings; if fewer than N have been accumulated, it is the average of those.

Returns
Current average.

◆ get_last_reading()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
T grmcdorman::device::Accumulator< T, N, unset, zero >::get_last_reading ( ) const
inline

Get the current value.

This is the last value supplied to new_reading. If a value has never been read, the returned value will be unset_value.

Returns
Last reading

◆ get_last_sample_age()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
uint32_t grmcdorman::device::Accumulator< T, N, unset, zero >::get_last_sample_age ( ) const
inline

Get the last sample age, in milliseconds.

Returns
Last sample age.

◆ get_sample_count()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
uint8_t grmcdorman::device::Accumulator< T, N, unset, zero >::get_sample_count ( ) const
inline

Get the number of samples used for the average.

At first this will be less than the configured maximum until sufficient samples have been collected.

Returns
Sample count.

◆ has_accumulation()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
bool grmcdorman::device::Accumulator< T, N, unset, zero >::has_accumulation ( ) const
inline

Return a value indicate whether data has been accumulated.

Returns
true At least one reading has been accumulated.
false No readings have been accumulated.

◆ new_reading()

template<typename T , uint8_t N, int unset = 0, int zero = 0>
void grmcdorman::device::Accumulator< T, N, unset, zero >::new_reading ( new_value)
inline

Record a new reading.

The last_reading is set to this value, and the reading is accumulated in the rolling average set.

Parameters
new_valueNew reading.

The documentation for this class was generated from the following file: