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.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. #include <avr/eeprom.h>
  25. PrintCounter::PrintCounter(): super() {
  26. this->loadStats();
  27. }
  28. uint16_t PrintCounter::deltaDuration() {
  29. #if ENABLED(DEBUG_PRINTCOUNTER)
  30. PrintCounter::debug(PSTR("deltaDuration"));
  31. #endif
  32. uint16_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::initStats() {
  40. #if ENABLED(DEBUG_PRINTCOUNTER)
  41. PrintCounter::debug(PSTR("initStats"));
  42. #endif
  43. this->loaded = true;
  44. this->data = { 0, 0, 0, 0 };
  45. this->saveStats();
  46. eeprom_write_byte((uint8_t *) this->address, 0x16);
  47. }
  48. void PrintCounter::loadStats() {
  49. #if ENABLED(DEBUG_PRINTCOUNTER)
  50. PrintCounter::debug(PSTR("loadStats"));
  51. #endif
  52. // Checks if the EEPROM block is initialized
  53. if (eeprom_read_byte((uint8_t *) this->address) != 0x16) this->initStats();
  54. else eeprom_read_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
  55. this->loaded = true;
  56. }
  57. void PrintCounter::saveStats() {
  58. #if ENABLED(DEBUG_PRINTCOUNTER)
  59. PrintCounter::debug(PSTR("saveStats"));
  60. #endif
  61. // Refuses to save data is object is not loaded
  62. if (!this->isLoaded()) return;
  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. uint16_t t = this->duration();;
  99. this->data.printTime += this->deltaDuration();
  100. update_before = now;
  101. }
  102. // Trying to get the amount of calculations down to the bare min
  103. const static uint16_t j = this->saveInterval * 1000;
  104. if (now - eeprom_before >= j) {
  105. eeprom_before = now;
  106. this->saveStats();
  107. }
  108. }
  109. void PrintCounter::start() {
  110. #if ENABLED(DEBUG_PRINTCOUNTER)
  111. PrintCounter::debug(PSTR("start"));
  112. #endif
  113. if (!this->isPaused()) this->data.totalPrints++;
  114. super::start();
  115. }
  116. void PrintCounter::stop() {
  117. #if ENABLED(DEBUG_PRINTCOUNTER)
  118. PrintCounter::debug(PSTR("stop"));
  119. #endif
  120. super::stop();
  121. this->data.finishedPrints++;
  122. this->data.printTime += this->deltaDuration();
  123. this->saveStats();
  124. }
  125. void PrintCounter::reset() {
  126. #if ENABLED(DEBUG_PRINTCOUNTER)
  127. PrintCounter::debug(PSTR("stop"));
  128. #endif
  129. this->lastDuration = 0;
  130. super::reset();
  131. }
  132. #if ENABLED(DEBUG_PRINTCOUNTER)
  133. void PrintCounter::debug(const char func[]) {
  134. if (DEBUGGING(INFO)) {
  135. SERIAL_ECHOPGM("PrintCounter::");
  136. serialprintPGM(func);
  137. SERIAL_ECHOLNPGM("()");
  138. }
  139. }
  140. #endif