83 lines
1.7 KiB
C++
Raw Normal View History

2013-08-17 18:38:38 +02:00
//
2011-10-09 22:21:55 +02:00
// FILE: StopWatch.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.03
2011-10-09 22:21:55 +02:00
// PURPOSE: Simple StopWatch library for Arduino
//
// The library is based upon millis() and therefore
// has the same restrictions as millis() has wrt overflow.
2011-10-09 22:21:55 +02:00
//
2013-08-17 18:38:38 +02:00
// HISTORY:
2011-10-09 22:21:55 +02:00
// 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
// 0.1.03 - 2012-01-22 Added several improvements
// By mromani & Rob Tillaart
2013-08-17 18:38:38 +02:00
//
2013-08-17 18:47:21 +02:00
// Released to the public domain
2011-10-09 22:21:55 +02:00
//
#include "StopWatch.h"
StopWatch::StopWatch(enum Resolution res)
2011-10-09 22:21:55 +02:00
{
_res = res;
switch(_res) {
case MICROS:
_gettime = micros;
break;
case MILLIS:
_gettime = millis;
break;
case SECONDS:
_gettime = seconds;
break;
default:
_gettime = millis;
break;
}
reset();
2011-10-09 22:21:55 +02:00
}
void StopWatch::reset()
{
_state = StopWatch::RESET;
_starttime = _stoptime = 0;
2011-10-09 22:21:55 +02:00
}
void StopWatch::start()
{
if (_state == StopWatch::RESET || _state == StopWatch::STOPPED)
{
_state = StopWatch::RUNNING;
unsigned long t = _gettime();
_starttime += t - _stoptime;
_stoptime = t;
}
2011-10-09 22:21:55 +02:00
}
unsigned long StopWatch::value()
{
if (_state == StopWatch::RUNNING) _stoptime = _gettime();
return _stoptime - _starttime;
2011-10-09 22:21:55 +02:00
}
void StopWatch::stop()
{
if (_state == StopWatch::RUNNING)
{
_state = StopWatch::STOPPED;
_stoptime = _gettime();
}
2011-10-09 22:21:55 +02:00
}
bool StopWatch::isRunning()
{
return (_state == StopWatch::RUNNING);
2011-10-09 22:21:55 +02:00
}
enum StopWatch::State StopWatch::state()
2011-10-09 22:21:55 +02:00
{
return _state;
2011-10-09 22:21:55 +02:00
}
// END OF FILE