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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 <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. #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/persistent_store_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. #if ENABLED(DEBUG_PRINTCOUNTER)
  61. debug(PSTR("deltaDuration"));
  62. #endif
  63. millis_t tmp = lastDuration;
  64. lastDuration = duration();
  65. return lastDuration - tmp;
  66. }
  67. void PrintCounter::incFilamentUsed(float const &amount) {
  68. #if ENABLED(DEBUG_PRINTCOUNTER)
  69. debug(PSTR("incFilamentUsed"));
  70. #endif
  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. #if ENABLED(DEBUG_PRINTCOUNTER)
  77. debug(PSTR("initStats"));
  78. #endif
  79. loaded = true;
  80. data = { 0, 0, 0, 0, 0.0
  81. #if HAS_SERVICE_INTERVALS
  82. #if SERVICE_INTERVAL_1 > 0
  83. , SERVICE_INTERVAL_SEC_1
  84. #endif
  85. #if SERVICE_INTERVAL_2 > 0
  86. , SERVICE_INTERVAL_SEC_2
  87. #endif
  88. #if SERVICE_INTERVAL_3 > 0
  89. , SERVICE_INTERVAL_SEC_3
  90. #endif
  91. #endif
  92. };
  93. saveStats();
  94. persistentStore.access_start();
  95. persistentStore.write_data(address, (uint8_t)0x16);
  96. persistentStore.access_finish();
  97. }
  98. #if HAS_SERVICE_INTERVALS
  99. inline void _print_divider() { SERIAL_ECHO_MSG("============================================="); }
  100. inline bool _service_warn(const char * const msg) {
  101. _print_divider();
  102. SERIAL_ECHO_START();
  103. serialprintPGM(msg);
  104. SERIAL_ECHOLNPGM("!");
  105. _print_divider();
  106. return true;
  107. }
  108. #endif
  109. void PrintCounter::loadStats() {
  110. #if ENABLED(DEBUG_PRINTCOUNTER)
  111. debug(PSTR("loadStats"));
  112. #endif
  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_BUZZER && SERVICE_WARNING_BUZZES > 0
  135. if (doBuzz) for (int i = 0; i < SERVICE_WARNING_BUZZES; i++) BUZZ(200, 404);
  136. #else
  137. UNUSED(doBuzz);
  138. #endif
  139. #endif // HAS_SERVICE_INTERVALS
  140. }
  141. void PrintCounter::saveStats() {
  142. #if ENABLED(DEBUG_PRINTCOUNTER)
  143. debug(PSTR("saveStats"));
  144. #endif
  145. // Refuses to save data if object is not loaded
  146. if (!isLoaded()) return;
  147. // Saves the struct to EEPROM
  148. persistentStore.access_start();
  149. persistentStore.write_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics));
  150. persistentStore.access_finish();
  151. #if ENABLED(EXTENSIBLE_UI)
  152. ExtUI::onConfigurationStoreWritten(true);
  153. #endif
  154. }
  155. #if HAS_SERVICE_INTERVALS
  156. inline void _service_when(char buffer[], const char * const msg, const uint32_t when) {
  157. SERIAL_ECHOPGM(STR_STATS);
  158. serialprintPGM(msg);
  159. SERIAL_ECHOLNPAIR(" in ", duration_t(when).toString(buffer));
  160. }
  161. #endif
  162. void PrintCounter::showStats() {
  163. char buffer[21];
  164. SERIAL_ECHOPGM(STR_STATS);
  165. SERIAL_ECHOLNPAIR(
  166. "Prints: ", data.totalPrints,
  167. ", Finished: ", data.finishedPrints,
  168. ", Failed: ", data.totalPrints - data.finishedPrints
  169. - ((isRunning() || isPaused()) ? 1 : 0) // Remove 1 from failures with an active counter
  170. );
  171. SERIAL_ECHOPGM(STR_STATS);
  172. duration_t elapsed = data.printTime;
  173. elapsed.toString(buffer);
  174. SERIAL_ECHOPAIR("Total time: ", buffer);
  175. #if ENABLED(DEBUG_PRINTCOUNTER)
  176. SERIAL_ECHOPAIR(" (", data.printTime);
  177. SERIAL_CHAR(')');
  178. #endif
  179. elapsed = data.longestPrint;
  180. elapsed.toString(buffer);
  181. SERIAL_ECHOPAIR(", Longest job: ", buffer);
  182. #if ENABLED(DEBUG_PRINTCOUNTER)
  183. SERIAL_ECHOPAIR(" (", data.longestPrint);
  184. SERIAL_CHAR(')');
  185. #endif
  186. SERIAL_ECHOPAIR("\n" STR_STATS "Filament used: ", data.filamentUsed / 1000);
  187. SERIAL_CHAR('m');
  188. SERIAL_EOL();
  189. #if SERVICE_INTERVAL_1 > 0
  190. _service_when(buffer, PSTR(SERVICE_NAME_1), data.nextService1);
  191. #endif
  192. #if SERVICE_INTERVAL_2 > 0
  193. _service_when(buffer, PSTR(SERVICE_NAME_2), data.nextService2);
  194. #endif
  195. #if SERVICE_INTERVAL_3 > 0
  196. _service_when(buffer, PSTR(SERVICE_NAME_3), data.nextService3);
  197. #endif
  198. }
  199. void PrintCounter::tick() {
  200. if (!isRunning()) return;
  201. millis_t now = millis();
  202. static uint32_t update_next; // = 0
  203. if (ELAPSED(now, update_next)) {
  204. #if ENABLED(DEBUG_PRINTCOUNTER)
  205. debug(PSTR("tick"));
  206. #endif
  207. millis_t delta = deltaDuration();
  208. data.printTime += delta;
  209. #if SERVICE_INTERVAL_1 > 0
  210. data.nextService1 -= _MIN(delta, data.nextService1);
  211. #endif
  212. #if SERVICE_INTERVAL_2 > 0
  213. data.nextService2 -= _MIN(delta, data.nextService2);
  214. #endif
  215. #if SERVICE_INTERVAL_3 > 0
  216. data.nextService3 -= _MIN(delta, data.nextService3);
  217. #endif
  218. update_next = now + updateInterval * 1000;
  219. }
  220. static uint32_t eeprom_next; // = 0
  221. if (ELAPSED(now, eeprom_next)) {
  222. eeprom_next = now + saveInterval * 1000;
  223. saveStats();
  224. }
  225. }
  226. // @Override
  227. bool PrintCounter::start() {
  228. #if ENABLED(DEBUG_PRINTCOUNTER)
  229. debug(PSTR("start"));
  230. #endif
  231. bool paused = isPaused();
  232. if (super::start()) {
  233. if (!paused) {
  234. data.totalPrints++;
  235. lastDuration = 0;
  236. }
  237. return true;
  238. }
  239. return false;
  240. }
  241. // @Override
  242. bool PrintCounter::stop() {
  243. #if ENABLED(DEBUG_PRINTCOUNTER)
  244. debug(PSTR("stop"));
  245. #endif
  246. if (super::stop()) {
  247. data.finishedPrints++;
  248. data.printTime += deltaDuration();
  249. if (duration() > data.longestPrint)
  250. data.longestPrint = duration();
  251. saveStats();
  252. return true;
  253. }
  254. else return false;
  255. }
  256. // @Override
  257. void PrintCounter::reset() {
  258. #if ENABLED(DEBUG_PRINTCOUNTER)
  259. debug(PSTR("stop"));
  260. #endif
  261. super::reset();
  262. lastDuration = 0;
  263. }
  264. #if HAS_SERVICE_INTERVALS
  265. void PrintCounter::resetServiceInterval(const int index) {
  266. switch (index) {
  267. #if SERVICE_INTERVAL_1 > 0
  268. case 1: data.nextService1 = SERVICE_INTERVAL_SEC_1;
  269. #endif
  270. #if SERVICE_INTERVAL_2 > 0
  271. case 2: data.nextService2 = SERVICE_INTERVAL_SEC_2;
  272. #endif
  273. #if SERVICE_INTERVAL_3 > 0
  274. case 3: data.nextService3 = SERVICE_INTERVAL_SEC_3;
  275. #endif
  276. }
  277. saveStats();
  278. }
  279. bool PrintCounter::needsService(const int index) {
  280. switch (index) {
  281. #if SERVICE_INTERVAL_1 > 0
  282. case 1: return data.nextService1 == 0;
  283. #endif
  284. #if SERVICE_INTERVAL_2 > 0
  285. case 2: return data.nextService2 == 0;
  286. #endif
  287. #if SERVICE_INTERVAL_3 > 0
  288. case 3: return data.nextService3 == 0;
  289. #endif
  290. default: return false;
  291. }
  292. }
  293. #endif // HAS_SERVICE_INTERVALS
  294. #if ENABLED(DEBUG_PRINTCOUNTER)
  295. void PrintCounter::debug(const char func[]) {
  296. if (DEBUGGING(INFO)) {
  297. SERIAL_ECHOPGM("PrintCounter::");
  298. serialprintPGM(func);
  299. SERIAL_ECHOLNPGM("()");
  300. }
  301. }
  302. #endif
  303. #endif // PRINTCOUNTER