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

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