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 6.0KB

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