| 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 __DURATION_T__
|
|
|
24 |
#define __DURATION_T__
|
|
|
25 |
|
|
|
26 |
#include <stdio.h>
|
|
|
27 |
#include <inttypes.h>
|
|
|
28 |
|
|
|
29 |
struct duration_t {
|
|
|
30 |
/**
|
|
|
31 |
* @brief Duration is stored in seconds
|
|
|
32 |
*/
|
|
|
33 |
uint32_t value;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* @brief Constructor
|
|
|
37 |
*/
|
|
|
38 |
duration_t()
|
|
|
39 |
: duration_t(0) {};
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* @brief Constructor
|
|
|
43 |
*
|
|
|
44 |
* @param seconds The number of seconds
|
|
|
45 |
*/
|
|
|
46 |
duration_t(uint32_t const &seconds) {
|
|
|
47 |
this->value = seconds;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @brief Equality comparison
|
|
|
52 |
* @details Overloads the equality comparison operator
|
|
|
53 |
*
|
|
|
54 |
* @param value The number of seconds to compare to
|
|
|
55 |
* @return True if both durations are equal
|
|
|
56 |
*/
|
|
|
57 |
bool operator==(const uint32_t &value) const {
|
|
|
58 |
return (this->value == value);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @brief Inequality comparison
|
|
|
63 |
* @details Overloads the inequality comparison operator
|
|
|
64 |
*
|
|
|
65 |
* @param value The number of seconds to compare to
|
|
|
66 |
* @return False if both durations are equal
|
|
|
67 |
*/
|
|
|
68 |
bool operator!=(const uint32_t &value) const {
|
|
|
69 |
return ! this->operator==(value);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* @brief Formats the duration as years
|
|
|
74 |
* @return The number of years
|
|
|
75 |
*/
|
|
|
76 |
inline uint8_t year() const {
|
|
|
77 |
return this->day() / 365;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* @brief Formats the duration as days
|
|
|
82 |
* @return The number of days
|
|
|
83 |
*/
|
|
|
84 |
inline uint16_t day() const {
|
|
|
85 |
return this->hour() / 24;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* @brief Formats the duration as hours
|
|
|
90 |
* @return The number of hours
|
|
|
91 |
*/
|
|
|
92 |
inline uint32_t hour() const {
|
|
|
93 |
return this->minute() / 60;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* @brief Formats the duration as minutes
|
|
|
98 |
* @return The number of minutes
|
|
|
99 |
*/
|
|
|
100 |
inline uint32_t minute() const {
|
|
|
101 |
return this->second() / 60;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* @brief Formats the duration as seconds
|
|
|
106 |
* @return The number of seconds
|
|
|
107 |
*/
|
|
|
108 |
inline uint32_t second() const {
|
|
|
109 |
return this->value;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* @brief Formats the duration as a string
|
|
|
114 |
* @details String will be formated using a "full" representation of duration
|
|
|
115 |
*
|
|
|
116 |
* @param buffer The array pointed to must be able to accommodate 21 bytes
|
|
|
117 |
*
|
|
|
118 |
* Output examples:
|
|
|
119 |
* 123456789012345678901 (strlen)
|
|
|
120 |
* 135y 364d 23h 59m 59s
|
|
|
121 |
* 364d 23h 59m 59s
|
|
|
122 |
* 23h 59m 59s
|
|
|
123 |
* 59m 59s
|
|
|
124 |
* 59s
|
|
|
125 |
*/
|
|
|
126 |
void toString(char *buffer) const {
|
|
|
127 |
int y = this->year(),
|
|
|
128 |
d = this->day() % 365,
|
|
|
129 |
h = this->hour() % 24,
|
|
|
130 |
m = this->minute() % 60,
|
|
|
131 |
s = this->second() % 60;
|
|
|
132 |
|
|
|
133 |
if (y) sprintf_P(buffer, PSTR("%iy %id %ih %im %is"), y, d, h, m, s);
|
|
|
134 |
else if (d) sprintf_P(buffer, PSTR("%id %ih %im %is"), d, h, m, s);
|
|
|
135 |
else if (h) sprintf_P(buffer, PSTR("%ih %im %is"), h, m, s);
|
|
|
136 |
else if (m) sprintf_P(buffer, PSTR("%im %is"), m, s);
|
|
|
137 |
else sprintf_P(buffer, PSTR("%is"), s);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* @brief Formats the duration as a string
|
|
|
142 |
* @details String will be formated using a "digital" representation of duration
|
|
|
143 |
*
|
|
|
144 |
* @param buffer The array pointed to must be able to accommodate 10 bytes
|
|
|
145 |
*
|
|
|
146 |
* Output examples:
|
|
|
147 |
* 123456789 (strlen)
|
|
|
148 |
* 99:59
|
|
|
149 |
* 11d 12:33
|
|
|
150 |
*/
|
|
|
151 |
uint8_t toDigital(char *buffer, bool with_days=false) const {
|
|
|
152 |
uint16_t h = uint16_t(this->hour()),
|
|
|
153 |
m = uint16_t(this->minute() % 60UL);
|
|
|
154 |
if (with_days) {
|
|
|
155 |
uint16_t d = this->day();
|
|
|
156 |
sprintf_P(buffer, PSTR("%ud %02u:%02u"), d, h % 24, m);
|
|
|
157 |
return d >= 10 ? 9 : 8;
|
|
|
158 |
}
|
|
|
159 |
else if (h < 100) {
|
|
|
160 |
sprintf_P(buffer, PSTR("%02u:%02u"), h, m);
|
|
|
161 |
return 5;
|
|
|
162 |
}
|
|
|
163 |
else {
|
|
|
164 |
sprintf_P(buffer, PSTR("%u:%02u"), h, m);
|
|
|
165 |
return 6;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
};
|
|
|
169 |
|
|
|
170 |
#endif // __DURATION_T__
|