printcounter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifndef PRINTCOUNTER_H
  23. #define PRINTCOUNTER_H
  24. // Print debug messages with M111 S2
  25. //#define DEBUG_PRINTCOUNTER
  26. #include "macros.h"
  27. #include "language.h"
  28. #include "stopwatch.h"
  29. #include <avr/eeprom.h>
  30. struct printStatistics { // 16 bytes
  31. //const uint8_t magic; // Magic header, it will always be 0x16
  32. uint16_t totalPrints; // Number of prints
  33. uint16_t finishedPrints; // Number of complete prints
  34. uint32_t printTime; // Accumulated printing time
  35. uint32_t longestPrint; // Longest successful print job
  36. float filamentUsed; // Accumulated filament consumed in mm
  37. };
  38. class PrintCounter: public Stopwatch {
  39. private:
  40. typedef Stopwatch super;
  41. #if ENABLED(I2C_EEPROM) || ENABLED(SPI_EEPROM)
  42. typedef uint32_t promdress;
  43. #else
  44. typedef uint16_t promdress;
  45. #endif
  46. static printStatistics data;
  47. /**
  48. * @brief EEPROM address
  49. * @details Defines the start offset address where the data is stored.
  50. */
  51. static const promdress address;
  52. /**
  53. * @brief Interval in seconds between counter updates
  54. * @details This const value defines what will be the time between each
  55. * accumulator update. This is different from the EEPROM save interval.
  56. *
  57. * @note The max value for this option is 60(s), otherwise integer
  58. * overflow will happen.
  59. */
  60. static const uint16_t updateInterval;
  61. /**
  62. * @brief Interval in seconds between EEPROM saves
  63. * @details This const value defines what will be the time between each
  64. * EEPROM save cycle, the development team recommends to set this value
  65. * no lower than 3600 secs (1 hour).
  66. */
  67. static const uint16_t saveInterval;
  68. /**
  69. * @brief Timestamp of the last call to deltaDuration()
  70. * @details Store the timestamp of the last deltaDuration(), this is
  71. * required due to the updateInterval cycle.
  72. */
  73. static millis_t lastDuration;
  74. /**
  75. * @brief Stats were loaded from EEPROM
  76. * @details If set to true it indicates if the statistical data was already
  77. * loaded from the EEPROM.
  78. */
  79. static bool loaded;
  80. protected:
  81. /**
  82. * @brief dT since the last call
  83. * @details Return the elapsed time in seconds since the last call, this is
  84. * used internally for print statistics accounting is not intended to be a
  85. * user callable function.
  86. */
  87. static millis_t deltaDuration();
  88. public:
  89. /**
  90. * @brief Initialize the print counter
  91. */
  92. static inline void init() {
  93. super::init();
  94. loadStats();
  95. }
  96. /**
  97. * @brief Check if Print Statistics has been loaded
  98. * @details Return true if the statistical data has been loaded.
  99. * @return bool
  100. */
  101. FORCE_INLINE static bool isLoaded() { return loaded; }
  102. /**
  103. * @brief Increment the total filament used
  104. * @details The total filament used counter will be incremented by "amount".
  105. *
  106. * @param amount The amount of filament used in mm
  107. */
  108. static void incFilamentUsed(float const &amount);
  109. /**
  110. * @brief Reset the Print Statistics
  111. * @details Reset the statistics to zero and saves them to EEPROM creating
  112. * also the magic header.
  113. */
  114. static void initStats();
  115. /**
  116. * @brief Load the Print Statistics
  117. * @details Load the statistics from EEPROM
  118. */
  119. static void loadStats();
  120. /**
  121. * @brief Save the Print Statistics
  122. * @details Save the statistics to EEPROM
  123. */
  124. static void saveStats();
  125. /**
  126. * @brief Serial output the Print Statistics
  127. * @details This function may change in the future, for now it directly
  128. * prints the statistical data to serial.
  129. */
  130. static void showStats();
  131. /**
  132. * @brief Return the currently loaded statistics
  133. * @details Return the raw data, in the same structure used internally
  134. */
  135. static printStatistics getStats() { return data; }
  136. /**
  137. * @brief Loop function
  138. * @details This function should be called at loop, it will take care of
  139. * periodically save the statistical data to EEPROM and do time keeping.
  140. */
  141. static void tick();
  142. /**
  143. * The following functions are being overridden
  144. */
  145. static bool start();
  146. static bool stop();
  147. static void reset();
  148. #if ENABLED(DEBUG_PRINTCOUNTER)
  149. /**
  150. * @brief Print a debug message
  151. * @details Print a simple debug message
  152. */
  153. static void debug(const char func[]);
  154. #endif
  155. };
  156. // Global Print Job Timer instance
  157. #if ENABLED(PRINTCOUNTER)
  158. extern PrintCounter print_job_timer;
  159. #else
  160. extern Stopwatch print_job_timer;
  161. #endif
  162. #endif // PRINTCOUNTER_H