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

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