fsfw/timemanager/Stopwatch.h

72 lines
2.1 KiB
C
Raw Normal View History

#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
#include <framework/timemanager/Clock.h>
2020-04-08 18:33:38 +02:00
enum class StopwatchDisplayMode {
2020-04-08 19:24:24 +02:00
MILLIS,
SECONDS
2020-04-08 18:33:38 +02:00
};
2020-04-08 18:27:18 +02:00
/**
* @brief Simple Stopwatch implementation to measure elapsed time
2020-04-08 18:27:18 +02:00
* @details
* This class can be used to measure elapsed times. It also displays elapsed
* times automatically on destruction if not explicitely deactivated in the
* constructor. The default time format is the elapsed time in miliseconds
2020-05-29 17:43:37 +02:00
* in seconds as a double.
* @author R. Mueller
2020-04-08 18:27:18 +02:00
*/
class Stopwatch {
2020-04-08 19:05:21 +02:00
public:
/**
* Default constructor. Call "Stopwatch stopwatch" without brackets if
* no parameters are required!
* @param displayOnDestruction If set to true, displays measured time on
* object destruction
* @param displayMode Display format is either MS rounded or MS as double
* format
* @param outputPrecision If using double format, specify precision here.
*/
2020-04-08 19:24:24 +02:00
Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode
= StopwatchDisplayMode::MILLIS);
virtual~ Stopwatch();
2020-04-08 18:33:38 +02:00
/**
* Caches the start time
*/
void start();
2020-04-08 18:33:38 +02:00
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in milliseconds (rounded)
*/
2020-06-06 23:23:20 +02:00
dur_millis_t stop();
2020-05-29 17:43:37 +02:00
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision)
*/
2020-06-06 23:23:20 +02:00
dur_seconds_t stopSeconds();
2020-04-08 18:33:38 +02:00
/**
* Displays the elapsed times on the osstream, depending on internal display
* mode.
*/
void display();
2020-04-08 18:33:38 +02:00
StopwatchDisplayMode getDisplayMode() const;
void setDisplayMode(StopwatchDisplayMode displayMode);
bool displayOnDestruction = true;
private:
timeval startTime {0, 0};
timeval elapsedTime {0, 0};
2020-04-08 19:24:24 +02:00
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS;
void stopInternal();
};
#endif /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */