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

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