Skip to content

Commit

Permalink
Fixes MarlinFirmware#3809 and adds several improvements to the Stopwa…
Browse files Browse the repository at this point in the history
…tch and

PrintCounter classes
  • Loading branch information
jbrazio committed May 22, 2016
1 parent 61de6da commit 8c0edb2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
34 changes: 23 additions & 11 deletions Marlin/printcounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Marlin/printcounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 26 additions & 20 deletions Marlin/stopwatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,59 +27,65 @@ 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->isPaused()) this->accumulator = this->duration();
else this->reset();
if (!this->isRunning()) {
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() {
#if ENABLED(DEBUG_STOPWATCH)
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() {
Expand Down
23 changes: 13 additions & 10 deletions Marlin/stopwatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
//#define DEBUG_STOPWATCH

enum StopwatchState {
STPWTCH_STOPPED,
STPWTCH_RUNNING,
STPWTCH_PAUSED
STOPWATCH_STOPPED,
STOPWATCH_RUNNING,
STOPWATCH_PAUSED
};

/**
Expand All @@ -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
Expand All @@ -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();

Expand Down

0 comments on commit 8c0edb2

Please sign in to comment.