added some more libs

This commit is contained in:
Rob Tillaart 2011-10-09 22:16:48 +02:00
parent 143a2fa95e
commit eb297bc993
10 changed files with 443 additions and 0 deletions

View File

@ -0,0 +1,57 @@
//
// FILE: RunningAverage.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: RunningAverage library for Arduino
//
// The library does store the last N individual values, to
// calculate the running average.
//
// HISTORY:
// 0.1.00 - 2011-01-30 initial version
// 0.1.01 - 2011-02-28 fixed missing destructor in .h
//
// Released to the public domain
//
#include "RunningAverage.h"
#include <stdlib.h>
RunningAverage::RunningAverage(int n)
{
_size = n;
_ar = (float*) malloc(_size * sizeof(float));
clr();
}
RunningAverage::~RunningAverage()
{
free(_ar);
}
// resets all counters
void RunningAverage::clr()
{
_cnt = 0;
_idx = 0;
_sum = 0.0;
for (int i=0; i< _size; i++) _ar[i] = 0.0;
}
// adds a new value to the data-set
void RunningAverage::add(float f)
{
_sum -= _ar[_idx];
_ar[_idx] = f;
_sum += _ar[_idx];
_idx = (_idx + 1) % _size;
if (_cnt < _size) _cnt++;
}
// returns the average of the data-set added sofar
float RunningAverage::avg()
{
return _sum / _cnt;
}
// END OF FILE

View File

@ -0,0 +1,33 @@
#ifndef RunningAverage_h
#define RunningAverage_h
//
// FILE: RunningAverage.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// PURPOSE: RunningAverage library for Arduino
// VERSION: 0.1.01
// URL: http://arduino.cc/playground/Main/RunningAverage
// HISTORY: See RunningAverage.cpp
//
// Released to the public domain
//
class RunningAverage
{
public:
RunningAverage(void);
RunningAverage(int);
~RunningAverage();
void clr();
void add(float);
float avg();
protected:
int _size;
int _cnt;
int _idx;
float _sum;
float * _ar;
};
#endif
// END OF FILE

View File

@ -0,0 +1,28 @@
#include "RunningAverage.h"
RunningAverage raMinute(60);
RunningAverage raHour(60);
int samples = 0;
void setup(void)
{
Serial.begin(115200);
Serial.println("Demo RunningAverage lib - average per minute & hour");
raHour.clr();
raMinute.clr();
}
void loop(void)
{
long rn = random(0, 100);
raMinute.add(rn);
samples++;
if (samples % 60 == 0) raHour.add(raMinute.avg());
Serial.print(" raMinute: ");
Serial.print(raMinute.avg(), 4);
Serial.print(" raHour: ");
Serial.println(raHour.avg(), 4);
}

View File

@ -0,0 +1,27 @@
#include "RunningAverage.h"
RunningAverage myRA(10); // use default size
int samples = 0;
void setup(void)
{
Serial.begin(115200);
Serial.println("Demo RunningAverage lib");
myRA.clr(); // explicitly start clean
}
void loop(void)
{
long rn = random(0, 100);
myRA.add(rn/100.0);
samples++;
Serial.print("Running Average: ");
Serial.println(myRA.avg(), 4);
if (samples == 300)
{
samples = 0;
myRA.clr();
}
delay(100);
}

View File

@ -0,0 +1,68 @@
//
// FILE: RunningMedian.cpp
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1
// PURPOSE: RunningMedian library for Arduino
//
// HISTORY:
// 0.1.00 - 2011-02-16 initial version
// 0.1.01 - 2011-02-22 added remarks from CodingBadly
//
// Released to the public domain
//
#include "RunningMedian.h"
RunningMedian::RunningMedian()
{
// size hardcoded
clear();
}
// resets all counters
void RunningMedian::clear()
{
_cnt = 0;
_idx = 0;
}
// adds a new value to the data-set
// or overwrites the oldest if full.
void RunningMedian::add(long value)
{
_ar[_idx++] = value;
if ( _idx >= MEDIAN_SIZE ) _idx = 0; // wrap around
if (_cnt < MEDIAN_SIZE) _cnt++;
return;
}
long RunningMedian::getMedian()
{
sort();
return _as[_cnt/2];
}
void RunningMedian::sort()
{
// copy
for (uint8_t i=0; i< _cnt; i++) _as[i] = _ar[i];
// sort all
for (uint8_t i=0; i< _cnt-1; i++)
{
uint8_t m = i;
for (uint8_t j=i+1; j< _cnt; j++)
{
if (_as[j] < _as[m]) m = j;
}
if (m != i)
{
long t = _as[m];
_as[m] = _as[i];
_as[i] = t;
}
}
}
// END OF FILE

View File

