Add Stopwatch::resume method

This commit is contained in:
Scott Lahteine 2018-04-21 19:06:05 -05:00
parent a90cbc6339
commit 2f4b4d6076
2 changed files with 26 additions and 19 deletions

View file

@ -71,6 +71,15 @@ bool Stopwatch::start() {
else return false; else return false;
} }
void Stopwatch::resume(const millis_t duration) {
#if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("resume"));
#endif
reset();
if ((accumulator = duration)) state = RUNNING;
}
void Stopwatch::reset() { void Stopwatch::reset() {
#if ENABLED(DEBUG_STOPWATCH) #if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("reset")); Stopwatch::debug(PSTR("reset"));
@ -82,16 +91,8 @@ void Stopwatch::reset() {
accumulator = 0; accumulator = 0;
} }
bool Stopwatch::isRunning() {
return (state == RUNNING) ? true : false;
}
bool Stopwatch::isPaused() {
return (state == PAUSED) ? true : false;
}
millis_t Stopwatch::duration() { millis_t Stopwatch::duration() {
return (((isRunning()) ? millis() : stopTimestamp) return ((isRunning() ? millis() : stopTimestamp)
- startTimestamp) / 1000UL + accumulator; - startTimestamp) / 1000UL + accumulator;
} }

View file

@ -54,10 +54,10 @@ class Stopwatch {
FORCE_INLINE static void init() { reset(); } FORCE_INLINE static void init() { reset(); }
/** /**
* @brief Stops the stopwatch * @brief Stop the stopwatch
* @details Stops the running timer, it will silently ignore the request if * @details Stop the running timer, it will silently ignore the request if
* no timer is currently running. * no timer is currently running.
* @return true is method was successful * @return true on success
*/ */
static bool stop(); static bool stop();
@ -65,7 +65,7 @@ class Stopwatch {
* @brief Pause the stopwatch * @brief Pause the stopwatch
* @details Pause the running timer, it will silently ignore the request if * @details Pause the running timer, it will silently ignore the request if
* no timer is currently running. * no timer is currently running.
* @return true is method was successful * @return true on success
*/ */
static bool pause(); static bool pause();
@ -73,10 +73,16 @@ class Stopwatch {
* @brief Start the stopwatch * @brief Start the stopwatch
* @details Start the timer, it will silently ignore the request if the * @details Start the timer, it will silently ignore the request if the
* timer is already running. * timer is already running.
* @return true is method was successful * @return true on success
*/ */
static bool start(); static bool start();
/**
* @brief Resume the stopwatch
* @details Resume a timer from a given duration
*/
static void resume(const millis_t duration);
/** /**
* @brief Reset the stopwatch * @brief Reset the stopwatch
* @details Reset all settings to their default values. * @details Reset all settings to their default values.
@ -88,14 +94,14 @@ class Stopwatch {
* @details Return true if the timer is currently running, false otherwise. * @details Return true if the timer is currently running, false otherwise.
* @return true if stopwatch is running * @return true if stopwatch is running
*/ */
static bool isRunning(); FORCE_INLINE static bool isRunning() { return state == RUNNING; }
/** /**
* @brief Check if the timer is paused * @brief Check if the timer is paused
* @details Return true if the timer is currently paused, false otherwise. * @details Return true if the timer is currently paused, false otherwise.
* @return true if stopwatch is paused * @return true if stopwatch is paused
*/ */
static bool isPaused(); FORCE_INLINE static bool isPaused() { return state == PAUSED; }
/** /**
* @brief Get the running time * @brief Get the running time