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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #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. #if ENABLED(EXTENSIBLE_UI)
  28. #include "../lcd/extui/ui_api.h"
  29. #endif
  30. #include "printcounter.h"
  31. #include "../MarlinCore.h"
  32. #include "../HAL/shared/eeprom_api.h"
  33. #if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0
  34. #include "../libs/buzzer.h"
  35. #endif
  36. #if PRINTCOUNTER_SYNC
  37. #include "../module/planner.h"
  38. #warning "To prevent step loss, motion will pause for PRINTCOUNTER auto-save."
  39. #endif
  40. // Service intervals
  41. #if HAS_SERVICE_INTERVALS
  42. #if SERVICE_INTERVAL_1 > 0
  43. #define SERVICE_INTERVAL_SEC_1 (3600UL * SERVICE_INTERVAL_1)
  44. #else
  45. #define SERVICE_INTERVAL_SEC_1 (3600UL * 100)
  46. #endif
  47. #if SERVICE_INTERVAL_2 > 0
  48. #define SERVICE_INTERVAL_SEC_2 (3600UL * SERVICE_INTERVAL_2)
  49. #else
  50. #define SERVICE_INTERVAL_SEC_2 (3600UL * 100)
  51. #endif
  52. #if SERVICE_INTERVAL_3 > 0
  53. #define SERVICE_INTERVAL_SEC_3 (3600UL * SERVICE_INTERVAL_3)
  54. #else
  55. #define SERVICE_INTERVAL_SEC_3 (3600UL * 100)
  56. #endif
  57. #endif
  58. PrintCounter print_job_timer; // Global Print Job Timer instance
  59. printStatistics PrintCounter::data;
  60. const PrintCounter::eeprom_address_t PrintCounter::address = STATS_EEPROM_ADDRESS;
  61. millis_t PrintCounter::lastDuration;
  62. bool PrintCounter::loaded = false;
  63. millis_t PrintCounter::deltaDuration() {
  64. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("deltaDuration")));
  65. millis_t tmp = lastDuration;
  66. lastDuration = duration();
  67. return lastDuration - tmp;
  68. }
  69. void PrintCounter::incFilamentUsed(float const &amount) {
  70. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("incFilamentUsed")));
  71. // Refuses to update data if object is not loaded
  72. if (!isLoaded()) return;
  73. data.filamentUsed += amount; // mm
  74. }
  75. void PrintCounter::initStats() {
  76. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("initStats")));
  77. loaded = true;
  78. data = { 0, 0, 0, 0, 0.0
  79. #if HAS_SERVICE_INTERVALS
  80. #if SERVICE_INTERVAL_1 > 0
  81. , SERVICE_INTERVAL_SEC_1
  82. #endif
  83. #if SERVICE_INTERVAL_2 > 0
  84. , SERVICE_INTERVAL_SEC_2
  85. #endif
  86. #if SERVICE_INTERVAL_3 > 0
  87. , SERVICE_INTERVAL_SEC_3
  88. #endif
  89. #endif
  90. };
  91. saveStats();
  92. persistentStore.access_start();
  93. persistentStore.write_data(address, (uint8_t)0x16);
  94. persistentStore.access_finish();
  95. }
  96. #if HAS_SERVICE_INTERVALS
  97. inline void _print_divider() { SERIAL_ECHO_MSG("============================================="); }
  98. inline bool _service_warn(const char * const msg) {
  99. _print_divider();
  100. SERIAL_ECHO_START();
  101. SERIAL_ECHOPGM_P(msg);
  102. SERIAL_ECHOLNPGM("!");
  103. _print_divider();
  104. return true;
  105. }
  106. #endif
  107. void PrintCounter::loadStats() {
  108. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("loadStats")));
  109. // Check if the EEPROM block is initialized
  110. uint8_t value = 0;
  111. persistentStore.access_start();
  112. persistentStore.read_data(address, &value, sizeof(uint8_t));
  113. if (value != 0x16)
  114. initStats();
  115. else
  116. persistentStore.read_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics));
  117. persistentStore.access_finish();
  118. loaded = true;
  119. #if HAS_SERVICE_INTERVALS
  120. bool doBuzz = false;
  121. #if SERVICE_INTERVAL_1 > 0
  122. if (data.nextService1 == 0) doBuzz = _service_warn(PSTR(" " SERVICE_NAME_1));
  123. #endif
  124. #if SERVICE_INTERVAL_2 > 0
  125. if (data.nextService2 == 0) doBuzz = _service_warn(PSTR(" " SERVICE_NAME_2));
  126. #endif
  127. #if SERVICE_INTERVAL_3 > 0
  128. if (data.nextService3 == 0) doBuzz = _service_warn(PSTR(" " SERVICE_NAME_3));
  129. #endif
  130. #if HAS_BUZZER && SERVICE_WARNING_BUZZES > 0
  131. if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) BUZZ(200, 404);
  132. #else
  133. UNUSED(doBuzz);
  134. #endif
  135. #endif // HAS_SERVICE_INTERVALS
  136. }
  137. void PrintCounter::saveStats() {
  138. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("saveStats")));
  139. // Refuses to save data if object is not loaded
  140. if (!isLoaded()) return;
  141. TERN_(PRINTCOUNTER_SYNC, planner.synchronize());
  142. // Saves the struct to EEPROM
  143. persistentStore.access_start();
  144. persistentStore.write_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics));
  145. persistentStore.access_finish();
  146. TERN_(EXTENSIBLE_UI, ExtUI::onConfigurationStoreWritten(true));
  147. }
  148. #if HAS_SERVICE_INTERVALS
  149. inline void _service_when(char buffer[], const char * const msg, const uint32_t when) {
  150. SERIAL_ECHOPGM(STR_STATS);
  151. SERIAL_ECHOPGM_P(msg);
  152. SERIAL_ECHOLNPAIR(" in ", duration_t(when).toString(buffer));
  153. }
  154. #endif
  155. void PrintCounter::showStats() {
  156. char buffer[22];
  157. SERIAL_ECHOPGM(STR_STATS);
  158. SERIAL_ECHOLNPAIR(
  159. "Prints: ", data.totalPrints,
  160. ", Finished: ", data.finishedPrints,
  161. ", Failed: ", data.totalPrints - data.finishedPrints
  162. - ((isRunning() || isPaused()) ? 1 : 0) // Remove 1 from failures with an active counter
  163. );
  164. SERIAL_ECHOPGM(STR_STATS);
  165. duration_t elapsed = data.printTime;
  166. elapsed.toString(buffer);
  167. SERIAL_ECHOPAIR("Total time: ", buffer);
  168. #if ENABLED(DEBUG_PRINTCOUNTER)
  169. SERIAL_ECHOPAIR(" (", data.printTime);
  170. SERIAL_CHAR(')');
  171. #endif
  172. elapsed = data.longestPrint;
  173. elapsed.toString(buffer);
  174. SERIAL_ECHOPAIR(", Longest job: ", buffer);
  175. #if ENABLED(DEBUG_PRINTCOUNTER)
  176. SERIAL_ECHOPAIR(" (", data.longestPrint);
  177. SERIAL_CHAR(')');
  178. #endif
  179. SERIAL_ECHOPAIR("\n" STR_STATS "Filament used: ", data.filamentUsed / 1000);
  180. SERIAL_CHAR('m');
  181. SERIAL_EOL();
  182. #if SERVICE_INTERVAL_1 > 0
  183. _service_when(buffer, PSTR(SERVICE_NAME_1), data.nextService1);
  184. #endif
  185. #if SERVICE_INTERVAL_2 > 0
  186. _service_when(buffer, PSTR(SERVICE_NAME_2), data.nextService2);
  187. #endif
  188. #if SERVICE_INTERVAL_3 > 0
  189. _service_when(buffer, PSTR(SERVICE_NAME_3), data.nextService3);
  190. #endif
  191. }
  192. void PrintCounter::tick() {
  193. if (!isRunning()) return;
  194. millis_t now = millis();
  195. static millis_t update_next; // = 0
  196. if (ELAPSED(now, update_next)) {
  197. update_next = now + updateInterval;
  198. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("tick")));
  199. millis_t delta = deltaDuration();
  200. data.printTime += delta;
  201. #if SERVICE_INTERVAL_1 > 0
  202. data.nextService1 -= _MIN(delta, data.nextService1);
  203. #endif
  204. #if SERVICE_INTERVAL_2 > 0
  205. data.nextService2 -= _MIN(delta, data.nextService2);
  206. #endif
  207. #if SERVICE_INTERVAL_3 > 0
  208. data.nextService3 -= _MIN(delta, data.nextService3);
  209. #endif
  210. }
  211. #if PRINTCOUNTER_SAVE_INTERVAL > 0
  212. static millis_t eeprom_next; // = 0
  213. if (ELAPSED(now, eeprom_next)) {
  214. eeprom_next = now + saveInterval;
  215. saveStats();
  216. }
  217. #endif
  218. }
  219. // @Override
  220. bool PrintCounter::start() {
  221. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("start")));
  222. bool paused = isPaused();
  223. if (super::start()) {
  224. if (!paused) {
  225. data.totalPrints++;
  226. lastDuration = 0;
  227. }
  228. return true;
  229. }
  230. return false;
  231. }
  232. bool PrintCounter::_stop(const bool completed) {
  233. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("stop")));
  234. const bool did_stop = super::stop();
  235. if (did_stop) {
  236. data.printTime += deltaDuration();
  237. if (completed) {
  238. data.finishedPrints++;
  239. if (duration() > data.longestPrint)
  240. data.longestPrint = duration();
  241. }
  242. }
  243. saveStats();
  244. return did_stop;
  245. }
  246. // @Override
  247. void PrintCounter::reset() {
  248. TERN_(DEBUG_PRINTCOUNTER, debug(PSTR("stop")));
  249. super::reset();
  250. lastDuration = 0;
  251. }
  252. #if HAS_SERVICE_INTERVALS
  253. void PrintCounter::resetServiceInterval(const int index) {
  254. switch (index) {
  255. #if SERVICE_INTERVAL_1 > 0
  256. case 1: data.nextService1 = SERVICE_INTERVAL_SEC_1;
  257. #endif
  258. #if SERVICE_INTERVAL_2 > 0
  259. case 2: data.nextService2 = SERVICE_INTERVAL_SEC_2;
  260. #endif
  261. #if SERVICE_INTERVAL_3 > 0
  262. case 3: data.nextService3 = SERVICE_INTERVAL_SEC_3;
  263. #endif
  264. }
  265. saveStats();
  266. }
  267. bool PrintCounter::needsService(const int index) {
  268. switch (index) {
  269. #if SERVICE_INTERVAL_1 > 0
  270. case 1: return data.nextService1 == 0;
  271. #endif
  272. #if SERVICE_INTERVAL_2 > 0
  273. case 2: return data.nextService2 == 0;
  274. #endif
  275. #if SERVICE_INTERVAL_3 > 0
  276. case 3: return data.nextService3 == 0;
  277. #endif
  278. default: return false;
  279. }
  280. }
  281. #endif // HAS_SERVICE_INTERVALS
  282. #if ENABLED(DEBUG_PRINTCOUNTER)
  283. void PrintCounter::debug(const char func[]) {
  284. if (DEBUGGING(INFO)) {
  285. SERIAL_ECHOPGM("PrintCounter::");
  286. SERIAL_ECHOPGM_P(func);
  287. SERIAL_ECHOLNPGM("()");
  288. }
  289. }
  290. #endif
  291. #endif // PRINTCOUNTER