Fixes #3809 and adds several improvements to the Stopwatch and

PrintCounter classes
This commit is contained in:
João Brázio 2016-05-22 01:59:59 +01:00
parent 61de6daf1d
commit 8c0edb2de4
No known key found for this signature in database
GPG key ID: F62CFD37DFFDB540
4 changed files with 64 additions and 43 deletions

View file

@ -73,6 +73,7 @@ void PrintCounter::saveStats() {
// Refuses to save data is object is not loaded
if (!this->isLoaded()) return;
// Saves the struct to EEPROM
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)
PrintCounter::debug(PSTR("start"));
#endif
if (!this->isPaused()) this->data.totalPrints++;
super::start();
bool paused = this->isPaused();
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)
PrintCounter::debug(PSTR("stop"));
#endif
if (!this->isRunning()) return;
super::stop();
this->data.finishedPrints++;
this->data.printTime += this->deltaDuration();
this->saveStats();
if (super::stop()) {
this->data.finishedPrints++;
this->data.printTime += this->deltaDuration();
this->saveStats();
}
else return false;
}
// @Override
void PrintCounter::reset() {
#if ENABLED(DEBUG_PRINTCOUNTER)
PrintCounter::debug(PSTR("stop"));
#endif
this->lastDuration = 0;
super::reset();
this->lastDuration = 0;
}
#if ENABLED(DEBUG_PRINTCOUNTER)

View file

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

View file

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

View file

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