getClock_timval used now

This commit is contained in:
Robin Müller 2020-05-29 17:43:06 +02:00
parent 35b9346c2b
commit 1cf5991101
2 changed files with 19 additions and 10 deletions

View File

@ -3,14 +3,14 @@
#include <iomanip> #include <iomanip>
Stopwatch::Stopwatch(bool displayOnDestruction, Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayMode(displayMode), StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction(displayOnDestruction) { displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization. // Measures start time on initialization.
startTime = Clock::getUptime(); Clock::getClock_timeval(&startTime);
} }
void Stopwatch::start() { void Stopwatch::start() {
startTime = Clock::getUptime(); Clock::getClock_timeval(&startTime);
} }
millis_t Stopwatch::stop() { millis_t Stopwatch::stop() {
@ -51,5 +51,7 @@ StopwatchDisplayMode Stopwatch::getDisplayMode() const {
} }
void Stopwatch::stopInternal() { void Stopwatch::stopInternal() {
elapsedTime = Clock::getUptime() - startTime; timeval endTime;
Clock::getClock_timeval(&endTime);
elapsedTime = endTime - startTime;
} }

View File

@ -12,7 +12,9 @@ enum class StopwatchDisplayMode {
* @details * @details
* This class can be used to measure elapsed times. It also displays elapsed * This class can be used to measure elapsed times. It also displays elapsed
* times automatically on destruction if not explicitely deactivated in the * times automatically on destruction if not explicitely deactivated in the
* constructor. The default time format is the elapsed time in miliseconds. * constructor. The default time format is the elapsed time in miliseconds
* in seconds as a double.
* @author R. Mueller
*/ */
class Stopwatch { class Stopwatch {
public: public:
@ -21,8 +23,8 @@ public:
* no parameters are required! * no parameters are required!
* @param displayOnDestruction If set to true, displays measured time on * @param displayOnDestruction If set to true, displays measured time on
* object destruction * object destruction
* @param displayMode Display format is either MS rounded or seconds as * @param displayMode Display format is either MS rounded or MS as double
* double format * format
* @param outputPrecision If using double format, specify precision here. * @param outputPrecision If using double format, specify precision here.
*/ */
Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode
@ -39,6 +41,10 @@ public:
* @return elapsed time in milliseconds (rounded) * @return elapsed time in milliseconds (rounded)
*/ */
millis_t stop(); millis_t stop();
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision)
*/
seconds_t stopSeconds(); seconds_t stopSeconds();
/** /**
@ -49,12 +55,13 @@ public:
StopwatchDisplayMode getDisplayMode() const; StopwatchDisplayMode getDisplayMode() const;
void setDisplayMode(StopwatchDisplayMode displayMode); void setDisplayMode(StopwatchDisplayMode displayMode);
bool displayOnDestruction = true;
private: private:
timeval startTime {0, 0}; timeval startTime {0, 0};
timeval elapsedTime {0, 0}; timeval elapsedTime {0, 0};
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS; StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS;
bool displayOnDestruction = true;
void stopInternal(); void stopInternal();
}; };