From f8fc2723da7c61728955b3147dca478620a86b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 26 Oct 2017 12:18:42 +0200 Subject: [PATCH] Dynamically allocate memory for MedianFilter --- code/espurna/config/general.h | 7 ------ code/espurna/power.h | 47 +++++++++++++++++++---------------- code/espurna/power.ino | 8 +++--- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 2cd263ef..757b8994 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -556,7 +556,6 @@ PROGMEM const char* const custom_reset_string[] = { #define POWER_VOLTAGE 230 // Default voltage #define POWER_READ_INTERVAL 6000 // Default reading interval (6 seconds) #define POWER_REPORT_INTERVAL 60000 // Default report interval (1 minute) -#define POWER_REPORT_BUFFER 12 // Default buffer size () #define POWER_CURRENT_DECIMALS 2 // Decimals for current values #define POWER_VOLTAGE_DECIMALS 0 // Decimals for voltage values #define POWER_POWER_DECIMALS 0 // Decimals for power values @@ -596,9 +595,6 @@ PROGMEM const char* const custom_reset_string[] = { #if POWER_PROVIDER == POWER_PROVIDER_V9261F - #undef POWER_REPORT_BUFFER - #define POWER_REPORT_BUFFER 60 // Override median buffer size - #ifndef V9261F_PIN #define V9261F_PIN 2 // TX pin from the V9261F #endif @@ -619,9 +615,6 @@ PROGMEM const char* const custom_reset_string[] = { #if POWER_PROVIDER == POWER_PROVIDER_ECH1560 - #undef POWER_REPORT_BUFFER - #define POWER_REPORT_BUFFER 60 // Override median buffer size - #ifndef ECH1560_CLK_PIN #define ECH1560_CLK_PIN 4 // Default CLK pin #endif diff --git a/code/espurna/power.h b/code/espurna/power.h index 2c1a786a..1284a3b2 100644 --- a/code/espurna/power.h +++ b/code/espurna/power.h @@ -8,36 +8,41 @@ class MedianFilter { public: - MedianFilter(unsigned char size) { - _size = size; - _data = new double[_size+1]; - reset(); + MedianFilter() { + _data = new std::vector(); + } + + ~MedianFilter() { + if (_data) delete _data; } virtual void reset() { - if (_pointer > 1) _data[0] = _data[_pointer-1]; - _pointer = 1; - for (unsigned char i=1; i<=_size; i++) _data[i] = 0; + double last = _data->empty() ? 0 : _data->back(); + _data->clear(); + add(last); } virtual void add(double value) { - if (_pointer <= _size) { - _data[_pointer] = value; - ++_pointer; - } + _data->push_back(value); } - virtual double average(bool do_reset = false) { + virtual double median(bool do_reset = false) { double sum = 0; - if (_pointer > 2) { + if (_data->size() == 1) { + sum = _data->back(); + + } else if (_data->size() == 2) { + sum = _data->front(); + + } else if (_data->size() > 2) { - for (unsigned char i = 1; i<_pointer-1; i++) { + for (unsigned char i = 1; i <= _data->size() - 2; i++) { - double previous = _data[i-1]; - double current = _data[i]; - double next = _data[i+1]; + double previous = _data->at(i-1); + double current = _data->at(i); + double next = _data->at(i+1); if (previous > current) std::swap(previous, current); if (current > next) std::swap(current, next); @@ -47,7 +52,7 @@ class MedianFilter { } - sum /= (_pointer - 2); + sum /= (_data->size() - 2); } @@ -58,13 +63,11 @@ class MedianFilter { } virtual unsigned char count() { - return _pointer - 1; + return _data->size(); } private: - double *_data; - unsigned char _size = 0; - unsigned char _pointer = 0; + std::vector *_data; }; diff --git a/code/espurna/power.ino b/code/espurna/power.ino index b6752ebd..1651d858 100644 --- a/code/espurna/power.ino +++ b/code/espurna/power.ino @@ -24,15 +24,15 @@ double _power_current = 0; double _power_voltage = 0; double _power_apparent = 0; double _power_energy = 0; -MedianFilter _filter_current = MedianFilter(POWER_REPORT_BUFFER); +MedianFilter _filter_current = MedianFilter(); #if POWER_HAS_ACTIVE double _power_active = 0; double _power_reactive = 0; double _power_factor = 0; - MedianFilter _filter_voltage = MedianFilter(POWER_REPORT_BUFFER); - MedianFilter _filter_active = MedianFilter(POWER_REPORT_BUFFER); - MedianFilter _filter_apparent = MedianFilter(POWER_REPORT_BUFFER); + MedianFilter _filter_voltage = MedianFilter(); + MedianFilter _filter_active = MedianFilter(); + MedianFilter _filter_apparent = MedianFilter(); #endif #if POWER_HAS_ENERGY