My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

printcounter.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
  63. }
  64. void PrintCounter::showStats() {
  65. SERIAL_ECHOPGM("Print statistics: Total: ");
  66. SERIAL_ECHO(this->data.totalPrints);
  67. SERIAL_ECHOPGM(", Finished: ");
  68. SERIAL_ECHO(this->data.finishedPrints);
  69. SERIAL_ECHOPGM(", Failed: ");
  70. SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints
  71. - (this->isRunning() || this->isPaused()) ? 1 : 0); // Removes 1 from failures with an active counter
  72. uint32_t t = this->data.printTime /60;
  73. SERIAL_ECHOPGM(", Total print time: ");
  74. SERIAL_ECHO(t / 60);
  75. SERIAL_ECHOPGM("h ");
  76. SERIAL_ECHO(t % 60);
  77. SERIAL_ECHOPGM("min");
  78. #if ENABLED(DEBUG_PRINTCOUNTER)
  79. SERIAL_ECHOPGM(" (");
  80. SERIAL_ECHO(this->data.printTime);
  81. SERIAL_ECHOPGM(")");
  82. #endif
  83. // @todo longestPrint missing implementation
  84. SERIAL_EOL;
  85. }
  86. void PrintCounter::tick() {
  87. if (!this->isRunning()) return;
  88. static uint32_t update_before = millis(),
  89. eeprom_before = millis();
  90. uint32_t now = millis();
  91. // Trying to get the amount of calculations down to the bare min
  92. const static uint16_t i = this->updateInterval * 1000;
  93. if (now - update_before >= i) {
  94. #if ENABLED(DEBUG_PRINTCOUNTER)
  95. PrintCounter::debug(PSTR("tick"));
  96. #endif
  97. this->data.printTime += this->deltaDuration();
  98. update_before = now;
  99. }
  100. // Trying to get the amount of calculations down to the bare min
  101. const static uint16_t j = this->saveInterval * 1000;
  102. if (now - eeprom_before >= j) {
  103. eeprom_before = now;
  104. this->saveStats();
  105. }
  106. }
  107. void PrintCounter::start() {
  108. #if ENABLED(DEBUG_PRINTCOUNTER)
  109. PrintCounter::debug(PSTR("start"));
  110. #endif
  111. if (!this->isPaused()) this->data.totalPrints++;
  112. super::start();
  113. }
  114. void PrintCounter::stop() {
  115. #if ENABLED(DEBUG_PRINTCOUNTER)
  116. PrintCounter::debug(PSTR("stop"));
  117. #endif
  118. if (!this->isRunning()) return;
  119. super::stop();
  120. this->data.finishedPrints++;
  121. this->data.printTime += this->deltaDuration();
  122. this->saveStats();
  123. }
  124. void PrintCounter::reset() {
  125. #if ENABLED(DEBUG_PRINTCOUNTER)
  126. PrintCounter::debug(PSTR("stop"));
  127. #endif
  128. this->lastDuration = 0;
  129. super::reset();
  130. }
  131. #if ENABLED(DEBUG_PRINTCOUNTER)
  132. void PrintCounter::debug(const char func[]) {
  133. if (DEBUGGING(INFO)) {
  134. SERIAL_ECHOPGM("PrintCounter::");
  135. serialprintPGM(func);
  136. SERIAL_ECHOLNPGM("()");
  137. }
  138. }
  139. #endif