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

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