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

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