Prusa MINI Firmware overview
stopwatch.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  * Copyright (c) 2019 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 #pragma once
23 
24 // Print debug messages with M111 S2 (Uses 156 bytes of PROGMEM)
25 //#define DEBUG_STOPWATCH
26 
27 #include "../core/macros.h" // for FORCE_INLINE
28 
29 #include <stdint.h>
30 typedef uint32_t millis_t;
31 
32 /**
33  * @brief Stopwatch class
34  * @details This class acts as a timer proving stopwatch functionality including
35  * the ability to pause the running time counter.
36  */
37 class Stopwatch {
38  private:
39  enum State : char { STOPPED, RUNNING, PAUSED };
40 
41  static Stopwatch::State state;
42  static millis_t accumulator;
43  static millis_t startTimestamp;
44  static millis_t stopTimestamp;
45 
46  public:
47  /**
48  * @brief Initialize the stopwatch
49  */
50  FORCE_INLINE static void init() { reset(); }
51 
52  /**
53  * @brief Stop the stopwatch
54  * @details Stop the running timer, it will silently ignore the request if
55  * no timer is currently running.
56  * @return true on success
57  */
58  static bool stop();
59 
60  /**
61  * @brief Pause the stopwatch
62  * @details Pause the running timer, it will silently ignore the request if
63  * no timer is currently running.
64  * @return true on success
65  */
66  static bool pause();
67 
68  /**
69  * @brief Start the stopwatch
70  * @details Start the timer, it will silently ignore the request if the
71  * timer is already running.
72  * @return true on success
73  */
74  static bool start();
75 
76  /**
77  * @brief Resume the stopwatch
78  * @details Resume a timer from a given duration
79  */
80  static void resume(const millis_t with_time);
81 
82  /**
83  * @brief Reset the stopwatch
84  * @details Reset all settings to their default values.
85  */
86  static void reset();
87 
88  /**
89  * @brief Check if the timer is running
90  * @details Return true if the timer is currently running, false otherwise.
91  * @return true if stopwatch is running
92  */
93  FORCE_INLINE static bool isRunning() { return state == RUNNING; }
94 
95  /**
96  * @brief Check if the timer is paused
97  * @details Return true if the timer is currently paused, false otherwise.
98  * @return true if stopwatch is paused
99  */
100  FORCE_INLINE static bool isPaused() { return state == PAUSED; }
101 
102  /**
103  * @brief Get the running time
104  * @details Return the total number of seconds the timer has been running.
105  * @return the delta since starting the stopwatch
106  */
107  static millis_t duration();
108 
109  #ifdef DEBUG_STOPWATCH
110 
111  /**
112  * @brief Print a debug message
113  * @details Print a simple debug message "Stopwatch::function"
114  */
115  static void debug(const char func[]);
116 
117  #endif
118 };
Stopwatch::isRunning
static FORCE_INLINE bool isRunning()
Check if the timer is running.
Definition: stopwatch.h:93
millis
uint32_t millis(void)
Definition: wiring_time.c:29
ExtUI::onPrintTimerPaused
void onPrintTimerPaused()
Definition: marlin_server.cpp:884
Stopwatch::resume
static void resume(const millis_t with_time)
Resume the stopwatch.
Definition: stopwatch.cpp:88
Stopwatch::stop
static bool stop()
Stop the stopwatch.
Definition: stopwatch.cpp:36
FORCE_INLINE
#define FORCE_INLINE
Definition: macros.h:40
PSTR
#define PSTR(str)
Definition: pgmspace.h:31
ExtUI::onPrintTimerStarted
void onPrintTimerStarted()
Definition: marlin_server.cpp:879
Stopwatch::duration
static millis_t duration()
Get the running time.
Definition: stopwatch.cpp:108
SERIAL_ECHOPGM
#define SERIAL_ECHOPGM(S)
Definition: serial.h:173
Stopwatch::pause
static bool pause()
Pause the stopwatch.
Definition: stopwatch.cpp:52
Stopwatch::start
static bool start()
Start the stopwatch.
Definition: stopwatch.cpp:68
Stopwatch::reset
static void reset()
Reset the stopwatch.
Definition: stopwatch.cpp:97
Stopwatch::isPaused
static FORCE_INLINE bool isPaused()
Check if the timer is paused.
Definition: stopwatch.h:100
millis_t
uint32_t millis_t
Definition: stopwatch.h:30
DEBUGGING
#define DEBUGGING(F)
Definition: serial.h:47
Stopwatch::init
static FORCE_INLINE void init()
Initialize the stopwatch.
Definition: stopwatch.h:50
serialprintPGM
void serialprintPGM(PGM_P str)
Definition: serial.cpp:35
SERIAL_ECHOLNPGM
#define SERIAL_ECHOLNPGM(S)
Definition: serial.h:174
ExtUI::onPrintTimerStopped
void onPrintTimerStopped()
Definition: marlin_server.cpp:889
millis_t
uint32_t millis_t
Definition: millis_t.h:26
Stopwatch
Stopwatch class.
Definition: stopwatch.h:37
stopwatch.h