2017-02-25 04:15:18 -06:00
|
|
|
/**
|
2016-04-26 20:12:08 -05:00
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2016-04-26 20:12:08 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2016-04-26 20:12:08 -05:00
|
|
|
*
|
|
|
|
* 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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-04-26 20:12:08 -05:00
|
|
|
*
|
|
|
|
*/
|
2018-11-04 02:25:55 -06:00
|
|
|
#pragma once
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "../libs/stopwatch.h"
|
2018-02-28 04:15:29 -06:00
|
|
|
#include "../libs/duration_t.h"
|
2018-03-04 21:23:43 -06:00
|
|
|
#include "../inc/MarlinConfig.h"
|
2016-04-26 20:12:08 -05:00
|
|
|
|
|
|
|
// Print debug messages with M111 S2
|
2016-04-26 20:55:22 -05:00
|
|
|
//#define DEBUG_PRINTCOUNTER
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2020-04-29 14:46:33 -05:00
|
|
|
// Round up I2C / SPI address to next page boundary (assuming 32 byte pages)
|
|
|
|
#define STATS_EEPROM_ADDRESS TERN(USE_WIRED_EEPROM, 0x40, 0x32)
|
2018-03-04 21:23:43 -06:00
|
|
|
|
2018-07-01 15:20:28 -05:00
|
|
|
struct printStatistics { // 16 bytes
|
2016-04-27 20:30:07 -05:00
|
|
|
//const uint8_t magic; // Magic header, it will always be 0x16
|
2016-04-26 20:12:46 -05:00
|
|
|
uint16_t totalPrints; // Number of prints
|
|
|
|
uint16_t finishedPrints; // Number of complete prints
|
2016-07-13 20:18:59 -05:00
|
|
|
uint32_t printTime; // Accumulated printing time
|
2017-04-26 02:43:11 -05:00
|
|
|
uint32_t longestPrint; // Longest successful print job
|
2018-07-01 15:20:28 -05:00
|
|
|
float filamentUsed; // Accumulated filament consumed in mm
|
2019-02-12 15:58:56 -06:00
|
|
|
#if SERVICE_INTERVAL_1 > 0
|
|
|
|
uint32_t nextService1; // Service intervals (or placeholders)
|
|
|
|
#endif
|
|
|
|
#if SERVICE_INTERVAL_2 > 0
|
|
|
|
uint32_t nextService2;
|
|
|
|
#endif
|
|
|
|
#if SERVICE_INTERVAL_3 > 0
|
|
|
|
uint32_t nextService3;
|
|
|
|
#endif
|
2016-04-26 20:12:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class PrintCounter: public Stopwatch {
|
|
|
|
private:
|
|
|
|
typedef Stopwatch super;
|
|
|
|
|
2020-04-29 14:46:33 -05:00
|
|
|
#if EITHER(USE_WIRED_EEPROM, CPU_32_BIT)
|
2019-02-12 15:58:56 -06:00
|
|
|
typedef uint32_t eeprom_address_t;
|
2018-03-04 21:23:43 -06:00
|
|
|
#else
|
2019-02-12 15:58:56 -06:00
|
|
|
typedef uint16_t eeprom_address_t;
|
2018-03-04 21:23:43 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static printStatistics data;
|
2016-04-26 20:12:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief EEPROM address
|
|
|
|
* @details Defines the start offset address where the data is stored.
|
|
|
|
*/
|
2019-02-12 15:58:56 -06:00
|
|
|
static const eeprom_address_t address;
|
2018-03-04 21:23:43 -06:00
|
|
|
|
2016-04-26 20:12:08 -05:00
|
|
|
/**
|
|
|
|
* @brief Interval in seconds between counter updates
|
|
|
|
* @details This const value defines what will be the time between each
|
2016-04-28 09:40:24 -05:00
|
|
|
* accumulator update. This is different from the EEPROM save interval.
|
2016-04-26 20:12:08 -05:00
|
|
|
*/
|
2021-01-25 08:32:58 -06:00
|
|
|
static constexpr millis_t updateInterval = SEC_TO_MS(10);
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2021-01-26 02:30:31 -06:00
|
|
|
#if PRINTCOUNTER_SAVE_INTERVAL > 0
|
|
|
|
/**
|
|
|
|
* @brief Interval in seconds between EEPROM saves
|
|
|
|
* @details This const value defines what will be the time between each
|
|
|
|
* EEPROM save cycle, the development team recommends to set this value
|
|
|
|
* no lower than 3600 secs (1 hour).
|
|
|
|
*/
|
|
|
|
static constexpr millis_t saveInterval = MIN_TO_MS(PRINTCOUNTER_SAVE_INTERVAL);
|
|
|
|
#endif
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2016-04-27 20:30:07 -05:00
|
|
|
/**
|
|
|
|
* @brief Timestamp of the last call to deltaDuration()
|
2018-03-04 21:23:43 -06:00
|
|
|
* @details Store the timestamp of the last deltaDuration(), this is
|
2016-04-27 20:30:07 -05:00
|
|
|
* required due to the updateInterval cycle.
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static millis_t lastDuration;
|
2016-04-27 20:30:07 -05:00
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Stats were loaded from EEPROM
|
2016-04-26 20:12:46 -05:00
|
|
|
* @details If set to true it indicates if the statistical data was already
|
|
|
|
* loaded from the EEPROM.
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static bool loaded;
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2016-04-27 20:30:07 -05:00
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* @brief dT since the last call
|
2018-03-04 21:23:43 -06:00
|
|
|
* @details Return the elapsed time in seconds since the last call, this is
|
2016-04-27 20:30:07 -05:00
|
|
|
* used internally for print statistics accounting is not intended to be a
|
|
|
|
* user callable function.
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static millis_t deltaDuration();
|
2016-04-27 20:30:07 -05:00
|
|
|
|
2016-04-26 20:12:08 -05:00
|
|
|
public:
|
2018-03-04 16:12:30 -06:00
|
|
|
|
2016-04-26 20:12:08 -05:00
|
|
|
/**
|
2018-03-04 16:12:30 -06:00
|
|
|
* @brief Initialize the print counter
|
2016-04-26 20:12:08 -05:00
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static inline void init() {
|
2018-03-04 16:12:30 -06:00
|
|
|
super::init();
|
2018-03-04 21:23:43 -06:00
|
|
|
loadStats();
|
2018-03-04 16:12:30 -06:00
|
|
|
}
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Check if Print Statistics has been loaded
|
|
|
|
* @details Return true if the statistical data has been loaded.
|
2016-04-26 20:12:46 -05:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
FORCE_INLINE static bool isLoaded() { return loaded; }
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2016-07-13 20:18:59 -05:00
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Increment the total filament used
|
2016-07-13 20:18:59 -05:00
|
|
|
* @details The total filament used counter will be incremented by "amount".
|
|
|
|
*
|
|
|
|
* @param amount The amount of filament used in mm
|
|
|
|
*/
|
2018-07-01 15:20:28 -05:00
|
|
|
static void incFilamentUsed(float const &amount);
|
2016-07-13 20:18:59 -05:00
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Reset the Print Statistics
|
|
|
|
* @details Reset the statistics to zero and saves them to EEPROM creating
|
2016-04-26 20:12:46 -05:00
|
|
|
* also the magic header.
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static void initStats();
|
2016-04-26 20:12:46 -05:00
|
|
|
|
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Load the Print Statistics
|
|
|
|
* @details Load the statistics from EEPROM
|
2016-04-26 20:12:46 -05:00
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static void loadStats();
|
2016-04-26 20:12:46 -05:00
|
|
|
|
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Save the Print Statistics
|
|
|
|
* @details Save the statistics to EEPROM
|
2016-04-26 20:12:46 -05:00
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static void saveStats();
|
2016-04-26 20:12:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Serial output the Print Statistics
|
|
|
|
* @details This function may change in the future, for now it directly
|
|
|
|
* prints the statistical data to serial.
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static void showStats();
|
2016-04-26 20:12:46 -05:00
|
|
|
|
2016-06-30 04:24:29 -05:00
|
|
|
/**
|
|
|
|
* @brief Return the currently loaded statistics
|
|
|
|
* @details Return the raw data, in the same structure used internally
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static printStatistics getStats() { return data; }
|
2016-06-30 04:24:29 -05:00
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
/**
|
|
|
|
* @brief Loop function
|
|
|
|
* @details This function should be called at loop, it will take care of
|
|
|
|
* periodically save the statistical data to EEPROM and do time keeping.
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static void tick();
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2016-04-26 20:12:46 -05:00
|
|
|
/**
|
|
|
|
* The following functions are being overridden
|
|
|
|
*/
|
2018-03-04 21:23:43 -06:00
|
|
|
static bool start();
|
2021-01-26 02:30:31 -06:00
|
|
|
static bool _stop(const bool completed);
|
|
|
|
static inline bool stop() { return _stop(true); }
|
|
|
|
static inline bool abort() { return _stop(false); }
|
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
static void reset();
|
2016-04-26 20:12:08 -05:00
|
|
|
|
2019-02-12 15:58:56 -06:00
|
|
|
#if HAS_SERVICE_INTERVALS
|
|
|
|
static void resetServiceInterval(const int index);
|
|
|
|
static bool needsService(const int index);
|
|
|
|
#endif
|
|
|
|
|
2016-04-26 20:12:08 -05:00
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
|
|
|
|
/**
|
2018-03-04 21:23:43 -06:00
|
|
|
* @brief Print a debug message
|
|
|
|
* @details Print a simple debug message
|
2016-04-26 20:12:08 -05:00
|
|
|
*/
|
|
|
|
static void debug(const char func[]);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2018-03-04 21:23:43 -06:00
|
|
|
// Global Print Job Timer instance
|
2017-09-09 00:18:43 -05:00
|
|
|
#if ENABLED(PRINTCOUNTER)
|
|
|
|
extern PrintCounter print_job_timer;
|
|
|
|
#else
|
|
|
|
extern Stopwatch print_job_timer;
|
|
|
|
#endif
|