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;
}
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() {
#if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("reset"));
@ -82,16 +91,8 @@ void Stopwatch::reset() {
accumulator = 0;
}
bool Stopwatch::isRunning() {
return (state == RUNNING) ? true : false;
}
bool Stopwatch::isPaused() {
return (state == PAUSED) ? true : false;
}
millis_t Stopwatch::duration() {
return (((isRunning()) ? millis() : stopTimestamp)
return ((isRunning() ? millis() : stopTimestamp)
- startTimestamp) / 1000UL + accumulator;
}

View file

@ -54,29 +54,35 @@ class Stopwatch {
FORCE_INLINE static void init() { reset(); }
/**
* @brief Stops the stopwatch
* @details Stops the running timer, it will silently ignore the request if
* no timer is currently running.
* @return true is method was successful
* @brief Stop the stopwatch
* @details Stop the running timer, it will silently ignore the request if
* no timer is currently running.
* @return true on success
*/
static bool stop();
/**
* @brief Pause the stopwatch
* @details Pause the running timer, it will silently ignore the request if
* no timer is currently running.
* @return true is method was successful
* no timer is currently running.
* @return true on success
*/
static bool pause();
/**
* @brief Start the stopwatch
* @details Start the timer, it will silently ignore the request if the
* timer is already running.
* @return true is method was successful
* timer is already running.
* @return true on success
*/
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
* @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.
* @return true if stopwatch is running
*/
static bool isRunning();
FORCE_INLINE static bool isRunning() { return state == RUNNING; }
/**
* @brief Check if the timer is paused
* @details Return true if the timer is currently paused, false otherwise.
* @return true if stopwatch is paused
*/
static bool isPaused();
FORCE_INLINE static bool isPaused() { return state == PAUSED; }
/**
* @brief Get the running time