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

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