mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
Added min/max functions
Added getMin() and getMax() for a user of the library to easily keep track of the lowest and highest value that were inserted in the data-set.
This commit is contained in:
parent
503c409485
commit
ad44ae9e88
@ -1,8 +1,8 @@
|
|||||||
//
|
//
|
||||||
// FILE: RunningAverage.cpp
|
// FILE: RunningAverage.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.2.06
|
// VERSION: 0.2.07
|
||||||
// DATE: 2015-mar-07
|
// DATE: 2015-mar-16
|
||||||
// PURPOSE: RunningAverage library for Arduino
|
// PURPOSE: RunningAverage library for Arduino
|
||||||
//
|
//
|
||||||
// The library stores N individual values in a circular buffer,
|
// The library stores N individual values in a circular buffer,
|
||||||
@ -19,6 +19,7 @@
|
|||||||
// 0.2.04 - 2014-07-03 added memory protection
|
// 0.2.04 - 2014-07-03 added memory protection
|
||||||
// 0.2.05 - 2014-12-16 changed float -> double
|
// 0.2.05 - 2014-12-16 changed float -> double
|
||||||
// 0.2.06 - 2015-03-07 all size uint8_t
|
// 0.2.06 - 2015-03-07 all size uint8_t
|
||||||
|
// 0.2.07 - 2015-03-16 added getMin() and getMax() functions
|
||||||
//
|
//
|
||||||
// Released to the public domain
|
// Released to the public domain
|
||||||
//
|
//
|
||||||
@ -32,6 +33,8 @@ RunningAverage::RunningAverage(uint8_t size)
|
|||||||
_ar = (double*) malloc(_size * sizeof(double));
|
_ar = (double*) malloc(_size * sizeof(double));
|
||||||
if (_ar == NULL) _size = 0;
|
if (_ar == NULL) _size = 0;
|
||||||
clear();
|
clear();
|
||||||
|
_min = NAN;
|
||||||
|
_max = NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
RunningAverage::~RunningAverage()
|
RunningAverage::~RunningAverage()
|
||||||
@ -60,6 +63,9 @@ void RunningAverage::addValue(double value)
|
|||||||
_sum += _ar[_idx];
|
_sum += _ar[_idx];
|
||||||
_idx++;
|
_idx++;
|
||||||
if (_idx == _size) _idx = 0; // faster than %
|
if (_idx == _size) _idx = 0; // faster than %
|
||||||
|
if (f < _min) _min = f;
|
||||||
|
if (f > _max) _max = f;
|
||||||
|
if (_cnt == 0) _min = _max = f;
|
||||||
if (_cnt < _size) _cnt++;
|
if (_cnt < _size) _cnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +76,20 @@ double RunningAverage::getAverage()
|
|||||||
return _sum / _cnt;
|
return _sum / _cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns the lowest value added to the data-set
|
||||||
|
double RunningAverage::getMin()
|
||||||
|
{
|
||||||
|
if (_cnt == 0) return NAN;
|
||||||
|
return _min;
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns the highest value added to the data-set
|
||||||
|
double RunningAverage::getMax())
|
||||||
|
{
|
||||||
|
if (_cnt == 0) return NAN;
|
||||||
|
return _max;
|
||||||
|
}
|
||||||
|
|
||||||
// returns the value of an element if exist, 0 otherwise
|
// returns the value of an element if exist, 0 otherwise
|
||||||
double RunningAverage::getElement(uint8_t idx)
|
double RunningAverage::getElement(uint8_t idx)
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
//
|
//
|
||||||
// FILE: RunningAverage.h
|
// FILE: RunningAverage.h
|
||||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||||
// VERSION: 0.2.06
|
// VERSION: 0.2.07
|
||||||
// DATE: 2015-mar-07
|
// DATE: 2015-mar-16
|
||||||
// PURPOSE: RunningAverage library for Arduino
|
// PURPOSE: RunningAverage library for Arduino
|
||||||
// URL: http://arduino.cc/playground/Main/RunningAverage
|
// URL: http://arduino.cc/playground/Main/RunningAverage
|
||||||
// HISTORY: See RunningAverage.cpp
|
// HISTORY: See RunningAverage.cpp
|
||||||
@ -17,7 +17,7 @@
|
|||||||
#ifndef RunningAverage_h
|
#ifndef RunningAverage_h
|
||||||
#define RunningAverage_h
|
#define RunningAverage_h
|
||||||
|
|
||||||
#define RUNNINGAVERAGE_LIB_VERSION "0.2.06"
|
#define RUNNINGAVERAGE_LIB_VERSION "0.2.07"
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
@ -33,6 +33,8 @@ public:
|
|||||||
void fillValue(double, uint8_t);
|
void fillValue(double, uint8_t);
|
||||||
|
|
||||||
double getAverage();
|
double getAverage();
|
||||||
|
double getMin();
|
||||||
|
double getMax();
|
||||||
|
|
||||||
double getElement(uint8_t idx);
|
double getElement(uint8_t idx);
|
||||||
uint8_t getSize() { return _size; }
|
uint8_t getSize() { return _size; }
|
||||||
@ -44,6 +46,8 @@ protected:
|
|||||||
uint8_t _idx;
|
uint8_t _idx;
|
||||||
double _sum;
|
double _sum;
|
||||||
double * _ar;
|
double * _ar;
|
||||||
|
double _min;
|
||||||
|
double _max;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user