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