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.4KB

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