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
   this->loadStats();
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
 bool PrintCounter::isLoaded() {
41
 bool PrintCounter::isLoaded() {
32
   return this->loaded;
42
   return this->loaded;
33
 }
43
 }
38
   #endif
48
   #endif
39
 
49
 
40
   this->loaded = true;
50
   this->loaded = true;
41
-  this->data = {
42
-    0, 0, 0, 0
43
-  };
51
+  this->data = { 0, 0, 0, 0 };
44
 
52
 
45
   this->saveStats();
53
   this->saveStats();
46
   eeprom_write_byte((uint8_t*) this->addr, 0x16);
54
   eeprom_write_byte((uint8_t*) this->addr, 0x16);
51
     PrintCounter::debug(PSTR("loadStats"));
59
     PrintCounter::debug(PSTR("loadStats"));
52
   #endif
60
   #endif
53
 
61
 
54
-  uint16_t addr = this->addr;
55
-
56
   // Checks if the EEPROM block is initialized
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
   this->loaded = true;
66
   this->loaded = true;
78
 }
67
 }
85
   // Refuses to save data is object is not loaded
74
   // Refuses to save data is object is not loaded
86
   if (!this->isLoaded()) return;
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
 void PrintCounter::showStats() {
80
 void PrintCounter::showStats() {
110
   SERIAL_ECHO(this->data.finishedPrints);
85
   SERIAL_ECHO(this->data.finishedPrints);
111
 
86
 
112
   SERIAL_ECHOPGM(", Failed: ");
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
   uint32_t t = this->data.printTime /60;
91
   uint32_t t = this->data.printTime /60;
116
   SERIAL_ECHOPGM(", Total print time: ");
92
   SERIAL_ECHOPGM(", Total print time: ");
148
     #endif
124
     #endif
149
 
125
 
150
     uint16_t t = this->duration();;
126
     uint16_t t = this->duration();;
151
-    this->data.printTime += t - this->lastUpdate;
152
-    this->lastUpdate = t;
127
+    this->data.printTime += this->deltaDuration();
153
     update_before = now;
128
     update_before = now;
154
   }
129
   }
155
 
130
 
178
 
153
 
179
   super::stop();
154
   super::stop();
180
   this->data.finishedPrints++;
155
   this->data.finishedPrints++;
181
-  this->data.printTime += this->duration() - this->lastUpdate;
156
+  this->data.printTime += this->deltaDuration();
182
   this->saveStats();
157
   this->saveStats();
183
 }
158
 }
184
 
159
 
187
     PrintCounter::debug(PSTR("stop"));
162
     PrintCounter::debug(PSTR("stop"));
188
   #endif
163
   #endif
189
 
164
 
190
-  this->lastUpdate = 0;
165
+  this->lastDuration = 0;
191
   super::reset();
166
   super::reset();
192
 }
167
 }
193
 
168
 

+ 17
- 9
Marlin/printcounter.h View File

30
 //#define DEBUG_PRINTCOUNTER
30
 //#define DEBUG_PRINTCOUNTER
31
 
31
 
32
 struct printStatistics {    // 13 bytes
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
   uint16_t totalPrints;     // Number of prints
34
   uint16_t totalPrints;     // Number of prints
35
   uint16_t finishedPrints;  // Number of complete prints
35
   uint16_t finishedPrints;  // Number of complete prints
36
   uint32_t printTime;       // Total printing time
36
   uint32_t printTime;       // Total printing time
44
     printStatistics data;
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
      * @brief EEPROM address
47
      * @brief EEPROM address
56
      * @details Defines the start offset address where the data is stored.
48
      * @details Defines the start offset address where the data is stored.
57
      */
49
      */
74
     const uint16_t saveInterval = 3600;
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
      * @brief Stats were loaded from EERPROM
76
      * @brief Stats were loaded from EERPROM
78
      * @details If set to true it indicates if the statistical data was already
77
      * @details If set to true it indicates if the statistical data was already
79
      * loaded from the EEPROM.
78
      * loaded from the EEPROM.
80
      */
79
      */
81
     bool loaded = false;
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
   public:
91
   public:
84
     /**
92
     /**
85
      * @brief Class constructor
93
      * @brief Class constructor

Loading…
Cancel
Save