1
1
//
2
2
// FILE: RunningAverage.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.2.05
5
- // DATE: 2014-dec-16
4
+ // VERSION: 0.2.06
5
+ // DATE: 2015-mar-07
6
6
// PURPOSE: RunningAverage library for Arduino
7
7
//
8
- // The library stores the last N individual values in a circular buffer,
8
+ // The library stores N individual values in a circular buffer,
9
9
// to calculate the running average.
10
10
//
11
11
// HISTORY:
18
18
// 0.2.03 - 2013-11-31 getElement
19
19
// 0.2.04 - 2014-07-03 added memory protection
20
20
// 0.2.05 - 2014-12-16 changed float -> double
21
+ // 0.2.06 - 2015-03-07 all size uint8_t
21
22
//
22
23
// Released to the public domain
23
24
//
24
25
25
26
#include " RunningAverage.h"
26
27
#include < stdlib.h>
27
28
28
- RunningAverage::RunningAverage (int n )
29
+ RunningAverage::RunningAverage (uint8_t size )
29
30
{
30
- _size = n ;
31
+ _size = size ;
31
32
_ar = (double *) malloc (_size * sizeof (double ));
32
33
if (_ar == NULL ) _size = 0 ;
33
34
clear ();
@@ -44,15 +45,18 @@ void RunningAverage::clear()
44
45
_cnt = 0 ;
45
46
_idx = 0 ;
46
47
_sum = 0.0 ;
47
- for (int i = 0 ; i< _size; i++) _ar[i] = 0.0 ; // needed to keep addValue simple
48
+ for (uint8_t i = 0 ; i< _size; i++)
49
+ {
50
+ _ar[i] = 0.0 ; // keeps addValue simple
51
+ }
48
52
}
49
53
50
54
// adds a new value to the data-set
51
- void RunningAverage::addValue (double f )
55
+ void RunningAverage::addValue (double value )
52
56
{
53
57
if (_ar == NULL ) return ;
54
58
_sum -= _ar[_idx];
55
- _ar[_idx] = f ;
59
+ _ar[_idx] = value ;
56
60
_sum += _ar[_idx];
57
61
_idx++;
58
62
if (_idx == _size) _idx = 0 ; // faster than %
@@ -76,10 +80,11 @@ double RunningAverage::getElement(uint8_t idx)
76
80
// fill the average with a value
77
81
// the param number determines how often value is added (weight)
78
82
// number should preferably be between 1 and size
79
- void RunningAverage::fillValue (double value, int number)
83
+ void RunningAverage::fillValue (double value, uint8_t number)
80
84
{
81
- clear ();
82
- for (int i = 0 ; i < number; i++)
85
+ clear (); // TODO conditional? if (clr) clear();
86
+
87
+ for (uint8_t i = 0 ; i < number; i++)
83
88
{
84
89
addValue (value);
85
90
}
0 commit comments