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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "../inc/MarlinConfig.h"
  23. #if DISABLED(PRINTCOUNTER)
  24. #include "../libs/stopwatch.h"
  25. Stopwatch print_job_timer; // Global Print Job Timer instance
  26. #else // PRINTCOUNTER
  27. #include "printcounter.h"
  28. #include "../Marlin.h"
  29. PrintCounter print_job_timer; // Global Print Job Timer instance
  30. printStatistics PrintCounter::data;
  31. const PrintCounter::promdress PrintCounter::address = STATS_EEPROM_ADDRESS;
  32. const uint16_t PrintCounter::updateInterval = 10;
  33. const uint16_t PrintCounter::saveInterval = 3600;
  34. millis_t PrintCounter::lastDuration;
  35. bool PrintCounter::loaded = false;
  36. millis_t PrintCounter::deltaDuration() {
  37. #if ENABLED(DEBUG_PRINTCOUNTER)
  38. debug(PSTR("deltaDuration"));
  39. #endif
  40. millis_t tmp = lastDuration;
  41. lastDuration = duration();
  42. return lastDuration - tmp;
  43. }
  44. void PrintCounter::incFilamentUsed(double const &amount) {
  45. #if ENABLED(DEBUG_PRINTCOUNTER)
  46. debug(PSTR("incFilamentUsed"));
  47. #endif
  48. // Refuses to update data if object is not loaded
  49. if (!isLoaded()) return;
  50. data.filamentUsed += amount; // mm
  51. }
  52. void PrintCounter::initStats() {
  53. #if ENABLED(DEBUG_PRINTCOUNTER)
  54. debug(PSTR("initStats"));
  55. #endif
  56. loaded = true;
  57. data = { 0, 0, 0, 0, 0.0 };
  58. saveStats();
  59. eeprom_write_byte((uint8_t*)address, 0x16);
  60. }
  61. void PrintCounter::loadStats() {
  62. #if ENABLED(DEBUG_PRINTCOUNTER)
  63. debug(PSTR("loadStats"));
  64. #endif
  65. // Checks if the EEPROM block is initialized
  66. if (eeprom_read_byte((uint8_t*)address) != 0x16) initStats();
  67. else eeprom_read_block(&data,
  68. (void*)(address + sizeof(uint8_t)), sizeof(printStatistics));
  69. loaded = true;
  70. }
  71. void PrintCounter::saveStats() {
  72. #if ENABLED(DEBUG_PRINTCOUNTER)
  73. debug(PSTR("saveStats"));
  74. #endif
  75. // Refuses to save data if object is not loaded
  76. if (!isLoaded()) return;
  77. // Saves the struct to EEPROM
  78. eeprom_update_block(&data,
  79. (void*)(address + sizeof(uint8_t)), sizeof(printStatistics));
  80. }
  81. void PrintCounter::showStats() {
  82. char buffer[21];
  83. SERIAL_PROTOCOLPGM(MSG_STATS);
  84. SERIAL_ECHOPGM("Prints: ");
  85. SERIAL_ECHO(data.totalPrints);
  86. SERIAL_ECHOPGM(", Finished: ");
  87. SERIAL_ECHO(data.finishedPrints);
  88. SERIAL_ECHOPGM(", Failed: "); // Note: Removes 1 from failures with an active counter
  89. SERIAL_ECHO(data.totalPrints - data.finishedPrints
  90. - ((isRunning() || isPaused()) ? 1 : 0));
  91. SERIAL_EOL();
  92. SERIAL_PROTOCOLPGM(MSG_STATS);
  93. duration_t elapsed = data.printTime;
  94. elapsed.toString(buffer);
  95. SERIAL_ECHOPGM("Total time: ");
  96. SERIAL_ECHO(buffer);
  97. #if ENABLED(DEBUG_PRINTCOUNTER)
  98. SERIAL_ECHOPGM(" (");
  99. SERIAL_ECHO(data.printTime);
  100. SERIAL_CHAR(')');
  101. #endif
  102. elapsed = data.longestPrint;
  103. elapsed.toString(buffer);
  104. SERIAL_ECHOPGM(", Longest job: ");
  105. SERIAL_ECHO(buffer);
  106. #if ENABLED(DEBUG_PRINTCOUNTER)
  107. SERIAL_ECHOPGM(" (");
  108. SERIAL_ECHO(data.longestPrint);
  109. SERIAL_CHAR(')');
  110. #endif
  111. SERIAL_EOL();
  112. SERIAL_PROTOCOLPGM(MSG_STATS);
  113. SERIAL_ECHOPGM("Filament used: ");
  114. SERIAL_ECHO(data.filamentUsed / 1000);
  115. SERIAL_CHAR('m');
  116. SERIAL_EOL();
  117. }
  118. void PrintCounter::tick() {
  119. if (!isRunning()) return;
  120. static uint32_t update_last = millis(),
  121. eeprom_last = millis();
  122. millis_t now = millis();
  123. // Trying to get the amount of calculations down to the bare min
  124. const static uint16_t i = updateInterval * 1000;
  125. if (now - update_last >= i) {
  126. #if ENABLED(DEBUG_PRINTCOUNTER)
  127. debug(PSTR("tick"));
  128. #endif
  129. data.printTime += deltaDuration();
  130. update_last = now;
  131. }
  132. // Trying to get the amount of calculations down to the bare min
  133. const static millis_t j = saveInterval * 1000;
  134. if (now - eeprom_last >= j) {
  135. eeprom_last = now;
  136. saveStats();
  137. }
  138. }
  139. // @Override
  140. bool PrintCounter::start() {
  141. #if ENABLED(DEBUG_PRINTCOUNTER)
  142. debug(PSTR("start"));
  143. #endif
  144. bool paused = isPaused();
  145. if (super::start()) {
  146. if (!paused) {
  147. data.totalPrints++;
  148. lastDuration = 0;
  149. }
  150. return true;
  151. }
  152. return false;
  153. }
  154. // @Override
  155. bool PrintCounter::stop() {
  156. #if ENABLED(DEBUG_PRINTCOUNTER)
  157. debug(PSTR("stop"));
  158. #endif
  159. if (super::stop()) {
  160. data.finishedPrints++;
  161. data.printTime += deltaDuration();
  162. if (duration() > data.longestPrint)
  163. data.longestPrint = duration();
  164. saveStats();
  165. return true;
  166. }
  167. else return false;
  168. }
  169. // @Override
  170. void PrintCounter::reset() {
  171. #if ENABLED(DEBUG_PRINTCOUNTER)
  172. debug(PSTR("stop"));
  173. #endif
  174. super::reset();
  175. lastDuration = 0;
  176. }
  177. #if ENABLED(DEBUG_PRINTCOUNTER)
  178. void PrintCounter::debug(const char func[]) {
  179. if (DEBUGGING(INFO)) {
  180. SERIAL_ECHOPGM("PrintCounter::");
  181. serialprintPGM(func);
  182. SERIAL_ECHOLNPGM("()");
  183. }
  184. }
  185. #endif
  186. #endif // PRINTCOUNTER