| 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 |
#include "stopwatch.h"
|
|
|
24 |
|
|
|
25 |
#include "Marlin.h"
|
|
|
26 |
|
|
|
27 |
Stopwatch::State Stopwatch::state;
|
|
|
28 |
millis_t Stopwatch::accumulator;
|
|
|
29 |
millis_t Stopwatch::startTimestamp;
|
|
|
30 |
millis_t Stopwatch::stopTimestamp;
|
|
|
31 |
|
|
|
32 |
bool Stopwatch::stop() {
|
|
|
33 |
#if ENABLED(DEBUG_STOPWATCH)
|
|
|
34 |
Stopwatch::debug(PSTR("stop"));
|
|
|
35 |
#endif
|
|
|
36 |
|
|
|
37 |
if (isRunning() || isPaused()) {
|
|
|
38 |
state = STOPPED;
|
|
|
39 |
stopTimestamp = millis();
|
|
|
40 |
return true;
|
|
|
41 |
}
|
|
|
42 |
else return false;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
bool Stopwatch::pause() {
|
|
|
46 |
#if ENABLED(DEBUG_STOPWATCH)
|
|
|
47 |
Stopwatch::debug(PSTR("pause"));
|
|
|
48 |
#endif
|
|
|
49 |
|
|
|
50 |
if (isRunning()) {
|
|
|
51 |
state = PAUSED;
|
|
|
52 |
stopTimestamp = millis();
|
|
|
53 |
return true;
|
|
|
54 |
}
|
|
|
55 |
else return false;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
bool Stopwatch::start() {
|
|
|
59 |
#if ENABLED(DEBUG_STOPWATCH)
|
|
|
60 |
Stopwatch::debug(PSTR("start"));
|
|
|
61 |
#endif
|
|
|
62 |
|
|
|
63 |
if (!isRunning()) {
|
|
|
64 |
if (isPaused()) accumulator = duration();
|
|
|
65 |
else reset();
|
|
|
66 |
|
|
|
67 |
state = RUNNING;
|
|
|
68 |
startTimestamp = millis();
|
|
|
69 |
return true;
|
|
|
70 |
}
|
|
|
71 |
else return false;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
void Stopwatch::resume(const millis_t duration) {
|
|
|
75 |
#if ENABLED(DEBUG_STOPWATCH)
|
|
|
76 |
Stopwatch::debug(PSTR("resume"));
|
|
|
77 |
#endif
|
|
|
78 |
|
|
|
79 |
reset();
|
|
|
80 |
if ((accumulator = duration)) state = RUNNING;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
void Stopwatch::reset() {
|
|
|
84 |
#if ENABLED(DEBUG_STOPWATCH)
|
|
|
85 |
Stopwatch::debug(PSTR("reset"));
|
|
|
86 |
#endif
|
|
|
87 |
|
|
|
88 |
state = STOPPED;
|
|
|
89 |
startTimestamp = 0;
|
|
|
90 |
stopTimestamp = 0;
|
|
|
91 |
accumulator = 0;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
millis_t Stopwatch::duration() {
|
|
|
95 |
return ((isRunning() ? millis() : stopTimestamp)
|
|
|
96 |
- startTimestamp) / 1000UL + accumulator;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
#if ENABLED(DEBUG_STOPWATCH)
|
|
|
100 |
|
|
|
101 |
void Stopwatch::debug(const char func[]) {
|
|
|
102 |
if (DEBUGGING(INFO)) {
|
|
|
103 |
SERIAL_ECHOPGM("Stopwatch::");
|
|
|
104 |
serialprintPGM(func);
|
|
|
105 |
SERIAL_ECHOLNPGM("()");
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
#endif
|