This version uses a function pointer to either millis(), micros() or seconds() (which it defines as a class method). Decision about what function to use is made at constructor time, thus no overhead and no different codepaths at runtime when _gettime() is called.
StopWatch.h
#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"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class StopWatch
{
public:
enum State { RESET, RUNNING, STOPPED };
enum Resolution { MILLIS, MICROS, SECONDS };
StopWatch(enum Resolution res = MILLIS);
void start();
void stop();
void reset();
unsigned long value(); // should this be deprecated ?
unsigned long elapsed() { return value(); };
bool isRunning();
enum State state();
enum Resolution resolution() { return _res; };
private:
enum State _state;
enum Resolution _res;
unsigned long _starttime;
unsigned long _stoptime;
//unsigned long _gettime();
unsigned long (*_gettime)(void);
static unsigned long seconds() { return millis()/1000; };
};
#endif
// END OF FILE
StopWatch.cpp
//
// FILE: StopWatch.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// PURPOSE: Simple StopWatch library for Arduino
//
// The library is based upon millis() and therefore
// 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"
StopWatch::StopWatch(enum Resolution res)
{
_res = res;
switch(_res) {
case MICROS:
_gettime = micros;
break;
case MILLIS:
_gettime = millis;
break;
case SECONDS:
_gettime = seconds;
break;
}
reset();
}
void StopWatch::reset()
{
_state = StopWatch::RESET;
_starttime = _stoptime = 0;
}
void StopWatch::start()
{
if (_state == StopWatch::RESET || _state == StopWatch::STOPPED)
{
_state = StopWatch::RUNNING;
unsigned long t = _gettime();
_starttime += t - _stoptime;
_stoptime = t;
}
}
unsigned long StopWatch::value()
{
if (_state == StopWatch::RUNNING) _stoptime = _gettime();
return _stoptime - _starttime;
}
void StopWatch::stop()
{
if (_state == StopWatch::RUNNING)
{
_state = StopWatch::STOPPED;
_stoptime = _gettime();
}
}
bool StopWatch::isRunning()
{
return (_state == StopWatch::RUNNING);
}
enum StopWatch::State StopWatch::state()
{
return _state;
}
/*
unsigned long StopWatch::_gettime() {
switch(_res) {
case MICROS:
return micros();
case MILLIS:
return millis();
case SECONDS:
return millis()/1000;
}
}
*/
// END OF FILE