stopwatch.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 STOPWATCH_H
  23. #define STOPWATCH_H
  24. // Print debug messages with M111 S2 (Uses 156 bytes of PROGMEM)
  25. //#define DEBUG_STOPWATCH
  26. #include "macros.h"
  27. #include "types.h"
  28. /**
  29. * @brief Stopwatch class
  30. * @details This class acts as a timer proving stopwatch functionality including
  31. * the ability to pause the running time counter.
  32. */
  33. class Stopwatch {
  34. private:
  35. enum State : char {
  36. STOPPED,
  37. RUNNING,
  38. PAUSED
  39. };
  40. static Stopwatch::State state;
  41. static millis_t accumulator;
  42. static millis_t startTimestamp;
  43. static millis_t stopTimestamp;
  44. public:
  45. /**
  46. * @brief Initialize the stopwatch
  47. */
  48. FORCE_INLINE static void init() { reset(); }
  49. /**
  50. * @brief Stop the stopwatch
  51. * @details Stop the running timer. Silently ignore the request if
  52. * no timer is running.
  53. * @return true on success
  54. */
  55. static bool stop();
  56. /**
  57. * @brief Pause the stopwatch
  58. * @details Pause the running timer, it will silently ignore the request if
  59. * no timer is running.
  60. * @return true on success
  61. */
  62. static bool pause();
  63. /**
  64. * @brief Start the stopwatch
  65. * @details Start the timer, it will silently ignore the request if the
  66. * timer is already running.
  67. * @return true on success
  68. */
  69. static bool start();
  70. /**
  71. * @brief Resume the stopwatch
  72. * @details Resume a timer from a given duration
  73. */
  74. static void resume(const millis_t duration);
  75. /**
  76. * @brief Reset the stopwatch
  77. * @details Reset all settings to their default values.
  78. */
  79. static void reset();
  80. /**
  81. * @brief Check if the timer is running
  82. * @details Return true if the timer is currently running, false otherwise.
  83. * @return true if stopwatch is running
  84. */
  85. FORCE_INLINE static bool isRunning() { return state == RUNNING; }
  86. /**
  87. * @brief Check if the timer is paused
  88. * @details Return true if the timer is currently paused, false otherwise.
  89. * @return true if stopwatch is paused
  90. */
  91. FORCE_INLINE static bool isPaused() { return state == PAUSED; }
  92. /**
  93. * @brief Get the running time
  94. * @details Return the total number of seconds the timer has been running.
  95. * @return the delta since starting the stopwatch
  96. */
  97. static millis_t duration();
  98. #ifdef DEBUG_STOPWATCH
  99. /**
  100. * @brief Print a debug message
  101. * @details Print a simple debug message "Stopwatch::function"
  102. */
  103. static void debug(const char func[]);
  104. #endif
  105. };
  106. #endif // STOPWATCH_H