My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

printcounter.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #pragma once
  23. #include "../libs/stopwatch.h"
  24. #include "../libs/duration_t.h"
  25. #include "../inc/MarlinConfig.h"
  26. // Print debug messages with M111 S2
  27. //#define DEBUG_PRINTCOUNTER
  28. #if USE_REAL_EEPROM
  29. // round up address to next page boundary (assuming 32 byte pages)
  30. #define STATS_EEPROM_ADDRESS 0x40
  31. #else
  32. #define STATS_EEPROM_ADDRESS 0x32
  33. #endif
  34. struct printStatistics { // 16 bytes
  35. //const uint8_t magic; // Magic header, it will always be 0x16
  36. uint16_t totalPrints; // Number of prints
  37. uint16_t finishedPrints; // Number of complete prints
  38. uint32_t printTime; // Accumulated printing time
  39. uint32_t longestPrint; // Longest successful print job
  40. float filamentUsed; // Accumulated filament consumed in mm
  41. #if SERVICE_INTERVAL_1 > 0
  42. uint32_t nextService1; // Service intervals (or placeholders)
  43. #endif
  44. #if SERVICE_INTERVAL_2 > 0
  45. uint32_t nextService2;
  46. #endif
  47. #if SERVICE_INTERVAL_3 > 0
  48. uint32_t nextService3;
  49. #endif
  50. };
  51. class PrintCounter: public Stopwatch {
  52. private:
  53. typedef Stopwatch super;
  54. #if USE_REAL_EEPROM || defined(CPU_32_BIT)
  55. typedef uint32_t eeprom_address_t;
  56. #else
  57. typedef uint16_t eeprom_address_t;
  58. #endif
  59. static printStatistics data;
  60. /**
  61. * @brief EEPROM address
  62. * @details Defines the start offset address where the data is stored.
  63. */
  64. static const eeprom_address_t address;
  65. /**
  66. * @brief Interval in seconds between counter updates
  67. * @details This const value defines what will be the time between each
  68. * accumulator update. This is different from the EEPROM save interval.
  69. *
  70. * @note The max value for this option is 60(s), otherwise integer
  71. * overflow will happen.
  72. */
  73. static constexpr uint16_t updateInterval = 10;
  74. /**
  75. * @brief Interval in seconds between EEPROM saves
  76. * @details This const value defines what will be the time between each
  77. * EEPROM save cycle, the development team recommends to set this value
  78. * no lower than 3600 secs (1 hour).
  79. */
  80. static constexpr uint16_t saveInterval = 3600;
  81. /**
  82. * @brief Timestamp of the last call to deltaDuration()
  83. * @details Store the timestamp of the last deltaDuration(), this is
  84. * required due to the updateInterval cycle.
  85. */
  86. static millis_t lastDuration;
  87. /**
  88. * @brief Stats were loaded from EEPROM
  89. * @details If set to true it indicates if the statistical data was already
  90. * loaded from the EEPROM.
  91. */
  92. static bool loaded;
  93. protected:
  94. /**
  95. * @brief dT since the last call
  96. * @details Return the elapsed time in seconds since the last call, this is
  97. * used internally for print statistics accounting is not intended to be a
  98. * user callable function.
  99. */
  100. static millis_t deltaDuration();
  101. public:
  102. /**
  103. * @brief Initialize the print counter
  104. */
  105. static inline void init() {
  106. super::init();
  107. loadStats();
  108. }
  109. /**
  110. * @brief Check if Print Statistics has been loaded
  111. * @details Return true if the statistical data has been loaded.
  112. * @return bool
  113. */
  114. FORCE_INLINE static bool isLoaded() { return loaded; }
  115. /**
  116. * @brief Increment the total filament used
  117. * @details The total filament used counter will be incremented by "amount".
  118. *
  119. * @param amount The amount of filament used in mm
  120. */
  121. static void incFilamentUsed(float const &amount);
  122. /**
  123. * @brief Reset the Print Statistics
  124. * @details Reset the statistics to zero and saves them to EEPROM creating
  125. * also the magic header.
  126. */
  127. static void initStats();
  128. /**
  129. * @brief Load the Print Statistics
  130. * @details Load the statistics from EEPROM
  131. */
  132. static void loadStats();
  133. /**
  134. * @brief Save the Print Statistics
  135. * @details Save the statistics to EEPROM
  136. */
  137. static void saveStats();
  138. /**
  139. * @brief Serial output the Print Statistics
  140. * @details This function may change in the future, for now it directly
  141. * prints the statistical data to serial.
  142. */
  143. static void showStats();
  144. /**
  145. * @brief Return the currently loaded statistics
  146. * @details Return the raw data, in the same structure used internally
  147. */
  148. static printStatistics getStats() { return data; }
  149. /**
  150. * @brief Loop function
  151. * @details This function should be called at loop, it will take care of
  152. * periodically save the statistical data to EEPROM and do time keeping.
  153. */
  154. static void tick();
  155. /**
  156. * The following functions are being overridden
  157. */
  158. static bool start();
  159. static bool stop();
  160. static void reset();
  161. #if HAS_SERVICE_INTERVALS
  162. static void resetServiceInterval(const int index);
  163. static bool needsService(const int index);
  164. #endif
  165. #if ENABLED(DEBUG_PRINTCOUNTER)
  166. /**
  167. * @brief Print a debug message
  168. * @details Print a simple debug message
  169. */
  170. static void debug(const char func[]);
  171. #endif
  172. };
  173. // Global Print Job Timer instance
  174. #if ENABLED(PRINTCOUNTER)
  175. extern PrintCounter print_job_timer;
  176. #else
  177. extern Stopwatch print_job_timer;
  178. #endif