GY-63_MS5611/libraries/StopWatch/StopWatch.cpp

67 lines
1.2 KiB
C++
Raw Normal View History

2013-08-17 12:38:38 -04:00
//
2011-10-09 16:21:55 -04:00
// FILE: StopWatch.cpp
// AUTHOR: Rob Tillaart
2013-08-17 12:38:38 -04:00
// VERSION: 0.1.02
2011-10-09 16:21:55 -04:00
// PURPOSE: Simple StopWatch library for Arduino
//
2013-08-17 12:38:38 -04:00
// The library is based upon millis() and therefor has the same restrictions as millis() has wrt overflow.
2011-10-09 16:21:55 -04:00
//
2013-08-17 12:38:38 -04:00
// HISTORY:
2011-10-09 16:21:55 -04: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
2013-08-17 12:38:38 -04:00
//
2013-08-17 12:47:21 -04:00
// Released to the public domain
2011-10-09 16:21:55 -04:00
//
#include "StopWatch.h"
2013-08-17 12:38:38 -04:00
#include "wiring.h"
2011-10-09 16:21:55 -04:00
2013-08-17 12:38:38 -04:00
StopWatch::StopWatch()
2011-10-09 16:21:55 -04:00
{
2013-08-17 12:38:38 -04:00
reset();
2011-10-09 16:21:55 -04:00
}
void StopWatch::reset()
{
2013-08-17 12:38:38 -04:00
_state = STOPWATCH_RESET;
_starttime = _stoptime = 0;
2011-10-09 16:21:55 -04:00
}
void StopWatch::start()
{
2013-08-17 12:38:38 -04:00
if (STOPWATCH_RESET == _state || STOPWATCH_STOPPED == _state)
{
_state = STOPWATCH_RUNNING;
unsigned long t = millis();
_starttime += t - _stoptime;
_stoptime = t;
}
2011-10-09 16:21:55 -04:00
}
unsigned long StopWatch::value()
{
2013-08-17 12:38:38 -04:00
if (STOPWATCH_RUNNING == _state) _stoptime = millis();
return _stoptime - _starttime;
2011-10-09 16:21:55 -04:00
}
void StopWatch::stop()
{
2013-08-17 12:38:38 -04:00
if (STOPWATCH_RUNNING == _state)
{
_state = STOPWATCH_STOPPED;
_stoptime = millis();
}
2011-10-09 16:21:55 -04:00
}
bool StopWatch::isRunning()
{
2013-08-17 12:38:38 -04:00
return (STOPWATCH_RUNNING == _state);
2011-10-09 16:21:55 -04:00
}
2013-08-17 12:38:38 -04:00
int StopWatch::state()
2011-10-09 16:21:55 -04:00
{
2013-08-17 12:38:38 -04:00
return _state;
2011-10-09 16:21:55 -04:00
}
2013-08-17 12:38:38 -04:00
2011-10-09 16:21:55 -04:00
// END OF FILE