My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

duration_t.h 4.7KB

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