Skip to content

Commit 9cbe3bf

Browse files
committed
+ update version 0.2.04
+ added getElement() (not released 0.2.03 version) + added getSize() (not released 0.2.03 version) + added getCount() (not released 0.2.03 version) + updated keywords.txt + added memory protection if there is not enough memory to allocate. + added return NAN instead of 0 for getAverage() + reduced _size, _cnt, _idx to uint8_t + changed extensions of example code to .ino
1 parent 9c13789 commit 9cbe3bf

File tree

8 files changed

+143
-99
lines changed

8 files changed

+143
-99
lines changed
Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
//
1+
//
22
// FILE: RunningAverage.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.02
4+
// VERSION: 0.2.04
55
// PURPOSE: RunningAverage library for Arduino
66
//
7-
// The library stores the last N individual values in a circular buffer,
8-
// to calculate the running average.
7+
// The library stores the last N individual values in a circular buffer,
8+
// to calculate the running average.
99
//
10-
// HISTORY:
10+
// HISTORY:
1111
// 0.1.00 - 2011-01-30 initial version
1212
// 0.1.01 - 2011-02-28 fixed missing destructor in .h
1313
// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web)
1414
// http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp
1515
// 0.2.01 - 2012-11-21 refactored
1616
// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue
17+
// 0.2.03 - 2013-11-31 getElement
18+
// 0.2.04 - 2014-07-03 added memory protection
1719
//
1820
// Released to the public domain
1921
//
@@ -23,52 +25,61 @@
2325

2426
RunningAverage::RunningAverage(int n)
2527
{
26-
_size = n;
27-
_ar = (float*) malloc(_size * sizeof(float));
28-
clear();
28+
_size = n;
29+
_ar = (float*) malloc(_size * sizeof(float));
30+
if (_ar == NULL) _size = 0;
31+
clear();
2932
}
3033

3134
RunningAverage::~RunningAverage()
3235
{
33-
free(_ar);
36+
if (_ar != NULL) free(_ar);
3437
}
3538

