Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stopwatch and PrintCounter improvements #3813

Merged
merged 1 commit into from
May 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a compilation warning.

AppData\Local\Temp\builde7989c12e25d478367e8a5397818719d.tmp\sketch\printcounter.cpp: In member function 'bool PrintCounter::stop()':

AppData\Local\Temp\builde7989c12e25d478367e8a5397818719d.tmp\sketch\printcounter.cpp:169:1: warning: control reaches end of non-void function [-Wreturn-type]

 }

 ^

Then, is

  if (super::stop()) {
    this->data.finishedPrints++;
    this->data.printTime += this->deltaDuration();
    this->saveStats();
    return true;
  }
  else return false;

better?

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