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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. uint16_t PrintCounter::deltaDuration() {
  28. #if ENABLED(DEBUG_PRINTCOUNTER)
  29. PrintCounter::debug(PSTR("deltaDuration"));
  30. #endif
  31. uint16_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::initStats() {
  39. #if ENABLED(DEBUG_PRINTCOUNTER)
  40. PrintCounter::debug(PSTR("initStats"));
  41. #endif
  42. this->loaded = true;
  43. this->data = { 0, 0, 0, 0 };
  44. this->saveStats();
  45. eeprom_write_byte((uint8_t *) this->address, 0x16);
  46. }
  47. void PrintCounter::loadStats() {
  48. #if ENABLED(DEBUG_PRINTCOUNTER)
  49. PrintCounter::debug(PSTR("loadStats"));
  50. #endif
  51. // Checks if the EEPROM block is initialized
  52. if (eeprom_read_byte((uint8_t *) this->address) != 0x16) this->initStats();
  53. else eeprom_read_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
  54. this->loaded = true;
  55. }
  56. void PrintCounter::saveStats() {
  57. #if ENABLED(DEBUG_PRINTCOUNTER)
  58. PrintCounter::debug(PSTR("saveStats"));
  59. #endif
  60. // Refuses to save data is object is not loaded
  61. if (!this->isLoaded()) return;
  62. // Saves the struct to EEPROM
  63. eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
  64. }
  65. void PrintCounter::showStats() {
  66. SERIAL_ECHOPGM("Print statistics: Total: ");
  67. SERIAL_ECHO(this->data.totalPrints);
  68. SERIAL_ECHOPGM(", Finished: ");
  69. SERIAL_ECHO(this->data.finishedPrints);
  70. SERIAL_ECHOPGM(", Failed: ");
  71. SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints
  72. - (this->isRunning() || this->isPaused()) ? 1 : 0); // Removes 1 from failures with an active counter
  73. uint32_t t = this->data.printTime /60;
  74. SERIAL_ECHOPGM(", Total print time: ");
  75. SERIAL_ECHO(t / 60);
  76. SERIAL_ECHOPGM("h ");
  77. SERIAL_ECHO(t % 60);
  78. SERIAL_ECHOPGM("min");
  79. #if ENABLED(DEBUG_PRINTCOUNTER)
  80. SERIAL_ECHOPGM(" (");
  81. SERIAL_ECHO(this->data.printTime);
  82. SERIAL_ECHOPGM(")");
  83. #endif
  84. // @todo longestPrint missing implementation
  85. SERIAL_EOL;
  86. }
  87. void PrintCounter::tick() {
  88. if (!this->isRunning()) return;
  89. static uint32_t update_before = millis(),
  90. eeprom_before = millis();
  91. uint32_t now = millis();
  92. // Trying to get the amount of calculations down to the bare min
  93. const static uint16_t i = this->updateInterval * 1000;
  94. if (now - update_before >= i) {
  95. #if ENABLED(DEBUG_PRINTCOUNTER)
  96. PrintCounter::debug(PSTR("tick"));
  97. #endif
  98. this->data.printTime += this->deltaDuration();
  99. update_before = now;
  100. }
  101. // Trying to get the amount of calculations down to the bare min
  102. const static uint16_t j = this->saveInterval * 1000;
  103. if (now - eeprom_before >= j) {
  104. eeprom_before = now;
  105. this->saveStats();
  106. }
  107. }
  108. // @Override
  109. bool PrintCounter::start() {
  110. #if ENABLED(DEBUG_PRINTCOUNTER)
  111. PrintCounter::debug(PSTR("start"));
  112. #endif
  113. bool paused = this->isPaused();
  114. if (super::start()) {
  115. if (!paused) {
  116. this->data.totalPrints++;
  117. this->lastDuration = 0;
  118. }
  119. return true;
  120. }
  121. else return false;
  122. }
  123. // @Override
  124. bool PrintCounter::stop() {
  125. #if ENABLED(DEBUG_PRINTCOUNTER)
  126. PrintCounter::debug(PSTR("stop"));
  127. #endif
  128. if (super::stop()) {
  129. this->data.finishedPrints++;
  130. this->data.printTime += this->deltaDuration();
  131. this->saveStats();
  132. }
  133. else return false;
  134. }
  135. // @Override
  136. void PrintCounter::reset() {
  137. #if ENABLED(DEBUG_PRINTCOUNTER)
  138. PrintCounter::debug(PSTR("stop"));
  139. #endif
  140. super::reset();
  141. this->lastDuration = 0;
  142. }
  143. #if ENABLED(DEBUG_PRINTCOUNTER)
  144. void PrintCounter::debug(const char func[]) {
  145. if (DEBUGGING(INFO)) {
  146. SERIAL_ECHOPGM("PrintCounter::");
  147. serialprintPGM(func);
  148. SERIAL_ECHOLNPGM("()");
  149. }
  150. }
  151. #endif