3639
// resets all counters
37-
void RunningAverage::clear()
38-
{
39-
_cnt = 0;
40-
_idx = 0;
41-
_sum = 0.0;
42-
for (int i = 0; i< _size; i++) _ar[i] = 0.0; // needed to keep addValue simple
40+
void RunningAverage::clear()
41+
{
42+
_cnt = 0;
43+
_idx = 0;
44+
_sum = 0.0;
45+
for (int i = 0; i< _size; i++) _ar[i] = 0.0; // needed to keep addValue simple
4346
}
4447

4548
// adds a new value to the data-set
4649
void RunningAverage::addValue(float f)
4750
{
48-
_sum -= _ar[_idx];
49-
_ar[_idx] = f;
50-
_sum += _ar[_idx];
51-
_idx++;
52-
if (_idx == _size) _idx = 0; // faster than %
53-
if (_cnt < _size) _cnt++;
51+
if (_ar == NULL) return;
52+
_sum -= _ar[_idx];
53+
_ar[_idx] = f;
54+
_sum += _ar[_idx];
55+
_idx++;
56+
if (_idx == _size) _idx = 0; // faster than %
57+
if (_cnt < _size) _cnt++;
5458
}
5559

5660
// returns the average of the data-set added sofar
5761
float RunningAverage::getAverage()
5862
{
59-
if (_cnt == 0) return 0; // NaN ? math.h
60-
return _sum / _cnt;
63+
if (_cnt == 0) return NAN;
64+
return _sum / _cnt;
65+
}
66+
67+
// returns the value of an element if exist, 0 otherwise
68+
float RunningAverage::getElement(uint8_t idx)
69+
{
70+
if (idx >=_cnt ) return NAN;
71+
return _ar[idx];
6172
}
6273

6374
// fill the average with a value
6475
// the param number determines how often value is added (weight)
6576
// number should preferably be between 1 and size
6677
void RunningAverage::fillValue(float value, int number)
6778
{
68-
clear();
69-
for (int i = 0; i < number; i++)
70-
{
71-
addValue(value);
72-
}
79+
clear();
80+
for (int i = 0; i < number; i++)
81+
{
82+
addValue(value);
83+
}
7384
}
7485
// END OF FILE

libraries/RunningAverage/RunningAverage.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef RunningAverage_h
22
#define RunningAverage_h
3-
//
3+
//
44
// FILE: RunningAverage.h
55
// AUTHOR: Rob dot Tillaart at gmail dot com
66
// PURPOSE: RunningAverage library for Arduino
@@ -15,25 +15,33 @@
1515
// add(x) addValue(x)
1616
// avg() getAverage()
1717

18-
#define RUNNINGAVERAGE_LIB_VERSION "0.2.02"
18+
#define RUNNINGAVERAGE_LIB_VERSION "0.2.04"
19+
20+
#include "Arduino.h"
1921

20-
class RunningAverage
22+
class RunningAverage
2123
{
22-
public:
23-
RunningAverage(void);
24-
RunningAverage(int);
25-
~RunningAverage();
26-
void clear();
27-
void addValue(float);
28-
float getAverage();
29-
void fillValue(float, int);
24+
public:
25+
RunningAverage(void);
26+
RunningAverage(int);
27+
~RunningAverage();
28+
29+
void clear();
30+
void addValue(float);
31+
void fillValue(float, int);
32+
33+
float getAverage();
34+
35+
float getElement(uint8_t idx);
36+
uint8_t getSize() { return _size; }
37+
uint8_t getCount() { return _cnt; }
3038

3139
protected:
32-
int _size;
33-
int _cnt;
34-
int _idx;
35-
float _sum;
36-
float * _ar;
40+
uint8_t _size;
41+
uint8_t _cnt;
42+
uint8_t _idx;
43+
float _sum;
44+
float * _ar;
3745
};
3846

3947
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// FILE: runningAverageHour.pde
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2012-12-30
5+
//
6+
// PUPROSE: show working of runningAverage per hour
7+
// in 2 steps - last minute + last hour
8+
// 3 or more steps also possible
9+
//
10+
11+
#include "RunningAverage.h"
12+
13+
RunningAverage raMinute(60);
14+
RunningAverage raHour(60);
15+
16+
int samples = 0;
17+
18+
void setup(void)
19+
{
20+
Serial.begin(115200);
21+
Serial.println("Demo RunningAverage lib - average per minute & hour");
22+
Serial.print("Version: ");
23+
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
24+
raHour.clear();
25+
raMinute.clear();
26+
}
27+
28+
void loop(void)
29+
{
30+
long rn = random(0, 100);
31+
raMinute.addValue(rn);
32+
samples++;
33+
34+
if (samples % 60 == 0) raHour.addValue(raMinute.getAverage());
35+
36+
Serial.print(" raMinute: ");
37+
Serial.print(raMinute.getAverage(), 4);
38+
Serial.print(" raHour: ");
39+
Serial.println(raHour.getAverage(), 4);
40+
}

libraries/RunningAverage/examples/ra_hour/ra_hour.pde

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// FILE: runningAverageTest.pde
3+
// AUTHOR: Rob Tillaart
4+
// DATE: 2012-12-30
5+
//
6+
// PUPROSE: show working of runningAverage
7+
//
8+
9+
#include "RunningAverage.h"
10+
11+
RunningAverage myRA(10);
12+
int samples = 0;
13+
14+
void setup(void)
15+
{
16+
Serial.begin(115200);
17+
Serial.println("Demo RunningAverage lib");
18+
Serial.print("Version: ");
19+
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
20+
myRA.clear(); // explicitly start clean
21+
}
22+
23+
void loop(void)
24+
{
25+
long rn = random(0, 1000);
26+
myRA.addValue(rn * 0.001);
27+
samples++;
28+
Serial.print("Running Average: ");
29+
Serial.println(myRA.getAverage(), 3);
30+
31+
if (samples == 300)
32+
{
33+
samples = 0;
34+
myRA.clear();
35+
}
36+
delay(100);
37+
}

libraries/RunningAverage/examples/ra_test/ra_test.pde

Lines changed: 0 additions & 27 deletions
This file was deleted.

libraries/RunningAverage/keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ clear KEYWORD2
1616
addValue KEYWORD2
1717
getAverage KEYWORD2
1818
fillValue KEYWORD2
19+
getElement KEYWORD2
20+
getSize KEYWORD2
21+
getCount KEYWORD2
1922

2023
#######################################
2124
# Instances (KEYWORD2)

0 commit comments

Comments
 (0)