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.

stopwatch.h 3.5KB

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