fsfw/timemanager/Stopwatch.h

71 lines
2.0 KiB
C
Raw Normal View History

2020-09-18 12:27:40 +02:00
#ifndef FSFW_TIMEMANAGER_STOPWATCH_H_
#define FSFW_TIMEMANAGER_STOPWATCH_H_
2020-08-13 20:53:35 +02:00
#include "Clock.h"
enum class StopwatchDisplayMode {
MILLIS,
SECONDS
};
/**
* @brief Simple Stopwatch implementation to measure elapsed time
* @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
* in seconds as a double.
* @author R. Mueller
*/
class Stopwatch {
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.
*/
Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode
= StopwatchDisplayMode::MILLIS);
virtual~ Stopwatch();
/**
* Caches the start time
*/
void start();
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in milliseconds (rounded)
*/
2020-09-18 12:27:40 +02:00
dur_millis_t stop(bool display = false);
2020-08-13 20:53:35 +02:00
/**
* Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision)
*/
2020-09-18 12:27:40 +02:00
double stopSeconds();
2020-08-13 20:53:35 +02:00
/**
* Displays the elapsed times on the osstream, depending on internal display
* mode.
*/
void display();
StopwatchDisplayMode getDisplayMode() const;
void setDisplayMode(StopwatchDisplayMode displayMode);
bool displayOnDestruction = true;
private:
timeval startTime {0, 0};
timeval elapsedTime {0, 0};
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS;
void stopInternal();
};
2020-09-18 12:27:40 +02:00
#endif /* FSFW_TIMEMANAGER_STOPWATCH_H_ */