Merge pull request #3813 from jbrazio/bugfix/3809

Stopwatch and PrintCounter improvements
master
Scott Lahteine 9 years ago
commit f9b4b90058

@ -73,6 +73,7 @@ void PrintCounter::saveStats() {
// Refuses to save data is object is not loaded // Refuses to save data is object is not loaded
if (!this->isLoaded()) return; if (!this->isLoaded()) return;
// Saves the struct to EEPROM
eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics)); eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
} }
@ -135,35 +136,46 @@ void PrintCounter::tick() {
} }
} }
void PrintCounter::start() { // @Override
bool PrintCounter::start() {
#if ENABLED(DEBUG_PRINTCOUNTER) #if ENABLED(DEBUG_PRINTCOUNTER)
PrintCounter::debug(PSTR("start")); PrintCounter::debug(PSTR("start"));
#endif #endif
if (!this->isPaused()) this->data.totalPrints++; bool paused = this->isPaused();
super::start();
if (super::start()) {
if (!paused) {
this->data.totalPrints++;
this->lastDuration = 0;
}
return true;
}
else return false;
} }
void PrintCounter::stop() { // @Override
bool PrintCounter::stop() {
#if ENABLED(DEBUG_PRINTCOUNTER) #if ENABLED(DEBUG_PRINTCOUNTER)
PrintCounter::debug(PSTR("stop")); PrintCounter::debug(PSTR("stop"));
#endif #endif
if (!this->isRunning()) return; if (super::stop()) {
super::stop();
this->data.finishedPrints++; this->data.finishedPrints++;
this->data.printTime += this->deltaDuration(); this->data.printTime += this->deltaDuration();
this->saveStats(); this->saveStats();
}
else return false;
} }
// @Override
void PrintCounter::reset() { void PrintCounter::reset() {
#if ENABLED(DEBUG_PRINTCOUNTER) #if ENABLED(DEBUG_PRINTCOUNTER)
PrintCounter::debug(PSTR("stop")); PrintCounter::debug(PSTR("stop"));
#endif #endif
this->lastDuration = 0;
super::reset(); super::reset();
this->lastDuration = 0;
} }
#if ENABLED(DEBUG_PRINTCOUNTER) #if ENABLED(DEBUG_PRINTCOUNTER)

@ -138,8 +138,8 @@ class PrintCounter: public Stopwatch {
/** /**
* The following functions are being overridden * The following functions are being overridden
*/ */
void start(); bool start();
void stop(); bool stop();
void reset(); void reset();
#if ENABLED(DEBUG_PRINTCOUNTER) #if ENABLED(DEBUG_PRINTCOUNTER)

@ -27,40 +27,46 @@ Stopwatch::Stopwatch() {
this->reset(); this->reset();
} }
void Stopwatch::stop() { bool Stopwatch::stop() {
#if ENABLED(DEBUG_STOPWATCH) #if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("stop")); Stopwatch::debug(PSTR("stop"));
#endif #endif
if (!this->isRunning()) return; if (this->isRunning() || this->isPaused()) {
this->state = STOPWATCH_STOPPED;
this->state = STPWTCH_STOPPED;
this->stopTimestamp = millis(); this->stopTimestamp = millis();
return true;
}
else return false;
} }
void Stopwatch::pause() { bool Stopwatch::pause() {
#if ENABLED(DEBUG_STOPWATCH) #if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("pause")); Stopwatch::debug(PSTR("pause"));
#endif #endif
if (!this->isRunning()) return; if (this->isRunning()) {
this->state = STOPWATCH_PAUSED;
this->state = STPWTCH_PAUSED;
this->stopTimestamp = millis(); this->stopTimestamp = millis();
return true;
}
else return false;
} }
void Stopwatch::start() { bool Stopwatch::start() {
#if ENABLED(DEBUG_STOPWATCH) #if ENABLED(DEBUG_STOPWATCH)
Stopwatch::debug(PSTR("start")); Stopwatch::debug(PSTR("start"));
#endif #endif
if (this->isRunning()) return; if (!this->isRunning()) {
if (this->isPaused()) this->accumulator = this->duration(); if (this->isPaused()) this->accumulator = this->duration();
else this->reset(); else this->reset();
this->state = STPWTCH_RUNNING; this->state = STOPWATCH_RUNNING;
this->startTimestamp = millis(); this->startTimestamp = millis();
return true;
}
else return false;
} }
void Stopwatch::reset() { void Stopwatch::reset() {
@ -68,18 +74,18 @@ void Stopwatch::reset() {
Stopwatch::debug(PSTR("reset")); Stopwatch::debug(PSTR("reset"));
#endif #endif
this->state = STPWTCH_STOPPED; this->state = STOPWATCH_STOPPED;
this->startTimestamp = 0; this->startTimestamp = 0;
this->stopTimestamp = 0; this->stopTimestamp = 0;
this->accumulator = 0; this->accumulator = 0;
} }
bool Stopwatch::isRunning() { bool Stopwatch::isRunning() {
return (this->state == STPWTCH_RUNNING) ? true : false; return (this->state == STOPWATCH_RUNNING) ? true : false;
} }
bool Stopwatch::isPaused() { bool Stopwatch::isPaused() {
return (this->state == STPWTCH_PAUSED) ? true : false; return (this->state == STOPWATCH_PAUSED) ? true : false;
} }
uint16_t Stopwatch::duration() { uint16_t Stopwatch::duration() {

@ -29,9 +29,9 @@
//#define DEBUG_STOPWATCH //#define DEBUG_STOPWATCH
enum StopwatchState { enum StopwatchState {
STPWTCH_STOPPED, STOPWATCH_STOPPED,
STPWTCH_RUNNING, STOPWATCH_RUNNING,
STPWTCH_PAUSED STOPWATCH_PAUSED
}; };
/** /**
@ -56,22 +56,25 @@ class Stopwatch {
* @brief Stops the stopwatch * @brief Stops the stopwatch
* @details Stops the running timer, it will silently ignore the request if * @details Stops 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
*/ */
void stop(); bool stop();
/** /**
* @brief Pauses the stopwatch * @brief Pause the stopwatch
* @details Pauses the running timer, it will silently ignore the request if * @details Pauses 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
*/ */
void pause(); bool pause();
/** /**
* @brief Starts the stopwatch * @brief Starts the stopwatch
* @details Starts the timer, it will silently ignore the request if the * @details Starts the timer, it will silently ignore the request if the
* timer is already running. * timer is already running.
* @return true is method was successful
*/ */
void start(); bool start();
/** /**
* @brief Resets the stopwatch * @brief Resets the stopwatch
@ -82,21 +85,21 @@ class Stopwatch {
/** /**
* @brief Checks if the timer is running * @brief Checks if the timer is running
* @details Returns true if the timer is currently running, false otherwise. * @details Returns true if the timer is currently running, false otherwise.
* @return bool * @return true if stopwatch is running
*/ */
bool isRunning(); bool isRunning();
/** /**
* @brief Checks if the timer is paused * @brief Checks if the timer is paused
* @details Returns true if the timer is currently paused, false otherwise. * @details Returns true if the timer is currently paused, false otherwise.
* @return bool * @return true if stopwatch is paused
*/ */
bool isPaused(); bool isPaused();
/** /**
* @brief Gets the running time * @brief Gets the running time
* @details Returns the total number of seconds the timer has been running. * @details Returns the total number of seconds the timer has been running.
* @return uint16_t * @return the delta since starting the stopwatch
*/ */
uint16_t duration(); uint16_t duration();

Loading…
Cancel
Save