Use static classes for job timers (#9938)

This commit is contained in:
Scott Lahteine
2018-03-04 21:23:43 -06:00
committed by GitHub
parent 930720bbbb
commit 36262a0479
5 changed files with 171 additions and 166 deletions

View File

@ -20,21 +20,23 @@
*
*/
#include "../Marlin.h"
#include "stopwatch.h"
Stopwatch::Stopwatch() {
this->reset();
}
#include "../inc/MarlinConfig.h"
Stopwatch::State Stopwatch::state;
millis_t Stopwatch::accumulator;
millis_t Stopwatch::startTimestamp;
millis_t Stopwatch::stopTimestamp;
bool Stopwatch::stop() {
#if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("stop"));
#endif
if (this->isRunning() || this->isPaused()) {
this->state = STOPPED;
this->stopTimestamp = millis();
if (isRunning() || isPaused()) {
state = STOPPED;
stopTimestamp = millis();
return true;
}
else return false;
@ -45,9 +47,9 @@ bool Stopwatch::pause() {
Stopwatch::debug(PSTR("pause"));
#endif
if (this->isRunning()) {
this->state = PAUSED;
this->stopTimestamp = millis();
if (isRunning()) {
state = PAUSED;
stopTimestamp = millis();
return true;
}
else return false;
@ -58,12 +60,12 @@ bool Stopwatch::start() {
Stopwatch::debug(PSTR("start"));
#endif
if (!this->isRunning()) {
if (this->isPaused()) this->accumulator = this->duration();
else this->reset();
if (!isRunning()) {
if (isPaused()) accumulator = duration();
else reset();
this->state = RUNNING;
this->startTimestamp = millis();
state = RUNNING;
startTimestamp = millis();
return true;
}
else return false;
@ -74,23 +76,23 @@ void Stopwatch::reset() {
Stopwatch::debug(PSTR("reset"));
#endif
this->state = STOPPED;
this->startTimestamp = 0;
this->stopTimestamp = 0;
this->accumulator = 0;
state = STOPPED;
startTimestamp = 0;
stopTimestamp = 0;
accumulator = 0;
}
bool Stopwatch::isRunning() {
return (this->state == RUNNING) ? true : false;
return (state == RUNNING) ? true : false;
}
bool Stopwatch::isPaused() {
return (this->state == PAUSED) ? true : false;
return (state == PAUSED) ? true : false;
}
millis_t Stopwatch::duration() {
return (((this->isRunning()) ? millis() : this->stopTimestamp)
- this->startTimestamp) / 1000UL + this->accumulator;
return (((isRunning()) ? millis() : stopTimestamp)
- startTimestamp) / 1000UL + accumulator;
}
#if ENABLED(DEBUG_STOPWATCH)

View File

@ -23,11 +23,12 @@
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include "../core/types.h"
// Print debug messages with M111 S2 (Uses 156 bytes of PROGMEM)
//#define DEBUG_STOPWATCH
#include "../core/macros.h"
#include "../core/types.h"
/**
* @brief Stopwatch class
* @details This class acts as a timer proving stopwatch functionality including
@ -41,21 +42,16 @@ class Stopwatch {
PAUSED
};
Stopwatch::State state;
millis_t accumulator;
millis_t startTimestamp;
millis_t stopTimestamp;
static Stopwatch::State state;
static millis_t accumulator;
static millis_t startTimestamp;
static millis_t stopTimestamp;
public:
/**
* @brief Class constructor
*/
Stopwatch();
/**
* @brief Initialize the stopwatch
*/
inline void init() {}
FORCE_INLINE static void init() { reset(); }
/**
* @brief Stops the stopwatch
@ -63,56 +59,56 @@ class Stopwatch {
* no timer is currently running.
* @return true is method was successful
*/
bool stop();
static bool stop();
/**
* @brief Pause the stopwatch
* @details Pauses 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.
* @return true is method was successful
*/
bool pause();
static bool pause();
/**
* @brief Starts the stopwatch
* @details Starts the timer, it will silently ignore the request if the
* @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
*/
bool start();
static bool start();
/**
* @brief Resets the stopwatch
* @details Resets all settings to their default values.
* @brief Reset the stopwatch
* @details Reset all settings to their default values.
*/
void reset();
static void reset();
/**
* @brief Checks if the timer is running
* @details Returns true if the timer is currently running, false otherwise.
* @brief Check if the timer is running
* @details Return true if the timer is currently running, false otherwise.
* @return true if stopwatch is running
*/
bool isRunning();
static bool isRunning();
/**
* @brief Checks if the timer is paused
* @details Returns true if the timer is currently paused, false otherwise.
* @brief Check if the timer is paused
* @details Return true if the timer is currently paused, false otherwise.
* @return true if stopwatch is paused
*/
bool isPaused();
static bool isPaused();
/**
* @brief Gets the running time
* @details Returns the total number of seconds the timer has been running.
* @brief Get the running time
* @details Return the total number of seconds the timer has been running.
* @return the delta since starting the stopwatch
*/
millis_t duration();
static millis_t duration();
#ifdef DEBUG_STOPWATCH
/**
* @brief Prints a debug message
* @details Prints a simple debug message "Stopwatch::function"
* @brief Print a debug message
* @details Print a simple debug message "Stopwatch::function"
*/
static void debug(const char func[]);