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

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