Skip to content

Commit b527f06

Browse files
committed
+ version 0.2.08
+ refactored getMin() and getMax() + fixed typos + added demo sketch getMin getMax
1 parent 33664e1 commit b527f06

File tree

4 files changed

+75
-36
lines changed

4 files changed

+75
-36
lines changed

libraries/RunningAverage/RunningAverage.cpp

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// FILE: RunningAverage.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.07
5-
// DATE: 2015-mar-16
4+
// VERSION: 0.2.08
5+
// DATE: 2015-apr-10
66
// PURPOSE: RunningAverage library for Arduino
77
//
88
// The library stores N individual values in a circular buffer,
@@ -19,7 +19,8 @@
1919
// 0.2.04 - 2014-07-03 added memory protection
2020
// 0.2.05 - 2014-12-16 changed float -> double
2121
// 0.2.06 - 2015-03-07 all size uint8_t
22-
// 0.2.07 - 2015-03-16 added getMin() and getMax() functions
22+
// 0.2.07 - 2015-03-16 added getMin() and getMax() functions (Eric Mulder)
23+
// 0.2.08 - 2015-04-10 refactored getMin() and getMax() implementation
2324
//
2425
// Released to the public domain
2526
//
@@ -33,8 +34,6 @@ RunningAverage::RunningAverage(uint8_t size)
3334
_ar = (double*) malloc(_size * sizeof(double));
3435
if (_ar == NULL) _size = 0;
3536
clear();
36-
_min = NAN;
37-
_max = NAN;
3837
}
3938

4039
RunningAverage::~RunningAverage()
@@ -48,7 +47,9 @@ void RunningAverage::clear()
4847
_cnt = 0;
4948
_idx = 0;
5049
_sum = 0.0;
51-
for (uint8_t i = 0; i< _size; i++)
50+
_min = NAN;
51+
_max = NAN;
52+
for (uint8_t i = 0; i < _size; i++)
5253
{
5354
_ar[i] = 0.0; // keeps addValue simple
5455
}
@@ -57,15 +58,17 @@ void RunningAverage::clear()
5758
// adds a new value to the data-set
5859
void RunningAverage::addValue(double value)
5960
{
60-
if (_ar == NULL) return;
61+
if (_ar == NULL) return; // allocation error
6162
_sum -= _ar[_idx];
6263
_ar[_idx] = value;
6364
_sum += _ar[_idx];
6465
_idx++;
6566
if (_idx == _size) _idx = 0; // faster than %
66-
if (_cnt == 0) _min = _max = f;
67-
if (f < _min) _min = f;
68-
if (f > _max) _max = f;
67+
// handle min max
68+
if (_cnt == 0) _min = _max = value;
69+
else if (value < _min) _min = value;
70+
else if (value > _max) _max = value;
71+
// update count as last otherwise if( _cnt == 0) above will fail
6972
if (_cnt < _size) _cnt++;
7073
}
7174

@@ -76,21 +79,7 @@ double RunningAverage::getAverage()
7679
return _sum / _cnt;
7780
}
7881

79-
// returns the lowest value added to the data-set
80-
double RunningAverage::getMin()
81-
{
82-
if (_cnt == 0) return NAN;
83-
return _min;
84-
}
85-
86-
// returns the highest value added to the data-set
87-
double RunningAverage::getMax())
88-
{
89-
if (_cnt == 0) return NAN;
90-
return _max;
91-
}
92-
93-
// returns the value of an element if exist, 0 otherwise
82+
// returns the value of an element if exist, NAN otherwise
9483
double RunningAverage::getElement(uint8_t idx)
9584
{
9685
if (idx >=_cnt ) return NAN;

libraries/RunningAverage/RunningAverage.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
//
22
// FILE: RunningAverage.h
33
// AUTHOR: Rob dot Tillaart at gmail dot com
4-
// VERSION: 0.2.07
5-
// DATE: 2015-mar-16
4+
// VERSION: 0.2.08
5+
// DATE: 2015-apr-10
66
// PURPOSE: RunningAverage library for Arduino
77
// URL: http://arduino.cc/playground/Main/RunningAverage
88
// HISTORY: See RunningAverage.cpp
99
//
1010
// Released to the public domain
1111
//
1212
// backwards compatibility
13-
// clr() clear()
14-
// add(x) addValue(x)
15-
// avg() getAverage()
13+
// clr() clear()
14+
// add(x) addValue(x)
15+
// avg() getAverage()
1616

1717
#ifndef RunningAverage_h
1818
#define RunningAverage_h
1919

20-
#define RUNNINGAVERAGE_LIB_VERSION "0.2.07"
20+
#define RUNNINGAVERAGE_LIB_VERSION "0.2.08"
2121

2222
#include "Arduino.h"
2323

@@ -33,8 +33,10 @@ class RunningAverage
3333
void fillValue(double, uint8_t);
3434

3535
double getAverage();
36-
double getMin();
37-
double getMax();
36+
// returns lowest value added to the data-set since last clear
37+
double getMin() { return _min; };
38+
// returns highest value added to the data-set since last clear
39+
double getMax() { return _max; };
3840

3941
double getElement(uint8_t idx);
4042
uint8_t getSize() { return _size; }
@@ -44,10 +46,10 @@ class RunningAverage
4446
uint8_t _size;
4547
uint8_t _cnt;
4648
uint8_t _idx;
47-
double _sum;
49+
double _sum;
4850
double * _ar;
49-
double _min;
50-
double _max;
51+
double _min;
52+
double _max;
5153
};
5254

5355
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// FILE: runningAverageMinMaxTest.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// DATE: 2015-apr-10
6+
//
7+
// PUPROSE: demo
8+
//
9+
10+
#include "RunningAverage.h"
11+
12+
RunningAverage myRA(10);
13+
int samples = 0;
14+
15+
void setup(void)
16+
{
17+
Serial.begin(115200);
18+
Serial.println("\nDemo runningAverageMinMaxTest");
19+
Serial.print("Version: ");
20+
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
21+
myRA.clear(); // explicitly start clean
22+
23+
Serial.println("\nCNT\tMIN\tAVG\tMAX");
24+
}
25+
26+
void loop(void)
27+
{
28+
long rn = random(0, 1000);
29+
myRA.addValue(rn * 0.001);
30+
samples++;
31+
Serial.print(samples);
32+
Serial.print("\t");
33+
Serial.print(myRA.getMin(), 3);
34+
Serial.print("\t");
35+
Serial.print(myRA.getAverage(), 3);
36+
Serial.print("\t");
37+
Serial.println(myRA.getMax(), 3);
38+
39+
if (samples == 100)
40+
{
41+
samples = 0;
42+
myRA.clear();
43+
Serial.println("\nCNT\tMIN\tAVG\tMAX");
44+
}
45+
delay(10);
46+
}

libraries/RunningAverage/keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ RunningAverage KEYWORD1
1515
clear KEYWORD2
1616
addValue KEYWORD2
1717
getAverage KEYWORD2
18+
getMin KEYWORD2
19+
getMax KEYWORD2
1820
fillValue KEYWORD2
1921
getElement KEYWORD2
2022
getSize KEYWORD2

0 commit comments

Comments
 (0)