Browse Source

PrintCounter EEPROM read/write optimizations

João Brázio 9 years ago
parent
commit
8fb23e899f
No account linked to committer's email address
2 changed files with 36 additions and 53 deletions
  1. 19
    44
      Marlin/printcounter.cpp
  2. 17
    9
      Marlin/printcounter.h

+ 19
- 44
Marlin/printcounter.cpp View File

@@ -28,6 +28,16 @@ PrintCounter::PrintCounter(): super() {
28 28
   this->loadStats();
29 29
 }
30 30
 
31
+uint16_t PrintCounter::deltaDuration() {
32
+  #if ENABLED(DEBUG_PRINTCOUNTER)
33
+    PrintCounter::debug(PSTR("deltaDuration"));
34
+  #endif
35
+
36
+  uint16_t tmp = this->lastDuration;
37
+  this->lastDuration = this->duration();
38
+  return this->lastDuration - tmp;
39
+}
40
+
31 41
 bool PrintCounter::isLoaded() {
32 42
   return this->loaded;
33 43
 }
@@ -38,9 +48,7 @@ void PrintCounter::initStats() {
38 48
   #endif
39 49
 
40 50
   this->loaded = true;
41
-  this->data = {
42
-    0, 0, 0, 0
43
-  };
51
+  this->data = { 0, 0, 0, 0 };
44 52
 
45 53
   this->saveStats();
46 54
   eeprom_write_byte((uint8_t*) this->addr, 0x16);
@@ -51,28 +59,9 @@ void PrintCounter::loadStats() {
51 59
     PrintCounter::debug(PSTR("loadStats"));
52 60
   #endif
53 61
 
54
-  uint16_t addr = this->addr;
55
-
56 62
   // Checks if the EEPROM block is initialized
57
-  if (eeprom_read_byte((uint8_t*) addr) != 0x16) this->initStats();
58
-
59
-  else {
60
-    // Skip the magic header byte
61
-    addr += sizeof(uint8_t);
62
-
63
-    // @todo This section will need rewrite once the ConfigurationStore
64
-    // and/or PersistentStorage object comes along.
65
-    this->data.totalPrints = eeprom_read_word((uint16_t*) addr);
66
-    addr += sizeof(uint16_t);
67
-
68
-    this->data.finishedPrints = eeprom_read_word((uint16_t*) addr);
69
-    addr += sizeof(uint16_t);
70
-
71
-    this->data.printTime = eeprom_read_dword((uint32_t*) addr);
72
-    addr += sizeof(uint32_t);
73
-
74
-    this->data.longestPrint = eeprom_read_dword((uint32_t*) addr);
75
-  }
63
+  if (eeprom_read_byte((uint8_t*) this->addr) != 0x16) this->initStats();
64
+  else eeprom_read_block(&this->data, (void *)(this->addr + sizeof(uint8_t)), sizeof(printStatistics));
76 65
 
77 66
   this->loaded = true;
78 67
 }
@@ -85,21 +74,7 @@ void PrintCounter::saveStats() {
85 74
   // Refuses to save data is object is not loaded
86 75
   if (!this->isLoaded()) return;
87 76
 
88
-  // Skip the magic header byte
89
-  uint16_t addr = this->addr + sizeof(uint8_t);
90
-
91
-  // @todo This section will need rewrite once the ConfigurationStore
92
-  // and/or PersistentStorage object comes along.
93
-  eeprom_write_word ((uint16_t*) addr, this->data.totalPrints);
94
-  addr += sizeof(uint16_t);
95
-
96
-  eeprom_write_word ((uint16_t*) addr, this->data.finishedPrints);
97
-  addr += sizeof(uint16_t);
98
-
99
-  eeprom_write_dword((uint32_t*) addr, this->data.printTime);
100
-  addr += sizeof(uint32_t);
101
-
102
-  eeprom_write_dword((uint32_t*) addr, this->data.longestPrint);
77
+  eeprom_write_block(&this->data, (void *)(this->addr + sizeof(uint8_t)), sizeof(printStatistics));
103 78
 }
104 79
 
105 80
 void PrintCounter::showStats() {
@@ -110,7 +85,8 @@ void PrintCounter::showStats() {
110 85
   SERIAL_ECHO(this->data.finishedPrints);
111 86
 
112 87
   SERIAL_ECHOPGM(", Failed: ");
113
-  SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints);
88
+  SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints
89
+    - (this->isRunning() || this->isPaused()) ? 1 : 0); // Removes 1 from failures with an active counter
114 90
 
115 91
   uint32_t t = this->data.printTime /60;
116 92
   SERIAL_ECHOPGM(", Total print time: ");
@@ -148,8 +124,7 @@ void PrintCounter::tick() {
148 124
     #endif
149 125
 
150 126
     uint16_t t = this->duration();;
151
-    this->data.printTime += t - this->lastUpdate;
152
-    this->lastUpdate = t;
127
+    this->data.printTime += this->deltaDuration();
153 128
     update_before = now;
154 129
   }
155 130
 
@@ -178,7 +153,7 @@ void PrintCounter::stop() {
178 153
 
179 154
   super::stop();
180 155
   this->data.finishedPrints++;
181
-  this->data.printTime += this->duration() - this->lastUpdate;
156
+  this->data.printTime += this->deltaDuration();
182 157
   this->saveStats();
183 158
 }
184 159
 
@@ -187,7 +162,7 @@ void PrintCounter::reset() {
187 162
     PrintCounter::debug(PSTR("stop"));
188 163
   #endif
189 164
 
190
-  this->lastUpdate = 0;
165
+  this->lastDuration = 0;
191 166
   super::reset();
192 167
 }
193 168
 

+ 17
- 9
Marlin/printcounter.h View File

@@ -30,7 +30,7 @@
30 30
 //#define DEBUG_PRINTCOUNTER
31 31
 
32 32
 struct printStatistics {    // 13 bytes
33
-  //uint8_t  magic;         // Magic header, it will always be 0x16
33
+  //const uint8_t magic;    // Magic header, it will always be 0x16
34 34
   uint16_t totalPrints;     // Number of prints
35 35
   uint16_t finishedPrints;  // Number of complete prints
36 36
   uint32_t printTime;       // Total printing time
@@ -44,14 +44,6 @@ class PrintCounter: public Stopwatch {
44 44
     printStatistics data;
45 45
 
46 46
     /**
47
-     * @brief Timestamp of the last update
48
-     * @details Stores the timestamp of the last data.pritnTime update, when the
49
-     * print job finishes, this will be used to calculate the exact time elapsed,
50
-     * this is required due to the updateInterval cycle.
51
-     */
52
-    uint16_t lastUpdate;
53
-
54
-    /**
55 47
      * @brief EEPROM address
56 48
      * @details Defines the start offset address where the data is stored.
57 49
      */
@@ -74,12 +66,28 @@ class PrintCounter: public Stopwatch {
74 66
     const uint16_t saveInterval = 3600;
75 67
 
76 68
     /**
69
+     * @brief Timestamp of the last call to deltaDuration()
70
+     * @details Stores the timestamp of the last deltaDuration(), this is
71
+     * required due to the updateInterval cycle.
72
+     */
73
+    uint16_t lastDuration;
74
+
75
+    /**
77 76
      * @brief Stats were loaded from EERPROM
78 77
      * @details If set to true it indicates if the statistical data was already
79 78
      * loaded from the EEPROM.
80 79
      */
81 80
     bool loaded = false;
82 81
 
82
+  protected:
83
+    /**
84
+     * @brief dT since the last call
85
+     * @details Returns the elapsed time in seconds since the last call, this is
86
+     * used internally for print statistics accounting is not intended to be a
87
+     * user callable function.
88
+     */
89
+    uint16_t deltaDuration();
90
+
83 91
   public:
84 92
     /**
85 93
      * @brief Class constructor

Loading…
Cancel
Save