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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. bool PrintCounter::isLoaded() {
  29. return this->loaded;
  30. }
  31. void PrintCounter::initStats() {
  32. #if ENABLED(DEBUG_PRINTCOUNTER)
  33. PrintCounter::debug(PSTR("initStats"));
  34. #endif
  35. this->loaded = true;
  36. this->data = {
  37. 0, 0, 0, 0
  38. };
  39. this->saveStats();
  40. eeprom_write_byte((uint8_t*) this->addr, 0x16);
  41. }
  42. void PrintCounter::loadStats() {
  43. #if ENABLED(DEBUG_PRINTCOUNTER)
  44. PrintCounter::debug(PSTR("loadStats"));
  45. #endif
  46. uint16_t addr = this->addr;
  47. // Checks if the EEPROM block is initialized
  48. if (eeprom_read_byte((uint8_t*) addr) != 0x16) this->initStats();
  49. else {
  50. // Skip the magic header byte
  51. addr += sizeof(uint8_t);
  52. // @todo This section will need rewrite once the ConfigurationStore
  53. // and/or PersistentStorage object comes along.
  54. this->data.totalPrints = eeprom_read_word((uint16_t*) addr);
  55. addr += sizeof(uint16_t);
  56. this->data.finishedPrints = eeprom_read_word((uint16_t*) addr);
  57. addr += sizeof(uint16_t);
  58. this->data.printTime = eeprom_read_dword((uint32_t*) addr);
  59. addr += sizeof(uint32_t);
  60. this->data.longestPrint = eeprom_read_dword((uint32_t*) addr);
  61. }
  62. this->loaded = true;
  63. }
  64. void PrintCounter::saveStats() {
  65. #if ENABLED(DEBUG_PRINTCOUNTER)
  66. PrintCounter::debug(PSTR("saveStats"));
  67. #endif
  68. // Refuses to save data is object is not loaded
  69. if (!this->isLoaded()) return;
  70. // Skip the magic header byte
  71. uint16_t addr = this->addr + sizeof(uint8_t);
  72. // @todo This section will need rewrite once the ConfigurationStore
  73. // and/or PersistentStorage object comes along.
  74. eeprom_write_word ((uint16_t*) addr, this->data.totalPrints);
  75. addr += sizeof(uint16_t);
  76. eeprom_write_word ((uint16_t*) addr, this->data.finishedPrints);
  77. addr += sizeof(uint16_t);
  78. eeprom_write_dword((uint32_t*) addr, this->data.printTime);
  79. addr += sizeof(uint32_t);
  80. eeprom_write_dword((uint32_t*) addr, this->data.longestPrint);
  81. }
  82. void PrintCounter::showStats() {
  83. SERIAL_ECHOPGM("Print statistics: Total: ");
  84. SERIAL_ECHO(this->data.totalPrints);
  85. SERIAL_ECHOPGM(", Finished: ");
  86. SERIAL_ECHO(this->data.finishedPrints);
  87. SERIAL_ECHOPGM(", Failed: ");
  88. SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints);
  89. uint32_t t = this->data.printTime /60;
  90. SERIAL_ECHOPGM(", Total print time: ");
  91. SERIAL_ECHO(t / 60);
  92. SERIAL_ECHOPGM("h ");
  93. SERIAL_ECHO(t % 60);
  94. SERIAL_ECHOPGM("min");
  95. #if ENABLED(DEBUG_PRINTCOUNTER)
  96. SERIAL_ECHOPGM(" (");
  97. SERIAL_ECHO(this->data.printTime);
  98. SERIAL_ECHOPGM(")");
  99. #endif
  100. // @todo longestPrint missing implementation
  101. SERIAL_EOL;
  102. }
  103. void PrintCounter::tick() {
  104. if (!this->isRunning()) return;
  105. static uint32_t update_before = millis(),
  106. eeprom_before = millis();
  107. uint32_t now = millis();
  108. // Trying to get the amount of calculations down to the bare min
  109. const static uint16_t i = this->updateInterval * 1000;
  110. if (now - update_before >= i) {
  111. #if ENABLED(DEBUG_PRINTCOUNTER)
  112. PrintCounter::debug(PSTR("tick"));
  113. #endif
  114. uint16_t t = this->duration();;
  115. this->data.printTime += t - this->lastUpdate;
  116. this->lastUpdate = t;
  117. update_before = now;
  118. }
  119. // Trying to get the amount of calculations down to the bare min
  120. const static uint16_t j = this->saveInterval * 1000;
  121. if (now - eeprom_before >= j) {
  122. eeprom_before = now;
  123. this->saveStats();
  124. }
  125. }
  126. void PrintCounter::start() {
  127. #if ENABLED(DEBUG_PRINTCOUNTER)
  128. PrintCounter::debug(PSTR("start"));
  129. #endif
  130. if (!this->isPaused()) this->data.totalPrints++;
  131. super::start();
  132. }
  133. void PrintCounter::stop() {
  134. #if ENABLED(DEBUG_PRINTCOUNTER)
  135. PrintCounter::debug(PSTR("stop"));
  136. #endif
  137. super::stop();
  138. this->data.finishedPrints++;
  139. this->data.printTime += this->duration() - this->lastUpdate;
  140. this->saveStats();
  141. }
  142. void PrintCounter::reset() {
  143. #if ENABLED(DEBUG_PRINTCOUNTER)
  144. PrintCounter::debug(PSTR("stop"));
  145. #endif
  146. this->lastUpdate = 0;
  147. super::reset();
  148. }
  149. #if ENABLED(DEBUG_PRINTCOUNTER)
  150. void PrintCounter::debug(const char func[]) {
  151. if (DEBUGGING(INFO)) {
  152. SERIAL_ECHOPGM("PrintCounter::");
  153. serialprintPGM(func);
  154. SERIAL_ECHOLNPGM("()");
  155. }
  156. }
  157. #endif