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

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