My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

printcounter.cpp 5.7KB

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