Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
966 B

  1. // -----------------------------------------------------------------------------
  2. // Aggregator Moving Average
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include <vector>
  6. #include "AggregatorBase.h"
  7. class AggregatorMovingAverage : public AggregatorBase {
  8. public:
  9. AggregatorMovingAverage(unsigned char size) {
  10. _size = size;
  11. for (unsigned char i=0; i<size; i++) {
  12. _data->push_back(0);
  13. }
  14. }
  15. virtual void add(double value) {
  16. _sum = _sum + value - _data->at(_pointer);
  17. _data->at(_pointer) = value;
  18. _pointer = (_pointer + 1) % _size;
  19. }
  20. virtual void reset() {
  21. // Nothing to do
  22. }
  23. virtual double result() {
  24. return _sum;
  25. }
  26. protected:
  27. unsigned char _size = 0;
  28. unsigned char _pointer = 0;
  29. double _sum = 0;
  30. };