PrintCounter EEPROM read/write optimizations

This commit is contained in:
João Brázio 2016-04-28 02:30:07 +01:00
parent 9589e51810
commit 8fb23e899f
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: F62CFD37DFFDB540
2 changed files with 36 additions and 53 deletions

View file

@ -28,6 +28,16 @@ PrintCounter::PrintCounter(): super() {
this->loadStats(); this->loadStats();
} }
uint16_t PrintCounter::deltaDuration() {
#if ENABLED(DEBUG_PRINTCOUNTER)
PrintCounter::debug(PSTR("deltaDuration"));
#endif
uint16_t tmp = this->lastDuration;
this->lastDuration = this->duration();
return this->lastDuration - tmp;
}
bool PrintCounter::isLoaded() { bool PrintCounter::isLoaded() {
return this->loaded; return this->loaded;
} }
@ -38,9 +48,7 @@ void PrintCounter::initStats() {
#endif #endif
this->loaded = true; this->loaded = true;
this->data = { this->data = { 0, 0, 0, 0 };
0, 0, 0, 0
};
this->saveStats(); this->saveStats();
eeprom_write_byte((uint8_t*) this->addr, 0x16); eeprom_write_byte((uint8_t*) this->addr, 0x16);
@ -51,28 +59,9 @@ void PrintCounter::loadStats() {
PrintCounter::debug(PSTR("loadStats")); PrintCounter::debug(PSTR("loadStats"));
#endif #endif
uint16_t addr = this->addr;
// Checks if the EEPROM block is initialized // Checks if the EEPROM block is initialized
if (eeprom_read_byte((uint8_t*) addr) != 0x16) this->initStats(); if (eeprom_read_byte((uint8_t*) this->addr) != 0x16) this->initStats();
else eeprom_read_block(&this->data, (void *)(this->addr + sizeof(uint8_t)), sizeof(printStatistics));
else {
// Skip the magic header byte
addr += sizeof(uint8_t);
// @todo This section will need rewrite once the ConfigurationStore
// and/or PersistentStorage object comes along.
this->data.totalPrints = eeprom_read_word((uint16_t*) addr);
addr += sizeof(uint16_t);
this->data.finishedPrints = eeprom_read_word((uint16_t*) addr);
addr += sizeof(uint16_t);
this->data.printTime = eeprom_read_dword((uint32_t*) addr);
addr += sizeof(uint32_t);
this->data.longestPrint = eeprom_read_dword((uint32_t*) addr);
}
this->loaded = true; this->loaded = true;
} }
@ -85,21 +74,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;
// Skip the magic header byte eeprom_write_block(&this->data, (void *)(this->addr + sizeof(uint8_t)), sizeof(printStatistics));
uint16_t addr = this->addr + sizeof(uint8_t);
// @todo This section will need rewrite once the ConfigurationStore
// and/or PersistentStorage object comes along.
eeprom_write_word ((uint16_t*) addr, this->data.totalPrints);
addr += sizeof(uint16_t);
eeprom_write_word ((uint16_t*) addr, this->data.finishedPrints);
addr += sizeof(uint16_t);
eeprom_write_dword((uint32_t*) addr, this->data.printTime);
addr += sizeof(uint32_t);
eeprom_write_dword((uint32_t*) addr, this->data.longestPrint);
} }
void PrintCounter::showStats() { void PrintCounter::showStats() {
@ -110,7 +85,8 @@ void PrintCounter::showStats() {
SERIAL_ECHO(this->data.finishedPrints); SERIAL_ECHO(this->data.finishedPrints);
SERIAL_ECHOPGM(", Failed: "); SERIAL_ECHOPGM(", Failed: ");
SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints); SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints
- (this->isRunning() || this->isPaused()) ? 1 : 0); // Removes 1 from failures with an active counter
uint32_t t = this->data.printTime /60; uint32_t t = this->data.printTime /60;
SERIAL_ECHOPGM(", Total print time: "); SERIAL_ECHOPGM(", Total print time: ");
@ -148,8 +124,7 @@ void PrintCounter::tick() {
#endif #endif
uint16_t t = this->duration();; uint16_t t = this->duration();;
this->data.printTime += t - this->lastUpdate; this->data.printTime += this->deltaDuration();
this->lastUpdate = t;
update_before = now; update_before = now;
} }
@ -178,7 +153,7 @@ void PrintCounter::stop() {
super::stop(); super::stop();
this->data.finishedPrints++; this->data.finishedPrints++;
this->data.printTime += this->duration() - this->lastUpdate; this->data.printTime += this->deltaDuration();
this->saveStats(); this->saveStats();
} }
@ -187,7 +162,7 @@ void PrintCounter::reset() {
PrintCounter::debug(PSTR("stop")); PrintCounter::debug(PSTR("stop"));
#endif #endif
this->lastUpdate = 0; this->lastDuration = 0;
super::reset(); super::reset();
} }

View file

@ -30,7 +30,7 @@
//#define DEBUG_PRINTCOUNTER //#define DEBUG_PRINTCOUNTER
struct printStatistics { // 13 bytes struct printStatistics { // 13 bytes
//uint8_t magic; // Magic header, it will always be 0x16 //const uint8_t magic; // Magic header, it will always be 0x16
uint16_t totalPrints; // Number of prints uint16_t totalPrints; // Number of prints
uint16_t finishedPrints; // Number of complete prints uint16_t finishedPrints; // Number of complete prints
uint32_t printTime; // Total printing time uint32_t printTime; // Total printing time
@ -43,14 +43,6 @@ class PrintCounter: public Stopwatch {
printStatistics data; printStatistics data;
/**
* @brief Timestamp of the last update
* @details Stores the timestamp of the last data.pritnTime update, when the
* print job finishes, this will be used to calculate the exact time elapsed,
* this is required due to the updateInterval cycle.
*/
uint16_t lastUpdate;
/** /**
* @brief EEPROM address * @brief EEPROM address
* @details Defines the start offset address where the data is stored. * @details Defines the start offset address where the data is stored.
@ -73,6 +65,13 @@ class PrintCounter: public Stopwatch {
*/ */
const uint16_t saveInterval = 3600; const uint16_t saveInterval = 3600;
/**
* @brief Timestamp of the last call to deltaDuration()
* @details Stores the timestamp of the last deltaDuration(), this is
* required due to the updateInterval cycle.
*/
uint16_t lastDuration;
/** /**
* @brief Stats were loaded from EERPROM * @brief Stats were loaded from EERPROM
* @details If set to true it indicates if the statistical data was already * @details If set to true it indicates if the statistical data was already
@ -80,6 +79,15 @@ class PrintCounter: public Stopwatch {
*/ */
bool loaded = false; bool loaded = false;
protected:
/**
* @brief dT since the last call
* @details Returns the elapsed time in seconds since the last call, this is
* used internally for print statistics accounting is not intended to be a
* user callable function.
*/
uint16_t deltaDuration();
public: public:
/** /**
* @brief Class constructor * @brief Class constructor