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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PRINTCOUNTER_H
|
|
|
|
#define PRINTCOUNTER_H
|
|
|
|
|
|
|
|
#include "macros.h"
|
|
|
|
#include "language.h"
|
|
|
|
#include "stopwatch.h"
|
|
|
|
#include <avr/eeprom.h>
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
|
|
|
|
// Print debug messages with M111 S2
|
|
|
|
//#define DEBUG_PRINTCOUNTER
|
|
|
|
|
|
|
|
struct printStatistics { // 16 bytes (20 with real doubles)
|
2018-11-16 03:32:01 -06:00
|
|
|
//const uint8_t magic; // Magic header, it will always be 0x16
|
|
|
|
uint16_t totalPrints; // Number of prints
|
|
|
|
uint16_t finishedPrints; // Number of complete prints
|
|
|
|
uint32_t printTime; // Accumulated printing time
|
|
|
|
uint32_t longestPrint; // Longest successful print job
|
2019-01-31 05:25:28 -06:00
|
|
|
double filamentUsed; // Accumulated filament consumed in mm
|
2018-11-16 03:32:01 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
class PrintCounter: public Stopwatch {
|
|
|
|
private:
|
|
|
|
typedef Stopwatch super;
|
|
|
|
|
2019-01-31 05:25:28 -06:00
|
|
|
printStatistics data;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief EEPROM address
|
|
|
|
* @details Defines the start offset address where the data is stored.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
const uint16_t address = 0x32;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Interval in seconds between counter updates
|
|
|
|
* @details This const value defines what will be the time between each
|
|
|
|
* accumulator update. This is different from the EEPROM save interval.
|
|
|
|
*
|
|
|
|
* @note The max value for this option is 60(s), otherwise integer
|
|
|
|
* overflow will happen.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
const uint16_t updateInterval = 10;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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).
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
const uint16_t saveInterval = 3600;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Timestamp of the last call to deltaDuration()
|
2019-01-31 05:25:28 -06:00
|
|
|
* @details Stores the timestamp of the last deltaDuration(), this is
|
2018-11-16 03:32:01 -06:00
|
|
|
* required due to the updateInterval cycle.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
millis_t lastDuration;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Stats were loaded from EERPROM
|
2018-11-16 03:32:01 -06:00
|
|
|
* @details If set to true it indicates if the statistical data was already
|
|
|
|
* loaded from the EEPROM.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
bool loaded = false;
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* @brief dT since the last call
|
2019-01-31 05:25:28 -06:00
|
|
|
* @details Returns the elapsed time in seconds since the last call, this is
|
2018-11-16 03:32:01 -06:00
|
|
|
* used internally for print statistics accounting is not intended to be a
|
|
|
|
* user callable function.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
millis_t deltaDuration();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Class constructor
|
2018-11-16 03:32:01 -06:00
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
PrintCounter();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Checks if Print Statistics has been loaded
|
|
|
|
* @details Returns true if the statistical data has been loaded.
|
2018-11-16 03:32:01 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
bool isLoaded();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Increments the total filament used
|
2018-11-16 03:32:01 -06:00
|
|
|
* @details The total filament used counter will be incremented by "amount".
|
|
|
|
*
|
|
|
|
* @param amount The amount of filament used in mm
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
void incFilamentUsed(double const &amount);
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Resets the Print Statistics
|
|
|
|
* @details Resets the statistics to zero and saves them to EEPROM creating
|
2018-11-16 03:32:01 -06:00
|
|
|
* also the magic header.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
void initStats();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Loads the Print Statistics
|
|
|
|
* @details Loads the statistics from EEPROM
|
2018-11-16 03:32:01 -06:00
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
void loadStats();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Saves the Print Statistics
|
|
|
|
* @details Saves the statistics to EEPROM
|
2018-11-16 03:32:01 -06:00
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
void saveStats();
|
2018-11-16 03:32:01 -06: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.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
void showStats();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the currently loaded statistics
|
|
|
|
* @details Return the raw data, in the same structure used internally
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
printStatistics getStats() { return this->data; }
|
2018-11-16 03:32:01 -06: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.
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
void tick();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The following functions are being overridden
|
|
|
|
*/
|
2019-01-31 05:25:28 -06:00
|
|
|
bool start();
|
|
|
|
bool stop();
|
|
|
|
void reset();
|
2018-11-16 03:32:01 -06:00
|
|
|
|
|
|
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
|
|
|
|
|
/**
|
2019-01-31 05:25:28 -06:00
|
|
|
* @brief Prints a debug message
|
|
|
|
* @details Prints a simple debug message "PrintCounter::function"
|
2018-11-16 03:32:01 -06:00
|
|
|
*/
|
|
|
|
static void debug(const char func[]);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PRINTCOUNTER_H
|