@ -0,0 +1,37 @@
#ifndef RunningMedian_h
#define RunningMedian_h
//
// FILE: RunningMedian.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// PURPOSE: RunningMedian library for Arduino
// VERSION: 0.1.01
// URL: http://arduino.cc/playground/Main/RunningMedian
// HISTORY: See RunningMedian.cpp
//
// Released to the public domain
//
#include <inttypes.h>
#define RUNNINGMEDIANVERSION "1.0"
// should at least be 5 and odd.
#define MEDIAN_SIZE 5
class RunningMedian
{
public:
RunningMedian();
void clear();
void add(long);
long getMedian();
protected:
uint8_t _cnt;
uint8_t _idx;
long _ar[MEDIAN_SIZE];
long _as[MEDIAN_SIZE];
void sort();
};
#endif
// END OF FILE

View File

@ -0,0 +1,67 @@
//
// FILE: StopWatch.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// PURPOSE: Simple StopWatch library for Arduino
//
// The library is based upon millis() and therefor has the same restrictions as millis() has wrt overflow.
//
// HISTORY:
// 0.1.00 - 2011-01-04 initial version
// 0.1.01 - 2011-01-04 Added better state
// 0.1.02 - 2011-06-15 Added state() + #defines + lib version
//
// Released to the public domain
//
#include "StopWatch.h"
#include "wiring.h"
StopWatch::StopWatch()
{
reset();
}
void StopWatch::reset()
{
_state = STOPWATCH_RESET;
_starttime = _stoptime = 0;
}
void StopWatch::start()
{
if (STOPWATCH_RESET == _state || STOPWATCH_STOPPED == _state)
{
_state = STOPWATCH_RUNNING;
unsigned long t = millis();
_starttime += t - _stoptime;
_stoptime = t;
}
}
unsigned long StopWatch::value()
{
if (STOPWATCH_RUNNING == _state) _stoptime = millis();
return _stoptime - _starttime;
}
void StopWatch::stop()
{
if (STOPWATCH_RUNNING == _state)
{
_state = STOPWATCH_STOPPED;
_stoptime = millis();
}
}
bool StopWatch::isRunning()
{
return (STOPWATCH_RUNNING == _state);
}
int StopWatch::state()
{
return _state;
}
// END OF FILE

View File

@ -0,0 +1,38 @@
#ifndef StopWatch_h
#define StopWatch_h
//
// FILE: StopWatch.h
// AUTHOR: Rob Tillaart
// PURPOSE: Simple StopWatch library for Arduino
// HISTORY: See StopWatch.cpp
// URL: http://www.arduino.cc/playground/Code/StopWatchClass
//
// Released to the public domain
//
#define STOPWATCH_LIB_VERSION "0.1.02"
#define STOPWATCH_RESET 0
#define STOPWATCH_RUNNING 1
#define STOPWATCH_STOPPED 2
class StopWatch
{
public:
StopWatch();
void start();
void stop();
void reset();
unsigned long value();
bool isRunning();
int state();
private:
int _state;
unsigned long _starttime;
unsigned long _stoptime;
};
#endif
// END OF FILE

View File

@ -0,0 +1,79 @@
#include <StopWatch.h>
StopWatch MySW;
StopWatch SWarray[5];
void setup()
{
Serial.begin(115200);
Serial.println("Stopwatch demo");
Serial.print("Version: ");
Serial.println(STOPWATCH_LIB_VERSION);
SWarray[0].start();
}
void loop()
{
Serial.println(MySW.isRunning());
delay(100);
MySW.start();
Serial.println(MySW.isRunning());
Serial.println("START 1");
for(int i=0; i<5; i++)
{
delay(10);
Serial.println(MySW.value());
}
MySW.stop();
Serial.println(MySW.isRunning());
Serial.println("STOP");
for(int i=0; i<5; i++)
{
delay(10);
Serial.println(MySW.value());
}
MySW.start();
Serial.println(MySW.isRunning());
Serial.println("START 2");
for(int i=0; i<5; i++)
{
delay(10);
Serial.println(MySW.value());
}
MySW.reset();
Serial.println(MySW.isRunning());
Serial.println("RESET");
MySW.start();
Serial.println(MySW.isRunning());
Serial.println("START 3");
for(int i=0; i<5; i++)
{
delay(10);
Serial.println(MySW.value());
}
switch(MySW.state())
{
case STOPWATCH_RESET:
Serial.println("reset"); // e.g. disable stop/reset
break;
case STOPWATCH_RUNNING:
Serial.println("running"); // display laptime
break;
case STOPWATCH_STOPPED:
Serial.println("stopped"); // display finaltime
break;
default:
Serial.println("unknown");
break;
}
delay(3000);
Serial.print(" >>> laptime loop() : ");
Serial.println(SWarray[0].value());
}

View File

@ -0,0 +1,9 @@
TODO
=====================
- base upon micro's ?
- add examples
-