2017-02-25 04:15:18 -06:00
|
|
|
/**
|
2016-04-26 20:12:08 -05: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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-09-09 00:18:43 -05:00
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
#if DISABLED(PRINTCOUNTER)
|
2017-09-09 00:18:43 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
#include "../libs/stopwatch.h"
|
|
|
|
Stopwatch print_job_timer; // Global Print Job Timer instance
|
2017-09-06 06:28:32 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
#else // PRINTCOUNTER
|
|
|
|
|
|
|
|
#include "printcounter.h"
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "../Marlin.h"
|
2018-08-14 03:28:52 -05:00
|
|
|
#include "../HAL/shared/persistent_store_api.h"
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
PrintCounter print_job_timer; // Global Print Job Timer instance
|
|
|
|
|
|
|
|
printStatistics PrintCounter::data;
|
|
|
|
|
|
|
|
const PrintCounter::promdress PrintCounter::address = STATS_EEPROM_ADDRESS;
|
|
|
|
|
|
|
|
millis_t PrintCounter::lastDuration;
|
|
|
|
bool PrintCounter::loaded = false;
|
|
|
|
|
2016-07-12 21:03:42 -05:00
|
|
|
millis_t PrintCounter::deltaDuration() {
|
2016-04-27 20:30:07 -05:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("deltaDuration"));
|
2016-04-27 20:30:07 -05:00
|
|
|
#endif
|
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
millis_t tmp = lastDuration;
|
|
|
|
lastDuration = duration();
|
|
|
|
return lastDuration - tmp;
|
2016-04-26 20:12:46 -05:00
|
|
|
}
|
|
|
|
|
2018-07-01 15:20:28 -05:00
|
|
|
void PrintCounter::incFilamentUsed(float const &amount) {
|
2016-07-13 20:18:59 -05:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("incFilamentUsed"));
|
2016-07-13 20:18:59 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Refuses to update data if object is not loaded
|
2018-03-04 21:23:43 -06:00
|
|
|
if (!isLoaded()) return;
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
data.filamentUsed += amount; // mm
|
2016-07-13 20:18:59 -05:00
|
|
|
}
|
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
void PrintCounter::initStats() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("initStats"));
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
loaded = true;
|
|
|
|
data = { 0, 0, 0, 0, 0.0 };
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
saveStats();
|
2018-08-12 02:50:39 -05:00
|
|
|
persistentStore.access_start();
|
2018-08-14 18:35:36 -05:00
|
|
|
persistentStore.write_data(address, (uint8_t)0x16);
|
2018-08-12 02:50:39 -05:00
|
|
|
persistentStore.access_finish();
|
2016-04-26 20:12:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::loadStats() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("loadStats"));
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
|
|
|
|
2018-08-12 02:50:39 -05:00
|
|
|
// Check if the EEPROM block is initialized
|
2018-08-14 18:35:36 -05:00
|
|
|
uint8_t value = 0;
|
2018-08-12 02:50:39 -05:00
|
|
|
persistentStore.access_start();
|
2018-08-14 18:35:36 -05:00
|
|
|
persistentStore.read_data(address, &value, sizeof(uint8_t));
|
|
|
|
if (value != 0x16)
|
|
|
|
initStats();
|
|
|
|
else
|
|
|
|
persistentStore.read_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics));
|
2018-08-12 02:50:39 -05:00
|
|
|
persistentStore.access_finish();
|
2018-03-04 21:23:43 -06:00
|
|
|
loaded = true;
|
2016-04-26 20:12:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::saveStats() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("saveStats"));
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
|
|
|
|
2016-07-13 20:18:59 -05:00
|
|
|
// Refuses to save data if object is not loaded
|
2018-03-04 21:23:43 -06:00
|
|
|
if (!isLoaded()) return;
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2016-05-21 19:59:59 -05:00
|
|
|
// Saves the struct to EEPROM
|
2018-08-12 02:50:39 -05:00
|
|
|
persistentStore.access_start();
|
2018-08-14 18:35:36 -05:00
|
|
|
persistentStore.write_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics));
|
2018-08-12 02:50:39 -05:00
|
|
|
persistentStore.access_finish();
|
2016-04-26 20:12:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintCounter::showStats() {
|
2016-07-22 09:47:56 -05:00
|
|
|
char buffer[21];
|
|
|
|
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_ECHOPGM(MSG_STATS);
|
2016-07-13 20:18:59 -05:00
|
|
|
|
|
|
|
SERIAL_ECHOPGM("Prints: ");
|
2018-03-04 21:23:43 -06:00
|
|
|
SERIAL_ECHO(data.totalPrints);
|
2016-04-26 20:12:46 -05:00
|
|
|
|
|
|
|
SERIAL_ECHOPGM(", Finished: ");
|
2018-03-04 21:23:43 -06:00
|
|
|
SERIAL_ECHO(data.finishedPrints);
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2016-07-13 20:18:59 -05:00
|
|
|
SERIAL_ECHOPGM(", Failed: "); // Note: Removes 1 from failures with an active counter
|
2018-03-04 21:23:43 -06:00
|
|
|
SERIAL_ECHO(data.totalPrints - data.finishedPrints
|
|
|
|
- ((isRunning() || isPaused()) ? 1 : 0));
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_ECHOPGM(MSG_STATS);
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
duration_t elapsed = data.printTime;
|
2016-07-23 21:13:35 -05:00
|
|
|
elapsed.toString(buffer);
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2016-07-22 09:47:56 -05:00
|
|
|
SERIAL_ECHOPGM("Total time: ");
|
|
|
|
SERIAL_ECHO(buffer);
|
2016-04-26 20:12:46 -05:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
SERIAL_ECHOPGM(" (");
|
2018-03-04 21:23:43 -06:00
|
|
|
SERIAL_ECHO(data.printTime);
|
2017-03-18 10:17:00 -05:00
|
|
|
SERIAL_CHAR(')');
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
elapsed = data.longestPrint;
|
2016-07-23 21:13:35 -05:00
|
|
|
elapsed.toString(buffer);
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2016-07-22 09:47:56 -05:00
|
|
|
SERIAL_ECHOPGM(", Longest job: ");
|
|
|
|
SERIAL_ECHO(buffer);
|
2016-07-13 20:18:59 -05:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
SERIAL_ECHOPGM(" (");
|
2018-03-04 21:23:43 -06:00
|
|
|
SERIAL_ECHO(data.longestPrint);
|
2017-03-18 10:17:00 -05:00
|
|
|
SERIAL_CHAR(')');
|
2016-07-13 20:18:59 -05:00
|
|
|
#endif
|
|
|
|
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2018-11-29 16:58:58 -06:00
|
|
|
SERIAL_ECHOPGM(MSG_STATS);
|
2016-07-13 20:18:59 -05:00
|
|
|
|
|
|
|
SERIAL_ECHOPGM("Filament used: ");
|
2018-03-04 21:23:43 -06:00
|
|
|
SERIAL_ECHO(data.filamentUsed / 1000);
|
2018-01-23 19:09:40 -06:00
|
|
|
SERIAL_CHAR('m');
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2017-06-09 10:51:23 -05:00
|
|
|
SERIAL_EOL();
|
2016-04-26 20:12:46 -05:00
|
|
|
}
|
2016-04-26 20:12:08 -05:00
|
|
|
|
|
|
|
void PrintCounter::tick() {
|
2018-03-04 21:23:43 -06:00
|
|
|
if (!isRunning()) return;
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2016-07-12 21:03:42 -05:00
|
|
|
millis_t now = millis();
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2018-08-31 17:40:39 -05:00
|
|
|
static uint32_t update_next; // = 0
|
|
|
|
if (ELAPSED(now, update_next)) {
|
2016-04-26 20:12:46 -05:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("tick"));
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
2018-03-04 21:23:43 -06:00
|
|
|
data.printTime += deltaDuration();
|
2018-08-31 17:40:39 -05:00
|
|
|
update_next = now + updateInterval * 1000;
|
2016-04-26 20:12:08 -05:00
|
|
|
}
|
|
|
|
|
2018-08-31 17:40:39 -05:00
|
|
|
static uint32_t eeprom_next; // = 0
|
|
|
|
if (ELAPSED(now, eeprom_next)) {
|
|
|
|
eeprom_next = now + saveInterval * 1000;
|
2018-03-04 21:23:43 -06:00
|
|
|
saveStats();
|
2016-04-26 20:12:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-21 19:59:59 -05:00
|
|
|
// @Override
|
|
|
|
bool PrintCounter::start() {
|
2016-04-26 20:12:46 -05:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("start"));
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
bool paused = isPaused();
|
2016-05-21 19:59:59 -05:00
|
|
|
|
|
|
|
if (super::start()) {
|
|
|
|
if (!paused) {
|
2018-03-04 21:23:43 -06:00
|
|
|
data.totalPrints++;
|
|
|
|
lastDuration = 0;
|
2016-05-21 19:59:59 -05:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-26 02:43:11 -05:00
|
|
|
|
|
|
|
return false;
|
2016-04-26 20:12:08 -05:00
|
|
|
}
|
|
|
|
|
2016-05-21 19:59:59 -05:00
|
|
|
// @Override
|
|
|
|
bool PrintCounter::stop() {
|
2016-04-26 20:12:08 -05:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("stop"));
|
2016-04-26 20:12:08 -05:00
|
|
|
#endif
|
|
|
|
|
2016-05-21 19:59:59 -05:00
|
|
|
if (super::stop()) {
|
2018-03-04 21:23:43 -06:00
|
|
|
data.finishedPrints++;
|
|
|
|
data.printTime += deltaDuration();
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
if (duration() > data.longestPrint)
|
|
|
|
data.longestPrint = duration();
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
saveStats();
|
2016-05-22 07:14:58 -05:00
|
|
|
return true;
|
2016-05-21 19:59:59 -05:00
|
|
|
}
|
|
|
|
else return false;
|
2016-04-26 20:12:08 -05:00
|
|
|
}
|
|
|
|
|
2016-05-21 19:59:59 -05:00
|
|
|
// @Override
|
2016-04-26 20:12:46 -05:00
|
|
|
void PrintCounter::reset() {
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
2018-03-04 21:23:43 -06:00
|
|
|
debug(PSTR("stop"));
|
2016-04-26 20:12:46 -05:00
|
|
|
#endif
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
super::reset();
|
2018-03-04 21:23:43 -06:00
|
|
|
lastDuration = 0;
|
2016-04-26 20:12:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
|
|
|
|
void PrintCounter::debug(const char func[]) {
|
|
|
|
if (DEBUGGING(INFO)) {
|
|
|
|
SERIAL_ECHOPGM("PrintCounter::");
|
|
|
|
serialprintPGM(func);
|
|
|
|
SERIAL_ECHOLNPGM("()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-09 00:18:43 -05:00
|
|
|
|
|
|
|
#endif // PRINTCOUNTER
|