2018-11-16 03:32:01 -06:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
#include "Marlin.h"
|
2018-11-16 03:32:01 -06:00
|
|
|
#include "printcounter.h"
|
|
|
|
#include "duration_t.h"
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::PrintCounter(): super() {
|
|
|
|
this->loadStats();
|
|
|
|
}
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
millis_t PrintCounter::deltaDuration() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("deltaDuration"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
millis_t tmp = this->lastDuration;
|
|
|
|
this->lastDuration = this->duration();
|
|
|
|
return this->lastDuration - tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PrintCounter::isLoaded() {
|
|
|
|
return this->loaded;
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
void PrintCounter::incFilamentUsed(double const &amount) {
|
2018-11-16 03:32:01 -06:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("incFilamentUsed"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Refuses to update data if object is not loaded
|
2019-01-31 05:25:28 -06:00
|
|
|
if (!this->isLoaded()) return;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
this->data.filamentUsed += amount; // mm
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
|
2018-11-16 03:32:01 -06:00
|
|
|
void PrintCounter::initStats() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("initStats"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
this->loaded = true;
|
|
|
|
this->data = { 0, 0, 0, 0, 0.0 };
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
this->saveStats();
|
|
|
|
eeprom_write_byte((uint8_t *) this->address, 0x16);
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::loadStats() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("loadStats"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Checks if the EEPROM block is initialized
|
2019-01-31 05:25:28 -06:00
|
|
|
if (eeprom_read_byte((uint8_t *) this->address) != 0x16) this->initStats();
|
|
|
|
else eeprom_read_block(&this->data,
|
|
|
|
(void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
this->loaded = true;
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::saveStats() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("saveStats"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Refuses to save data if object is not loaded
|
2019-01-31 05:25:28 -06:00
|
|
|
if (!this->isLoaded()) return;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
// Saves the struct to EEPROM
|
2019-01-31 05:25:28 -06:00
|
|
|
eeprom_update_block(&this->data,
|
|
|
|
(void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::showStats() {
|
|
|
|
char buffer[21];
|
2019-01-31 05:25:28 -06:00
|
|
|
duration_t elapsed;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
SERIAL_PROTOCOLPGM(MSG_STATS);
|
|
|
|
|
|
|
|
SERIAL_ECHOPGM("Prints: ");
|
2019-01-31 05:25:28 -06:00
|
|
|
SERIAL_ECHO(this->data.totalPrints);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
SERIAL_ECHOPGM(", Finished: ");
|
2019-01-31 05:25:28 -06:00
|
|
|
SERIAL_ECHO(this->data.finishedPrints);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
SERIAL_ECHOPGM(", Failed: "); // Note: Removes 1 from failures with an active counter
|
2019-01-31 05:25:28 -06:00
|
|
|
SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints
|
|
|
|
- ((this->isRunning() || this->isPaused()) ? 1 : 0));
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
SERIAL_EOL();
|
|
|
|
SERIAL_PROTOCOLPGM(MSG_STATS);
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
elapsed = this->data.printTime;
|
2018-11-16 03:32:01 -06:00
|
|
|
elapsed.toString(buffer);
|
|
|
|
|
|
|
|
SERIAL_ECHOPGM("Total time: ");
|
|
|
|
SERIAL_ECHO(buffer);
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
SERIAL_ECHOPGM(" (");
|
2019-01-31 05:25:28 -06:00
|
|
|
SERIAL_ECHO(this->data.printTime);
|
2018-11-16 03:32:01 -06:00
|
|
|
SERIAL_CHAR(')');
|
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
elapsed = this->data.longestPrint;
|
2018-11-16 03:32:01 -06:00
|
|
|
elapsed.toString(buffer);
|
|
|
|
|
|
|
|
SERIAL_ECHOPGM(", Longest job: ");
|
|
|
|
SERIAL_ECHO(buffer);
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
SERIAL_ECHOPGM(" (");
|
2019-01-31 05:25:28 -06:00
|
|
|
SERIAL_ECHO(this->data.longestPrint);
|
2018-11-16 03:32:01 -06:00
|
|
|
SERIAL_CHAR(')');
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SERIAL_EOL();
|
|
|
|
SERIAL_PROTOCOLPGM(MSG_STATS);
|
|
|
|
|
|
|
|
SERIAL_ECHOPGM("Filament used: ");
|
2019-01-31 05:25:28 -06:00
|
|
|
SERIAL_ECHO(this->data.filamentUsed / 1000);
|
|
|
|
SERIAL_ECHOPGM("m");
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
SERIAL_EOL();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::tick() {
|
2019-01-31 05:25:28 -06:00
|
|
|
if (!this->isRunning()) return;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
static uint32_t update_last = millis(),
|
|
|
|
eeprom_last = millis();
|
|
|
|
|
|
|
|
millis_t now = millis();
|
|
|
|
|
|
|
|
// Trying to get the amount of calculations down to the bare min
|
2019-01-31 05:25:28 -06:00
|
|
|
const static uint16_t i = this->updateInterval * 1000;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
if (now - update_last >= i) {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("tick"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
this->data.printTime += this->deltaDuration();
|
2018-11-16 03:32:01 -06:00
|
|
|
update_last = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trying to get the amount of calculations down to the bare min
|
2019-01-31 05:25:28 -06:00
|
|
|
const static millis_t j = this->saveInterval * 1000;
|
2018-11-16 03:32:01 -06:00
|
|
|
if (now - eeprom_last >= j) {
|
|
|
|
eeprom_last = now;
|
2019-01-31 05:25:28 -06:00
|
|
|
this->saveStats();
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
bool PrintCounter::start() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("start"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
bool paused = this->isPaused();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
if (super::start()) {
|
|
|
|
if (!paused) {
|
2019-01-31 05:25:28 -06:00
|
|
|
this->data.totalPrints++;
|
|
|
|
this->lastDuration = 0;
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
bool PrintCounter::stop() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("stop"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (super::stop()) {
|
2019-01-31 05:25:28 -06:00
|
|
|
this->data.finishedPrints++;
|
|
|
|
this->data.printTime += this->deltaDuration();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
if (this->duration() > this->data.longestPrint)
|
|
|
|
this->data.longestPrint = this->duration();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
this->saveStats();
|
2018-11-16 03:32:01 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
void PrintCounter::reset() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter::debug(PSTR("stop"));
|
2018-11-16 03:32:01 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
super::reset();
|
2019-01-31 05:25:28 -06:00
|
|
|
this->lastDuration = 0;
|
2018-11-16 03:32:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
|
|
|
|
void PrintCounter::debug(const char func[]) {
|
|
|
|
if (DEBUGGING(INFO)) {
|
|
|
|
SERIAL_ECHOPGM("PrintCounter::");
|
|
|
|
serialprintPGM(func);
|
|
|
|
SERIAL_ECHOLNPGM("()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
#endif
|