1 |
ron |
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 |
|
|
|
23 |
#ifndef PRINTCOUNTER_H
|
|
|
24 |
#define PRINTCOUNTER_H
|
|
|
25 |
|
|
|
26 |
// Print debug messages with M111 S2
|
|
|
27 |
//#define DEBUG_PRINTCOUNTER
|
|
|
28 |
|
|
|
29 |
#include "macros.h"
|
|
|
30 |
#include "language.h"
|
|
|
31 |
#include "stopwatch.h"
|
|
|
32 |
#include <avr/eeprom.h>
|
|
|
33 |
|
|
|
34 |
struct printStatistics { // 16 bytes
|
|
|
35 |
//const uint8_t magic; // Magic header, it will always be 0x16
|
|
|
36 |
uint16_t totalPrints; // Number of prints
|
|
|
37 |
uint16_t finishedPrints; // Number of complete prints
|
|
|
38 |
uint32_t printTime; // Accumulated printing time
|
|
|
39 |
uint32_t longestPrint; // Longest successful print job
|
|
|
40 |
float filamentUsed; // Accumulated filament consumed in mm
|
|
|
41 |
};
|
|
|
42 |
|
|
|
43 |
class PrintCounter: public Stopwatch {
|
|
|
44 |
private:
|
|
|
45 |
typedef Stopwatch super;
|
|
|
46 |
|
|
|
47 |
#if ENABLED(I2C_EEPROM) || ENABLED(SPI_EEPROM)
|
|
|
48 |
typedef uint32_t promdress;
|
|
|
49 |
#else
|
|
|
50 |
typedef uint16_t promdress;
|
|
|
51 |
#endif
|
|
|
52 |
|
|
|
53 |
static printStatistics data;
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @brief EEPROM address
|
|
|
57 |
* @details Defines the start offset address where the data is stored.
|
|
|
58 |
*/
|
|
|
59 |
static const promdress address;
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @brief Interval in seconds between counter updates
|
|
|
63 |
* @details This const value defines what will be the time between each
|
|
|
64 |
* accumulator update. This is different from the EEPROM save interval.
|
|
|
65 |
*
|
|
|
66 |
* @note The max value for this option is 60(s), otherwise integer
|
|
|
67 |
* overflow will happen.
|
|
|
68 |
*/
|
|
|
69 |
static const uint16_t updateInterval;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @brief Interval in seconds between EEPROM saves
|
|
|
73 |
* @details This const value defines what will be the time between each
|
|
|
74 |
* EEPROM save cycle, the development team recommends to set this value
|
|
|
75 |
* no lower than 3600 secs (1 hour).
|
|
|
76 |
*/
|
|
|
77 |
static const uint16_t saveInterval;
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* @brief Timestamp of the last call to deltaDuration()
|
|
|
81 |
* @details Store the timestamp of the last deltaDuration(), this is
|
|
|
82 |
* required due to the updateInterval cycle.
|
|
|
83 |
*/
|
|
|
84 |
static millis_t lastDuration;
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* @brief Stats were loaded from EEPROM
|
|
|
88 |
* @details If set to true it indicates if the statistical data was already
|
|
|
89 |
* loaded from the EEPROM.
|
|
|
90 |
*/
|
|
|
91 |
static bool loaded;
|
|
|
92 |
|
|
|
93 |
protected:
|
|
|
94 |
/**
|
|
|
95 |
* @brief dT since the last call
|
|
|
96 |
* @details Return the elapsed time in seconds since the last call, this is
|
|
|
97 |
* used internally for print statistics accounting is not intended to be a
|
|
|
98 |
* user callable function.
|
|
|
99 |
*/
|
|
|
100 |
static millis_t deltaDuration();
|
|
|
101 |
|
|
|
102 |
public:
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* @brief Initialize the print counter
|
|
|
106 |
*/
|
|
|
107 |
static inline void init() {
|
|
|
108 |
super::init();
|
|
|
109 |
loadStats();
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* @brief Check if Print Statistics has been loaded
|
|
|
114 |
* @details Return true if the statistical data has been loaded.
|
|
|
115 |
* @return bool
|
|
|
116 |
*/
|
|
|
117 |
FORCE_INLINE static bool isLoaded() { return loaded; }
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* @brief Increment the total filament used
|
|
|
121 |
* @details The total filament used counter will be incremented by "amount".
|
|
|
122 |
*
|
|
|
123 |
* @param amount The amount of filament used in mm
|
|
|
124 |
*/
|
|
|
125 |
static void incFilamentUsed(float const &amount);
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* @brief Reset the Print Statistics
|
|
|
129 |
* @details Reset the statistics to zero and saves them to EEPROM creating
|
|
|
130 |
* also the magic header.
|
|
|
131 |
*/
|
|
|
132 |
static void initStats();
|
|
|
133 |
|
|
|
134 |
/**
|
|
|
135 |
* @brief Load the Print Statistics
|
|
|
136 |
* @details Load the statistics from EEPROM
|
|
|
137 |
*/
|
|
|
138 |
static void loadStats();
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* @brief Save the Print Statistics
|
|
|
142 |
* @details Save the statistics to EEPROM
|
|
|
143 |
*/
|
|
|
144 |
static void saveStats();
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* @brief Serial output the Print Statistics
|
|
|
148 |
* @details This function may change in the future, for now it directly
|
|
|
149 |
* prints the statistical data to serial.
|
|
|
150 |
*/
|
|
|
151 |
static void showStats();
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* @brief Return the currently loaded statistics
|
|
|
155 |
* @details Return the raw data, in the same structure used internally
|
|
|
156 |
*/
|
|
|
157 |
static printStatistics getStats() { return data; }
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* @brief Loop function
|
|
|
161 |
* @details This function should be called at loop, it will take care of
|
|
|
162 |
* periodically save the statistical data to EEPROM and do time keeping.
|
|
|
163 |
*/
|
|
|
164 |
static void tick();
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* The following functions are being overridden
|
|
|
168 |
*/
|
|
|
169 |
static bool start();
|
|
|
170 |
static bool stop();
|
|
|
171 |
static void reset();
|
|
|
172 |
|
|
|
173 |
#if ENABLED(DEBUG_PRINTCOUNTER)
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* @brief Print a debug message
|
|
|
177 |
* @details Print a simple debug message
|
|
|
178 |
*/
|
|
|
179 |
static void debug(const char func[]);
|
|
|
180 |
|
|
|
181 |
#endif
|
|
|
182 |
};
|
|
|
183 |
|
|
|
184 |
// Global Print Job Timer instance
|
|
|
185 |
#if ENABLED(PRINTCOUNTER)
|
|
|
186 |
extern PrintCounter print_job_timer;
|
|
|
187 |
#else
|
|
|
188 |
extern Stopwatch print_job_timer;
|
|
|
189 |
#endif
|
|
|
190 |
|
|
|
191 |
#endif // PRINTCOUNTER_H
|