duration_t.h 4.3 KB

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