printcounter.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 "MarlinConfig.h"
  23. #if DISABLED(PRINTCOUNTER)
  24. #include "stopwatch.h"
  25. Stopwatch print_job_timer; // Global Print Job Timer instance
  26. #else // PRINTCOUNTER
  27. #include "printcounter.h"
  28. #include "duration_t.h"
  29. #include "Marlin.h"
  30. PrintCounter print_job_timer; // Global Print Job Timer instance
  31. #if ENABLED(I2C_EEPROM) || ENABLED(SPI_EEPROM)
  32. // round up address to next page boundary (assuming 32 byte pages)
  33. #define STATS_EEPROM_ADDRESS 0x40
  34. #else
  35. #define STATS_EEPROM_ADDRESS 0x32
  36. #endif
  37. const PrintCounter::promdress PrintCounter::address = STATS_EEPROM_ADDRESS;
  38. const uint16_t PrintCounter::updateInterval = 10;
  39. const uint16_t PrintCounter::saveInterval = 3600;
  40. printStatistics PrintCounter::data;
  41. millis_t PrintCounter::lastDuration;
  42. bool PrintCounter::loaded = false;
  43. millis_t PrintCounter::deltaDuration() {
  44. #if ENABLED(DEBUG_PRINTCOUNTER)
  45. debug(PSTR("deltaDuration"));
  46. #endif
  47. millis_t tmp = lastDuration;
  48. lastDuration = duration();
  49. return lastDuration - tmp;
  50. }
  51. void PrintCounter::incFilamentUsed(float const &amount) {
  52. #if ENABLED(DEBUG_PRINTCOUNTER)
  53. debug(PSTR("incFilamentUsed"));
  54. #endif
  55. // Refuses to update data if object is not loaded
  56. if (!isLoaded()) return;
  57. data.filamentUsed += amount; // mm
  58. }
  59. void PrintCounter::initStats() {
  60. #if ENABLED(DEBUG_PRINTCOUNTER)
  61. debug(PSTR("initStats"));
  62. #endif
  63. loaded = true;
  64. data = { 0, 0, 0, 0, 0.0 };
  65. saveStats();
  66. eeprom_write_byte((uint8_t*)address, 0x16);
  67. }
  68. void PrintCounter::loadStats() {
  69. #if ENABLED(DEBUG_PRINTCOUNTER)
  70. debug(PSTR("loadStats"));
  71. #endif
  72. // Checks if the EEPROM block is initialized
  73. if (eeprom_read_byte((uint8_t*)address) != 0x16) initStats();
  74. else eeprom_read_block(&data,
  75. (void*)(address + sizeof(uint8_t)), sizeof(printStatistics));
  76. loaded = true;
  77. }
  78. void PrintCounter::saveStats() {
  79. #if ENABLED(DEBUG_PRINTCOUNTER)
  80. debug(PSTR("saveStats"));
  81. #endif
  82. // Refuses to save data if object is not loaded
  83. if (!isLoaded()) return;
  84. // Saves the struct to EEPROM
  85. eeprom_update_block(&data,
  86. (void*)(address + sizeof(uint8_t)), sizeof(printStatistics));
  87. }
  88. void PrintCounter::showStats() {
  89. char buffer[21];
  90. SERIAL_PROTOCOLPGM(MSG_STATS);
  91. SERIAL_ECHOPGM("Prints: ");
  92. SERIAL_ECHO(data.totalPrints);
  93. SERIAL_ECHOPGM(", Finished: ");
  94. SERIAL_ECHO(data.finishedPrints);
  95. SERIAL_ECHOPGM(", Failed: "); // Note: Removes 1 from failures with an active counter
  96. SERIAL_ECHO(data.totalPrints - data.finishedPrints
  97. - ((isRunning() || isPaused()) ? 1 : 0));
  98. SERIAL_EOL();
  99. SERIAL_PROTOCOLPGM(MSG_STATS);
  100. duration_t elapsed = data.printTime;
  101. elapsed.toString(buffer);
  102. SERIAL_ECHOPGM("Total time: ");
  103. SERIAL_ECHO(buffer);
  104. #if ENABLED(DEBUG_PRINTCOUNTER)
  105. SERIAL_ECHOPGM(" (");
  106. SERIAL_ECHO(data.printTime);
  107. SERIAL_CHAR(')');
  108. #endif
  109. elapsed = data.longestPrint;
  110. elapsed.toString(buffer);
  111. SERIAL_ECHOPGM(", Longest job: ");
  112. SERIAL_ECHO(buffer);
  113. #if ENABLED(DEBUG_PRINTCOUNTER)
  114. SERIAL_ECHOPGM(" (");
  115. SERIAL_ECHO(data.longestPrint);
  116. SERIAL_CHAR(')');
  117. #endif
  118. SERIAL_EOL();
  119. SERIAL_PROTOCOLPGM(MSG_STATS);
  120. SERIAL_ECHOPGM("Filament used: ");
  121. SERIAL_ECHO(data.filamentUsed / 1000);
  122. SERIAL_CHAR('m');
  123. SERIAL_EOL();
  124. }
  125. void PrintCounter::tick() {
  126. if (!isRunning()) return;
  127. static uint32_t update_last = millis(),
  128. eeprom_last = millis();
  129. millis_t now = millis();
  130. // Trying to get the amount of calculations down to the bare min
  131. const static uint16_t i = updateInterval * 1000;
  132. if (now - update_last >= i) {
  133. #if ENABLED(DEBUG_PRINTCOUNTER)
  134. debug(PSTR("tick"));
  135. #endif
  136. data.printTime += deltaDuration();
  137. update_last = now;
  138. }
  139. // Trying to get the amount of calculations down to the bare min
  140. const static millis_t j = saveInterval * 1000;
  141. if (now - eeprom_last >= j) {
  142. eeprom_last = now;
  143. saveStats();
  144. }
  145. }
  146. // @Override
  147. bool PrintCounter::start() {
  148. #if ENABLED(DEBUG_PRINTCOUNTER)
  149. debug(PSTR("start"));
  150. #endif
  151. bool paused = isPaused();
  152. if (super::start()) {
  153. if (!paused) {
  154. data.totalPrints++;
  155. lastDuration = 0;
  156. }
  157. return true;
  158. }
  159. return false;
  160. }
  161. // @Override
  162. bool PrintCounter::stop() {
  163. #if ENABLED(DEBUG_PRINTCOUNTER)
  164. debug(PSTR("stop"));
  165. #endif
  166. if (super::stop()) {
  167. data.finishedPrints++;
  168. data.printTime += deltaDuration();
  169. if (duration() > data.longestPrint)
  170. data.longestPrint = duration();
  171. saveStats();
  172. return true;
  173. }
  174. else return false;
  175. }
  176. // @Override
  177. void PrintCounter::reset() {
  178. #if ENABLED(DEBUG_PRINTCOUNTER)
  179. debug(PSTR("stop"));
  180. #endif
  181. super::reset();
  182. lastDuration = 0;
  183. }
  184. #if ENABLED(DEBUG_PRINTCOUNTER)
  185. void PrintCounter::debug(const char func[]) {
  186. if (DEBUGGING(INFO)) {
  187. SERIAL_ECHOPGM("PrintCounter::");
  188. serialprintPGM(func);
  189. SERIAL_ECHOLNPGM("()");
  190. }
  191. }
  192. #endif
  193. #endif // PRINTCOUNTER