Browse Source

Fixes #3809 and adds several improvements to the Stopwatch and

PrintCounter classes
João Brázio 8 years ago
parent
commit
8c0edb2de4
No account linked to committer's email address
4 changed files with 64 additions and 43 deletions
  1. 23
    11
      Marlin/printcounter.cpp
  2. 2
    2
      Marlin/printcounter.h
  3. 26
    20
      Marlin/stopwatch.cpp
  4. 13
    10
      Marlin/stopwatch.h

+ 23
- 11
Marlin/printcounter.cpp View File

@@ -73,6 +73,7 @@ void PrintCounter::saveStats() {
73 73
   // Refuses to save data is object is not loaded
74 74
   if (!this->isLoaded()) return;
75 75
 
76
+  // Saves the struct to EEPROM
76 77
   eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
77 78
 }
78 79
 
@@ -135,35 +136,46 @@ void PrintCounter::tick() {
135 136
   }
136 137
 }
137 138
 
138
-void PrintCounter::start() {
139
+// @Override
140
+bool PrintCounter::start() {
139 141
   #if ENABLED(DEBUG_PRINTCOUNTER)
140 142
     PrintCounter::debug(PSTR("start"));
141 143
   #endif
142 144
 
143
-  if (!this->isPaused()) this->data.totalPrints++;
144
-  super::start();
145
+  bool paused = this->isPaused();
146
+
147
+  if (super::start()) {
148
+    if (!paused) {
149
+      this->data.totalPrints++;
150
+      this->lastDuration = 0;
151
+    }
152
+    return true;
153
+  }
154
+  else return false;
145 155
 }
146 156
 
147
-void PrintCounter::stop() {
157
+// @Override
158
+bool PrintCounter::stop() {
148 159
   #if ENABLED(DEBUG_PRINTCOUNTER)
149 160
     PrintCounter::debug(PSTR("stop"));
150 161
   #endif
151 162
 
152
-  if (!this->isRunning()) return;
153
-  super::stop();
154
-
155
-  this->data.finishedPrints++;
156
-  this->data.printTime += this->deltaDuration();
157
-  this->saveStats();
163
+  if (super::stop()) {
164
+    this->data.finishedPrints++;
165
+    this->data.printTime += this->deltaDuration();
166
+    this->saveStats();
167
+  }
168
+  else return false;
158 169
 }
159 170
 
171
+// @Override
160 172
 void PrintCounter::reset() {
161 173
   #if ENABLED(DEBUG_PRINTCOUNTER)
162 174
     PrintCounter::debug(PSTR("stop"));
163 175
   #endif
164 176
 
165
-  this->lastDuration = 0;
166 177
   super::reset();
178
+  this->lastDuration = 0;
167 179
 }
168 180
 
169 181
 #if ENABLED(DEBUG_PRINTCOUNTER)

+ 2
- 2
Marlin/printcounter.h View File

@@ -138,8 +138,8 @@ class PrintCounter: public Stopwatch {
138 138
     /**
139 139
      * The following functions are being overridden
140 140
      */
141
-    void start();
142
-    void stop();
141
+    bool start();
142
+    bool stop();
143 143
     void reset();
144 144
 
145 145
     #if ENABLED(DEBUG_PRINTCOUNTER)

+ 26
- 20
Marlin/stopwatch.cpp View File

@@ -27,40 +27,46 @@ Stopwatch::Stopwatch() {
27 27
   this->reset();
28 28
 }
29 29
 
30
-void Stopwatch::stop() {
30
+bool Stopwatch::stop() {
31 31
   #if ENABLED(DEBUG_STOPWATCH)
32 32
     Stopwatch::debug(PSTR("stop"));
33 33
   #endif
34 34
 
35
-  if (!this->isRunning()) return;
36
-
37
-  this->state = STPWTCH_STOPPED;
38
-  this->stopTimestamp = millis();
35
+  if (this->isRunning() || this->isPaused()) {
36
+    this->state = STOPWATCH_STOPPED;
37
+    this->stopTimestamp = millis();
38
+    return true;
39
+  }
40
+  else return false;
39 41
 }
40 42
 
41
-void Stopwatch::pause() {
43
+bool Stopwatch::pause() {
42 44
   #if ENABLED(DEBUG_STOPWATCH)
43 45
     Stopwatch::debug(PSTR("pause"));
44 46
   #endif
45 47
 
46
-  if (!this->isRunning()) return;
47
-
48
-  this->state = STPWTCH_PAUSED;
49
-  this->stopTimestamp = millis();
48
+  if (this->isRunning()) {
49
+    this->state = STOPWATCH_PAUSED;
50
+    this->stopTimestamp = millis();
51
+    return true;
52
+  }
53
+  else return false;
50 54
 }
51 55
 
52
-void Stopwatch::start() {
56
+bool Stopwatch::start() {
53 57
   #if ENABLED(DEBUG_STOPWATCH)
54 58
     Stopwatch::debug(PSTR("start"));
55 59
   #endif
56 60
 
57
-  if (this->isRunning()) return;
58
-
59
-  if (this->isPaused()) this->accumulator = this->duration();
60
-  else this->reset();
61
+  if (!this->isRunning()) {
62
+    if (this->isPaused()) this->accumulator = this->duration();
63
+    else this->reset();
61 64
 
62
-  this->state = STPWTCH_RUNNING;
63
-  this->startTimestamp = millis();
65
+    this->state = STOPWATCH_RUNNING;
66
+    this->startTimestamp = millis();
67
+    return true;
68
+  }
69
+  else return false;
64 70
 }
65 71
 
66 72
 void Stopwatch::reset() {
@@ -68,18 +74,18 @@ void Stopwatch::reset() {
68 74
     Stopwatch::debug(PSTR("reset"));
69 75
   #endif
70 76
 
71
-  this->state = STPWTCH_STOPPED;
77
+  this->state = STOPWATCH_STOPPED;
72 78
   this->startTimestamp = 0;
73 79
   this->stopTimestamp = 0;
74 80
   this->accumulator = 0;
75 81
 }
76 82
 
77 83
 bool Stopwatch::isRunning() {
78
-  return (this->state == STPWTCH_RUNNING) ? true : false;
84
+  return (this->state == STOPWATCH_RUNNING) ? true : false;
79 85
 }
80 86
 
81 87
 bool Stopwatch::isPaused() {
82
-  return (this->state == STPWTCH_PAUSED) ? true : false;
88
+  return (this->state == STOPWATCH_PAUSED) ? true : false;
83 89
 }
84 90
 
85 91
 uint16_t Stopwatch::duration() {

+ 13
- 10
Marlin/stopwatch.h View File

@@ -29,9 +29,9 @@
29 29
 //#define DEBUG_STOPWATCH
30 30
 
31 31
 enum StopwatchState {
32
-  STPWTCH_STOPPED,
33
-  STPWTCH_RUNNING,
34
-  STPWTCH_PAUSED
32
+  STOPWATCH_STOPPED,
33
+  STOPWATCH_RUNNING,
34
+  STOPWATCH_PAUSED
35 35
 };
36 36
 
37 37
 /**
@@ -56,22 +56,25 @@ class Stopwatch {
56 56
      * @brief Stops the stopwatch
57 57
      * @details Stops the running timer, it will silently ignore the request if
58 58
      * no timer is currently running.
59
+     * @return true is method was successful
59 60
      */
60
-    void stop();
61
+    bool stop();
61 62
 
62 63
     /**
63
-     * @brief Pauses the stopwatch
64
+     * @brief Pause the stopwatch
64 65
      * @details Pauses the running timer, it will silently ignore the request if
65 66
      * no timer is currently running.
67
+     * @return true is method was successful
66 68
      */
67
-    void pause();
69
+    bool pause();
68 70
 
69 71
     /**
70 72
      * @brief Starts the stopwatch
71 73
      * @details Starts the timer, it will silently ignore the request if the
72 74
      * timer is already running.
75
+     * @return true is method was successful
73 76
      */
74
-    void start();
77
+    bool start();
75 78
 
76 79
     /**
77 80
      * @brief Resets the stopwatch
@@ -82,21 +85,21 @@ class Stopwatch {
82 85
     /**
83 86
      * @brief Checks if the timer is running
84 87
      * @details Returns true if the timer is currently running, false otherwise.
85
-     * @return bool
88
+     * @return true if stopwatch is running
86 89
      */
87 90
     bool isRunning();
88 91
 
89 92
     /**
90 93
      * @brief Checks if the timer is paused
91 94
      * @details Returns true if the timer is currently paused, false otherwise.
92
-     * @return bool
95
+     * @return true if stopwatch is paused
93 96
      */
94 97
     bool isPaused();
95 98
 
96 99
     /**
97 100
      * @brief Gets the running time
98 101
      * @details Returns the total number of seconds the timer has been running.
99
-     * @return uint16_t
102
+     * @return the delta since starting the stopwatch
100 103
      */
101 104
     uint16_t duration();
102 105
 

Loading…
Cancel
Save