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
   // Refuses to save data is object is not loaded
73
   // Refuses to save data is object is not loaded
74
   if (!this->isLoaded()) return;
74
   if (!this->isLoaded()) return;
75
 
75
 
76
+  // Saves the struct to EEPROM
76
   eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
77
   eeprom_update_block(&this->data, (void *)(this->address + sizeof(uint8_t)), sizeof(printStatistics));
77
 }
78
 }
78
 
79
 
135
   }
136
   }
136
 }
137
 }
137
 
138
 
138
-void PrintCounter::start() {
139
+// @Override
140
+bool PrintCounter::start() {
139
   #if ENABLED(DEBUG_PRINTCOUNTER)
141
   #if ENABLED(DEBUG_PRINTCOUNTER)
140
     PrintCounter::debug(PSTR("start"));
142
     PrintCounter::debug(PSTR("start"));
141
   #endif
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
   #if ENABLED(DEBUG_PRINTCOUNTER)
159
   #if ENABLED(DEBUG_PRINTCOUNTER)
149
     PrintCounter::debug(PSTR("stop"));
160
     PrintCounter::debug(PSTR("stop"));
150
   #endif
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
 void PrintCounter::reset() {
172
 void PrintCounter::reset() {
161
   #if ENABLED(DEBUG_PRINTCOUNTER)
173
   #if ENABLED(DEBUG_PRINTCOUNTER)
162
     PrintCounter::debug(PSTR("stop"));
174
     PrintCounter::debug(PSTR("stop"));
163
   #endif
175
   #endif
164
 
176
 
165
-  this->lastDuration = 0;
166
   super::reset();
177
   super::reset();
178
+  this->lastDuration = 0;
167
 }
179
 }
168
 
180
 
169
 #if ENABLED(DEBUG_PRINTCOUNTER)
181
 #if ENABLED(DEBUG_PRINTCOUNTER)

+ 2
- 2
Marlin/printcounter.h View File

138
     /**
138
     /**
139
      * The following functions are being overridden
139
      * The following functions are being overridden
140
      */
140
      */
141
-    void start();
142
-    void stop();
141
+    bool start();
142
+    bool stop();
143
     void reset();
143
     void reset();
144
 
144
 
145
     #if ENABLED(DEBUG_PRINTCOUNTER)
145
     #if ENABLED(DEBUG_PRINTCOUNTER)

+ 26
- 20
Marlin/stopwatch.cpp View File

27
   this->reset();
27
   this->reset();
28
 }
28
 }
29
 
29
 
30
-void Stopwatch::stop() {
30
+bool Stopwatch::stop() {
31
   #if ENABLED(DEBUG_STOPWATCH)
31
   #if ENABLED(DEBUG_STOPWATCH)
32
     Stopwatch::debug(PSTR("stop"));
32
     Stopwatch::debug(PSTR("stop"));
33
   #endif
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
   #if ENABLED(DEBUG_STOPWATCH)
44
   #if ENABLED(DEBUG_STOPWATCH)
43
     Stopwatch::debug(PSTR("pause"));
45
     Stopwatch::debug(PSTR("pause"));
44
   #endif
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
   #if ENABLED(DEBUG_STOPWATCH)
57
   #if ENABLED(DEBUG_STOPWATCH)
54
     Stopwatch::debug(PSTR("start"));
58
     Stopwatch::debug(PSTR("start"));
55
   #endif
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
 void Stopwatch::reset() {
72
 void Stopwatch::reset() {
68
     Stopwatch::debug(PSTR("reset"));
74
     Stopwatch::debug(PSTR("reset"));
69
   #endif
75
   #endif
70
 
76
 
71
-  this->state = STPWTCH_STOPPED;
77
+  this->state = STOPWATCH_STOPPED;
72
   this->startTimestamp = 0;
78
   this->startTimestamp = 0;
73
   this->stopTimestamp = 0;
79
   this->stopTimestamp = 0;
74
   this->accumulator = 0;
80
   this->accumulator = 0;
75
 }
81
 }
76
 
82
 
77
 bool Stopwatch::isRunning() {
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
 bool Stopwatch::isPaused() {
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
 uint16_t Stopwatch::duration() {
91
 uint16_t Stopwatch::duration() {

+ 13
- 10
Marlin/stopwatch.h View File

29
 //#define DEBUG_STOPWATCH
29
 //#define DEBUG_STOPWATCH
30
 
30
 
31
 enum StopwatchState {
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
      * @brief Stops the stopwatch
56
      * @brief Stops the stopwatch
57
      * @details Stops the running timer, it will silently ignore the request if
57
      * @details Stops the running timer, it will silently ignore the request if
58
      * no timer is currently running.
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
      * @details Pauses the running timer, it will silently ignore the request if
65
      * @details Pauses the running timer, it will silently ignore the request if
65
      * no timer is currently running.
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
      * @brief Starts the stopwatch
72
      * @brief Starts the stopwatch
71
      * @details Starts the timer, it will silently ignore the request if the
73
      * @details Starts the timer, it will silently ignore the request if the
72
      * timer is already running.
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
      * @brief Resets the stopwatch
80
      * @brief Resets the stopwatch
82
     /**
85
     /**
83
      * @brief Checks if the timer is running
86
      * @brief Checks if the timer is running
84
      * @details Returns true if the timer is currently running, false otherwise.
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
     bool isRunning();
90
     bool isRunning();
88
 
91
 
89
     /**
92
     /**
90
      * @brief Checks if the timer is paused
93
      * @brief Checks if the timer is paused
91
      * @details Returns true if the timer is currently paused, false otherwise.
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
     bool isPaused();
97
     bool isPaused();
95
 
98
 
96
     /**
99
     /**
97
      * @brief Gets the running time
100
      * @brief Gets the running time
98
      * @details Returns the total number of seconds the timer has been running.
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
     uint16_t duration();
104
     uint16_t duration();
102
 
105
 

Loading…
Cancel
Save