+ version 0.2.06

+ all size vars not 8 bit
+ minor refactor
+ added sample counter to ra_test.ino (easier to compare output)
This commit is contained in:
rob tillaart 2015-03-07 13:47:56 +01:00
parent 394c4fda50
commit 48f5496fd9
3 changed files with 26 additions and 18 deletions

View File

@ -1,11 +1,11 @@
//
// FILE: RunningAverage.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.05
// DATE: 2014-dec-16
// VERSION: 0.2.06
// DATE: 2015-mar-07
// PURPOSE: RunningAverage library for Arduino
//
// The library stores the last N individual values in a circular buffer,
// The library stores N individual values in a circular buffer,
// to calculate the running average.
//
// HISTORY:
@ -18,6 +18,7 @@
// 0.2.03 - 2013-11-31 getElement
// 0.2.04 - 2014-07-03 added memory protection
// 0.2.05 - 2014-12-16 changed float -> double
// 0.2.06 - 2015-03-07 all size uint8_t
//
// Released to the public domain
//
@ -25,9 +26,9 @@
#include "RunningAverage.h"
#include <stdlib.h>
RunningAverage::RunningAverage(int n)
RunningAverage::RunningAverage(uint8_t size)
{
_size = n;
_size = size;
_ar = (double*) malloc(_size * sizeof(double));
if (_ar == NULL) _size = 0;
clear();
@ -44,15 +45,18 @@ void RunningAverage::clear()
_cnt = 0;
_idx = 0;
_sum = 0.0;
for (int i = 0; i< _size; i++) _ar[i] = 0.0; // needed to keep addValue simple
for (uint8_t i = 0; i< _size; i++)
{
_ar[i] = 0.0; // keeps addValue simple
}
}
// adds a new value to the data-set
void RunningAverage::addValue(double f)
void RunningAverage::addValue(double value)
{
if (_ar == NULL) return;
_sum -= _ar[_idx];
_ar[_idx] = f;
_ar[_idx] = value;
_sum += _ar[_idx];
_idx++;
if (_idx == _size) _idx = 0; // faster than %
@ -76,10 +80,11 @@ double RunningAverage::getElement(uint8_t idx)
// fill the average with a value
// the param number determines how often value is added (weight)
// number should preferably be between 1 and size
void RunningAverage::fillValue(double value, int number)
void RunningAverage::fillValue(double value, uint8_t number)
{
clear();
for (int i = 0; i < number; i++)
clear(); // TODO conditional? if (clr) clear();
for (uint8_t i = 0; i < number; i++)
{
addValue(value);
}

View File

@ -1,8 +1,8 @@
//
// FILE: RunningAverage.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2.05
// DATE: 2014-dec-16
// VERSION: 0.2.06
// DATE: 2015-mar-07
// PURPOSE: RunningAverage library for Arduino
// URL: http://arduino.cc/playground/Main/RunningAverage
// HISTORY: See RunningAverage.cpp
@ -17,7 +17,7 @@
#ifndef RunningAverage_h
#define RunningAverage_h
#define RUNNINGAVERAGE_LIB_VERSION "0.2.05"
#define RUNNINGAVERAGE_LIB_VERSION "0.2.06"
#include "Arduino.h"
@ -25,12 +25,12 @@ class RunningAverage
{
public:
RunningAverage(void);
RunningAverage(int);
RunningAverage(uint8_t);
~RunningAverage();
void clear();
void addValue(double);
void fillValue(double, int);
void fillValue(double, uint8_t);
double getAverage();

View File

@ -1,6 +1,7 @@
//
// FILE: runningAverageTest.pde
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// DATE: 2012-12-30
//
// PUPROSE: show working of runningAverage
@ -25,13 +26,15 @@ void loop(void)
long rn = random(0, 1000);
myRA.addValue(rn * 0.001);
samples++;
Serial.print("Running Average: ");
Serial.print(samples);
Serial.print("\t Running Average: ");
Serial.println(myRA.getAverage(), 3);
if (samples == 300)
{
samples = 0;
myRA.clear();
Serial.println();
}
delay(100);
delay(10);
}