Use static classes for job timers (#9940)

This commit is contained in:
Scott Lahteine 2018-03-04 22:52:25 -06:00 committed by GitHub
parent bc08ce86be
commit f0d8d76f68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 193 additions and 173 deletions

View file

@ -20,21 +20,23 @@
*
*/
#include "Marlin.h"
#include "stopwatch.h"
Stopwatch::Stopwatch() {
this->reset();
}
#include "Marlin.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)