1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-27 13:56:24 +00:00
MarlinFirmware/Marlin/printcounter.cpp

252 lines
5.7 KiB
C++
Raw Normal View History

2017-02-25 10:15:18 +00: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/>.
*
*/
#include "MarlinConfig.h"
#if DISABLED(PRINTCOUNTER)
#include "stopwatch.h"
Stopwatch print_job_timer; // Global Print Job Timer instance
#else // PRINTCOUNTER
#include "printcounter.h"
2016-07-24 02:13:35 +00:00
#include "duration_t.h"
#include "Marlin.h"
PrintCounter print_job_timer; // Global Print Job Timer instance
#if ENABLED(I2C_EEPROM) || ENABLED(SPI_EEPROM)
// round up address to next page boundary (assuming 32 byte pages)
#define STATS_EEPROM_ADDRESS 0x40
#else
#define STATS_EEPROM_ADDRESS 0x32
#endif
const PrintCounter::promdress PrintCounter::address = STATS_EEPROM_ADDRESS;
const uint16_t PrintCounter::updateInterval = 10;
const uint16_t PrintCounter::saveInterval = 3600;
printStatistics PrintCounter::data;
millis_t PrintCounter::lastDuration;
bool PrintCounter::loaded = false;
2016-04-27 01:12:46 +00:00
millis_t PrintCounter::deltaDuration() {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("deltaDuration"));
#endif
millis_t tmp = lastDuration;
lastDuration = duration();
return lastDuration - tmp;
2016-04-27 01:12:46 +00:00
}
void PrintCounter::incFilamentUsed(double const &amount) {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("incFilamentUsed"));
#endif
// Refuses to update data if object is not loaded
if (!isLoaded()) return;
data.filamentUsed += amount; // mm
}
2016-04-27 01:12:46 +00:00
void PrintCounter::initStats() {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("initStats"));
2016-04-27 01:12:46 +00:00
#endif
loaded = true;
data = { 0, 0, 0, 0, 0.0 };
2016-04-27 01:12:46 +00:00
saveStats();
eeprom_write_byte((uint8_t*)address, 0x16);
2016-04-27 01:12:46 +00:00
}
void PrintCounter::loadStats() {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("loadStats"));
2016-04-27 01:12:46 +00:00
#endif
// Checks if the EEPROM block is initialized
if (eeprom_read_byte((uint8_t*)address) != 0x16) initStats();
else eeprom_read_block(&data,
(void*)(address + sizeof(uint8_t)), sizeof(printStatistics));
2016-04-27 01:12:46 +00:00
loaded = true;
2016-04-27 01:12:46 +00:00
}
void PrintCounter::saveStats() {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("saveStats"));
2016-04-27 01:12:46 +00:00
#endif
// Refuses to save data if object is not loaded
if (!isLoaded()) return;
2016-04-27 01:12:46 +00:00
// Saves the struct to EEPROM
eeprom_update_block(&data,
(void*)(address + sizeof(uint8_t)), sizeof(printStatistics));
2016-04-27 01:12:46 +00:00
}
void PrintCounter::showStats() {
char buffer[21];
SERIAL_PROTOCOLPGM(MSG_STATS);
SERIAL_ECHOPGM("Prints: ");
SERIAL_ECHO(data.totalPrints);
2016-04-27 01:12:46 +00:00
SERIAL_ECHOPGM(", Finished: ");
SERIAL_ECHO(data.finishedPrints);
2016-04-27 01:12:46 +00:00
SERIAL_ECHOPGM(", Failed: "); // Note: Removes 1 from failures with an active counter
SERIAL_ECHO(data.totalPrints - data.finishedPrints
- ((isRunning() || isPaused()) ? 1 : 0));
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
SERIAL_PROTOCOLPGM(MSG_STATS);
2016-04-27 01:12:46 +00:00
duration_t elapsed = data.printTime;
2016-07-24 02:13:35 +00:00
elapsed.toString(buffer);
SERIAL_ECHOPGM("Total time: ");
SERIAL_ECHO(buffer);
2016-04-27 01:12:46 +00:00
#if ENABLED(DEBUG_PRINTCOUNTER)
SERIAL_ECHOPGM(" (");
SERIAL_ECHO(data.printTime);
2017-03-18 15:17:00 +00:00
SERIAL_CHAR(')');
2016-04-27 01:12:46 +00:00
#endif
elapsed = data.longestPrint;
2016-07-24 02:13:35 +00:00
elapsed.toString(buffer);
SERIAL_ECHOPGM(", Longest job: ");
SERIAL_ECHO(buffer);
#if ENABLED(DEBUG_PRINTCOUNTER)
SERIAL_ECHOPGM(" (");
SERIAL_ECHO(data.longestPrint);
2017-03-18 15:17:00 +00:00
SERIAL_CHAR(')');
#endif
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
SERIAL_PROTOCOLPGM(MSG_STATS);
SERIAL_ECHOPGM("Filament used: ");
SERIAL_ECHO(data.filamentUsed / 1000);
2018-01-24 00:58:10 +00:00
SERIAL_CHAR('m');
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
2016-04-27 01:12:46 +00:00
}
void PrintCounter::tick() {
if (!isRunning()) return;
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
const static uint16_t i = updateInterval * 1000;
if (now - update_last >= i) {
2016-04-27 01:12:46 +00:00
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("tick"));
2016-04-27 01:12:46 +00:00
#endif
data.printTime += deltaDuration();
update_last = now;
}
// Trying to get the amount of calculations down to the bare min
const static millis_t j = saveInterval * 1000;
if (now - eeprom_last >= j) {
eeprom_last = now;
saveStats();
}
}
// @Override
bool PrintCounter::start() {
2016-04-27 01:12:46 +00:00
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("start"));
2016-04-27 01:12:46 +00:00
#endif
bool paused = isPaused();
if (super::start()) {
if (!paused) {
data.totalPrints++;
lastDuration = 0;
}
return true;
}
2017-04-26 07:43:11 +00:00
return false;
}
// @Override
bool PrintCounter::stop() {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("stop"));
#endif
if (super::stop()) {
data.finishedPrints++;
data.printTime += deltaDuration();
if (duration() > data.longestPrint)
data.longestPrint = duration();
saveStats();
2016-05-22 12:14:58 +00:00
return true;
}
else return false;
}
// @Override
2016-04-27 01:12:46 +00:00
void PrintCounter::reset() {
#if ENABLED(DEBUG_PRINTCOUNTER)
debug(PSTR("stop"));
2016-04-27 01:12:46 +00:00
#endif
2016-04-27 01:12:46 +00:00
super::reset();
lastDuration = 0;
}
#if ENABLED(DEBUG_PRINTCOUNTER)
void PrintCounter::debug(const char func[]) {
if (DEBUGGING(INFO)) {
SERIAL_ECHOPGM("PrintCounter::");
serialprintPGM(func);
SERIAL_ECHOLNPGM("()");
}
}
#endif
#endif // PRINTCOUNTER