121 lines
2.6 KiB
C++
Raw Normal View History

2021-01-29 12:31:58 +01:00
//
// FILE: StopWatch.cpp
// AUTHOR: Rob Tillaart
2021-12-28 17:04:43 +01:00
// VERSION: 0.3.2
2021-01-29 12:31:58 +01:00
// DATE: 2011-01-04
// PURPOSE: Arduino Library implementing a stopwatch including seconds, milliseconds microseconds
// URL: https://github.com/RobTillaart/StopWatch_RT
//
// The library is based upon millis() and therefore
2021-12-28 17:04:43 +01:00
// has the same restrictions as millis() has with respect to overflow.
2021-01-29 12:31:58 +01:00
//
// 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
// 0.1.03 2012-01-22 Added several improvements
// By mromani & Rob Tillaart
// 0.1.4 2017-07-16 refactored
// 0.1.5 2017-09-13 removed const from functions
// 0.2.0 2020-05-07 updated metadata, removed pre 1.0 support; setResolution added, minutes added
// 0.2.1 2020-06-19 fix library.json
// 0.2.2 2020-07-14 fix #3 ESP support
2021-12-28 17:04:43 +01:00
// 0.3.0 2021-01-08 Arduino-CI + unit tests
2021-01-29 12:31:58 +01:00
// 0.3.1 2021-01-25 support Printable interface
2021-12-28 17:04:43 +01:00
// 0.3.2 2021-12-28 update library.json, readme, license, minor edits
2021-01-29 12:31:58 +01:00
#include "StopWatch.h"
2021-12-28 17:04:43 +01:00
StopWatch::StopWatch(const enum Resolution resolution)
2021-01-29 12:31:58 +01:00
{
2021-12-28 17:04:43 +01:00
setResolution(resolution);
2021-01-29 12:31:58 +01:00
}
2021-12-28 17:04:43 +01:00
void StopWatch::setResolution(const enum Resolution resolution)
2021-01-29 12:31:58 +01:00
{
reset();
2021-12-28 17:04:43 +01:00
_resolution = resolution;
switch(_resolution)
2021-01-29 12:31:58 +01:00
{
case MICROS:
_gettime = _micros;
break;
case SECONDS:
_gettime = seconds;
break;
case MINUTES:
_gettime = minutes;
break;
case MILLIS:
default:
_gettime = _millis;
2021-12-28 17:04:43 +01:00
_resolution = MILLIS; // for default!
2021-01-29 12:31:58 +01:00
break;
}
}
void StopWatch::reset()
{
2021-12-28 17:04:43 +01:00
_state = StopWatch::RESET;
_starttime = _stoptime = 0;
2021-01-29 12:31:58 +01:00
}
void StopWatch::start()
{
if (_state == StopWatch::RESET || _state == StopWatch::STOPPED)
{
_state = StopWatch::RUNNING;
uint32_t t = _gettime();
_starttime += t - _stoptime;
_stoptime = t;
}
}
// made const as printTo expect a const object.
uint32_t StopWatch::elapsed() const
{
if (_state == StopWatch::RUNNING)
{
return _gettime() - _starttime;
}
return _stoptime - _starttime;
}
void StopWatch::stop()
{
if (_state == StopWatch::RUNNING)
{
_stoptime = _gettime();
_state = StopWatch::STOPPED;
}
}
// PRINTABLE
size_t StopWatch::printTo(Print& p) const
{
uint32_t ti = StopWatch::elapsed();
size_t n = p.print(ti);
2021-12-28 17:04:43 +01:00
switch(_resolution)
2021-01-29 12:31:58 +01:00
{
case MICROS: n += p.print(" us"); break;
case SECONDS: n += p.print(" s"); break;
case MINUTES: n += p.print(" m."); break;
case MILLIS: n += p.print(" ms"); break;
}
return n;
}
2021-12-28 17:04:43 +01:00
2021-01-29 12:31:58 +01:00
// -- END OF FILE --
2021-12-28 17:04:43 +01:00