Subversion Repositories Tronxy-X3A-Marlin

Rev

Details | Last modification | View Log | RSS feed

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