|
@@ -24,7 +24,112 @@
|
24
|
24
|
#include "printcounter.h"
|
25
|
25
|
#include <avr/eeprom.h>
|
26
|
26
|
|
27
|
|
-PrintCounter::PrintCounter(): super() {}
|
|
27
|
+PrintCounter::PrintCounter(): super() {
|
|
28
|
+ this->loadStats();
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+bool PrintCounter::isLoaded() {
|
|
32
|
+ return this->loaded;
|
|
33
|
+}
|
|
34
|
+
|
|
35
|
+void PrintCounter::initStats() {
|
|
36
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
37
|
+ PrintCounter::debug(PSTR("initStats"));
|
|
38
|
+ #endif
|
|
39
|
+
|
|
40
|
+ this->loaded = true;
|
|
41
|
+ this->data = {
|
|
42
|
+ 0, 0, 0, 0
|
|
43
|
+ };
|
|
44
|
+
|
|
45
|
+ this->saveStats();
|
|
46
|
+ eeprom_write_byte((uint8_t*) this->addr, 0x16);
|
|
47
|
+}
|
|
48
|
+
|
|
49
|
+void PrintCounter::loadStats() {
|
|
50
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
51
|
+ PrintCounter::debug(PSTR("loadStats"));
|
|
52
|
+ #endif
|
|
53
|
+
|
|
54
|
+ uint16_t addr = this->addr;
|
|
55
|
+
|
|
56
|
+ // 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
|
+ }
|
|
76
|
+
|
|
77
|
+ this->loaded = true;
|
|
78
|
+}
|
|
79
|
+
|
|
80
|
+void PrintCounter::saveStats() {
|
|
81
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
82
|
+ PrintCounter::debug(PSTR("saveStats"));
|
|
83
|
+ #endif
|
|
84
|
+
|
|
85
|
+ // Refuses to save data is object is not loaded
|
|
86
|
+ if (!this->isLoaded()) return;
|
|
87
|
+
|
|
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);
|
|
103
|
+}
|
|
104
|
+
|
|
105
|
+void PrintCounter::showStats() {
|
|
106
|
+ SERIAL_ECHOPGM("Print statistics: Total: ");
|
|
107
|
+ SERIAL_ECHO(this->data.totalPrints);
|
|
108
|
+
|
|
109
|
+ SERIAL_ECHOPGM(", Finished: ");
|
|
110
|
+ SERIAL_ECHO(this->data.finishedPrints);
|
|
111
|
+
|
|
112
|
+ SERIAL_ECHOPGM(", Failed: ");
|
|
113
|
+ SERIAL_ECHO(this->data.totalPrints - this->data.finishedPrints);
|
|
114
|
+
|
|
115
|
+ uint32_t t = this->data.printTime /60;
|
|
116
|
+ SERIAL_ECHOPGM(", Total print time: ");
|
|
117
|
+ SERIAL_ECHO(t / 60);
|
|
118
|
+
|
|
119
|
+ SERIAL_ECHOPGM("h ");
|
|
120
|
+ SERIAL_ECHO(t % 60);
|
|
121
|
+
|
|
122
|
+ SERIAL_ECHOPGM("min");
|
|
123
|
+
|
|
124
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
125
|
+ SERIAL_ECHOPGM(" (");
|
|
126
|
+ SERIAL_ECHO(this->data.printTime);
|
|
127
|
+ SERIAL_ECHOPGM(")");
|
|
128
|
+ #endif
|
|
129
|
+
|
|
130
|
+ // @todo longestPrint missing implementation
|
|
131
|
+ SERIAL_EOL;
|
|
132
|
+}
|
28
|
133
|
|
29
|
134
|
void PrintCounter::tick() {
|
30
|
135
|
if (!this->isRunning()) return;
|
|
@@ -38,9 +143,14 @@ void PrintCounter::tick() {
|
38
|
143
|
const static uint16_t i = this->updateInterval * 1000;
|
39
|
144
|
|
40
|
145
|
if (now - update_before >= i) {
|
41
|
|
- //this->addToTimeCounter((uint16_t) (now - update_before) / 1000);
|
|
146
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
147
|
+ PrintCounter::debug(PSTR("tick"));
|
|
148
|
+ #endif
|
|
149
|
+
|
|
150
|
+ uint16_t t = this->duration();;
|
|
151
|
+ this->data.printTime += t - this->lastUpdate;
|
|
152
|
+ this->lastUpdate = t;
|
42
|
153
|
update_before = now;
|
43
|
|
- PrintCounter::debug(PSTR("tick1"));
|
44
|
154
|
}
|
45
|
155
|
|
46
|
156
|
// Trying to get the amount of calculations down to the bare min
|
|
@@ -48,52 +158,37 @@ void PrintCounter::tick() {
|
48
|
158
|
|
49
|
159
|
if (now - eeprom_before >= j) {
|
50
|
160
|
eeprom_before = now;
|
51
|
|
- this->save();
|
|
161
|
+ this->saveStats();
|
52
|
162
|
}
|
53
|
163
|
}
|
54
|
164
|
|
55
|
|
-void PrintCounter::load() {
|
56
|
|
- uint16_t pos = this->addr;
|
57
|
|
-
|
58
|
|
- this->data.successPrints= eeprom_read_word ((uint16_t*) pos);
|
59
|
|
- this->data.failedPrints = eeprom_read_word ((uint16_t*) pos);
|
60
|
|
- this->data.printTime = eeprom_read_dword((uint32_t*) pos);
|
61
|
|
- this->data.longestPrint = eeprom_read_dword((uint32_t*) pos);
|
62
|
|
-
|
63
|
|
- SERIAL_ECHOPGM("successPrints: ");
|
64
|
|
- SERIAL_ECHOLN(this->data.successPrints);
|
65
|
|
-
|
66
|
|
- SERIAL_ECHOPGM("failedPrints: ");
|
67
|
|
- SERIAL_ECHOLN(this->data.failedPrints);
|
68
|
|
-
|
69
|
|
- SERIAL_ECHOPGM("printTime: ");
|
70
|
|
- SERIAL_ECHOLN(this->data.printTime);
|
|
165
|
+void PrintCounter::start() {
|
|
166
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
167
|
+ PrintCounter::debug(PSTR("start"));
|
|
168
|
+ #endif
|
71
|
169
|
|
72
|
|
- SERIAL_ECHOPGM("longestPrint: ");
|
73
|
|
- SERIAL_ECHOLN(this->data.longestPrint);
|
|
170
|
+ if (!this->isPaused()) this->data.totalPrints++;
|
|
171
|
+ super::start();
|
74
|
172
|
}
|
75
|
173
|
|
76
|
|
-void PrintCounter::save() {
|
|
174
|
+void PrintCounter::stop() {
|
77
|
175
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|
78
|
|
- PrintCounter::debug(PSTR("save"));
|
|
176
|
+ PrintCounter::debug(PSTR("stop"));
|
79
|
177
|
#endif
|
80
|
178
|
|
81
|
|
- uint16_t pos = this->addr;
|
82
|
|
-
|
83
|
|
- eeprom_write_word ((uint16_t*) pos, this->data.successPrints);
|
84
|
|
- eeprom_write_word ((uint16_t*) pos, this->data.failedPrints);
|
85
|
|
- eeprom_write_dword((uint32_t*) pos, this->data.printTime);
|
86
|
|
- eeprom_write_dword((uint32_t*) pos, this->data.longestPrint);
|
|
179
|
+ super::stop();
|
|
180
|
+ this->data.finishedPrints++;
|
|
181
|
+ this->data.printTime += this->duration() - this->lastUpdate;
|
|
182
|
+ this->saveStats();
|
87
|
183
|
}
|
88
|
184
|
|
89
|
|
-void PrintCounter::start() {
|
90
|
|
- super::start();
|
91
|
|
- this->load();
|
92
|
|
-}
|
|
185
|
+void PrintCounter::reset() {
|
|
186
|
+ #if ENABLED(DEBUG_PRINTCOUNTER)
|
|
187
|
+ PrintCounter::debug(PSTR("stop"));
|
|
188
|
+ #endif
|
93
|
189
|
|
94
|
|
-void PrintCounter::stop() {
|
95
|
|
- super::stop();
|
96
|
|
- this->save();
|
|
190
|
+ this->lastUpdate = 0;
|
|
191
|
+ super::reset();
|
97
|
192
|
}
|
98
|
193
|
|
99
|
194
|
#if ENABLED(DEBUG_PRINTCOUNTER)
|