new stopwatch :-)
This commit is contained in:
		| @@ -3,8 +3,11 @@ | |||||||
| #include <stdlib.h> | #include <stdlib.h> | ||||||
| #include "Timekeeper.h" | #include "Timekeeper.h" | ||||||
|  |  | ||||||
|  | extern "C" { | ||||||
| #include <FreeRTOS.h> | #include <FreeRTOS.h> | ||||||
| #include <task.h> | #include <task.h> | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| //TODO sanitize input? | //TODO sanitize input? | ||||||
| //TODO much of this code can be reused for tick-only systems | //TODO much of this code can be reused for tick-only systems | ||||||
| @@ -56,7 +59,6 @@ ReturnValue_t Clock::getUptime(timeval* uptime) { | |||||||
|  |  | ||||||
| timeval Clock::getUptime() { | timeval Clock::getUptime() { | ||||||
| 	TickType_t ticksSinceStart = xTaskGetTickCount(); | 	TickType_t ticksSinceStart = xTaskGetTickCount(); | ||||||
|  |  | ||||||
| 	return Timekeeper::ticksToTimeval(ticksSinceStart); | 	return Timekeeper::ticksToTimeval(ticksSinceStart); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,20 +1,24 @@ | |||||||
| #include "Timekeeper.h" | /** | ||||||
| #include <FreeRTOSConfig.h> |  * @file Timekeeper.cpp | ||||||
|  |  * @date | ||||||
|  |  */ | ||||||
|  |  | ||||||
| Timekeeper::Timekeeper() : | #include <framework/osal/FreeRTOS/Timekeeper.h> | ||||||
| 		offset( { 0, 0 }) { |  | ||||||
| 	// TODO Auto-generated constructor stub |  | ||||||
|  |  | ||||||
|  | extern "C" { | ||||||
|  | #include <task.h> | ||||||
| } | } | ||||||
|  |  | ||||||
| Timekeeper * Timekeeper::myinstance = NULL; | Timekeeper * Timekeeper::myinstance = nullptr; | ||||||
|  |  | ||||||
|  | Timekeeper::Timekeeper() : offset( { 0, 0 }) {} | ||||||
|  |  | ||||||
| const timeval& Timekeeper::getOffset() const { | const timeval& Timekeeper::getOffset() const { | ||||||
| 	return offset; | 	return offset; | ||||||
| } | } | ||||||
|  |  | ||||||
| Timekeeper* Timekeeper::instance() { | Timekeeper* Timekeeper::instance() { | ||||||
| 	if (myinstance == NULL) { | 	if (myinstance == nullptr) { | ||||||
| 		myinstance = new Timekeeper(); | 		myinstance = new Timekeeper(); | ||||||
| 	} | 	} | ||||||
| 	return myinstance; | 	return myinstance; | ||||||
| @@ -24,9 +28,7 @@ void Timekeeper::setOffset(const timeval& offset) { | |||||||
| 	this->offset = offset; | 	this->offset = offset; | ||||||
| } | } | ||||||
|  |  | ||||||
| Timekeeper::~Timekeeper() { | Timekeeper::~Timekeeper() {} | ||||||
| 	// TODO Auto-generated destructor stub |  | ||||||
| } |  | ||||||
|  |  | ||||||
| timeval Timekeeper::ticksToTimeval(TickType_t ticks) { | timeval Timekeeper::ticksToTimeval(TickType_t ticks) { | ||||||
| 	timeval uptime; | 	timeval uptime; | ||||||
| @@ -40,3 +42,7 @@ timeval Timekeeper::ticksToTimeval(TickType_t ticks) { | |||||||
|  |  | ||||||
| 	return uptime; | 	return uptime; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | TickType_t Timekeeper::getTicks() { | ||||||
|  |     return xTaskGetTickCount(); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -2,8 +2,10 @@ | |||||||
| #define FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ | #define FRAMEWORK_OSAL_FREERTOS_TIMEKEEPER_H_ | ||||||
|  |  | ||||||
| #include <framework/timemanager/Clock.h> | #include <framework/timemanager/Clock.h> | ||||||
|  | extern "C" { | ||||||
| #include <FreeRTOS.h> | #include <FreeRTOS.h> | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * A Class to basically store the time difference between uptime and UTC |  * A Class to basically store the time difference between uptime and UTC | ||||||
| @@ -25,6 +27,11 @@ public: | |||||||
| 	virtual ~Timekeeper(); | 	virtual ~Timekeeper(); | ||||||
|  |  | ||||||
| 	static timeval ticksToTimeval(TickType_t ticks); | 	static timeval ticksToTimeval(TickType_t ticks); | ||||||
|  |     /** | ||||||
|  |      * Get elapsed time in system ticks. | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     static TickType_t getTicks(); | ||||||
|  |  | ||||||
| 	const timeval& getOffset() const; | 	const timeval& getOffset() const; | ||||||
| 	void setOffset(const timeval& offset); | 	void setOffset(const timeval& offset); | ||||||
|   | |||||||
| @@ -7,7 +7,8 @@ | |||||||
| #include <framework/ipc/MutexFactory.h> | #include <framework/ipc/MutexFactory.h> | ||||||
| #include <framework/globalfunctions/timevalOperations.h> | #include <framework/globalfunctions/timevalOperations.h> | ||||||
|  |  | ||||||
|  | typedef uint32_t millis_t; | ||||||
|  | typedef float seconds_t; | ||||||
|  |  | ||||||
| class Clock { | class Clock { | ||||||
| public: | public: | ||||||
|   | |||||||
							
								
								
									
										61
									
								
								timemanager/Stopwatch.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								timemanager/Stopwatch.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | |||||||
|  | /** | ||||||
|  |  * @file Stopwatch.cpp | ||||||
|  |  * | ||||||
|  |  * @date 08.04.2020 | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #include <framework/timemanager/Stopwatch.h> | ||||||
|  | #include <framework/serviceinterface/ServiceInterfaceStream.h> | ||||||
|  | #include <iomanip> | ||||||
|  |  | ||||||
|  | Stopwatch::Stopwatch(bool displayOnDestruction, | ||||||
|  |         StopwatchDisplayMode displayMode): displayMode(displayMode), | ||||||
|  |         displayOnDestruction(displayOnDestruction) { | ||||||
|  |     // Measures start time on initialization. | ||||||
|  |     Clock::getUptime(&startTime); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void Stopwatch::start() { | ||||||
|  |     startTime = Clock::getUptime(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | millis_t Stopwatch::stop() { | ||||||
|  |     stopInternal(); | ||||||
|  |     return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | seconds_t Stopwatch::stopSeconds() { | ||||||
|  |     stopInternal(); | ||||||
|  |     return timevalOperations::toDouble(elapsedTime); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void Stopwatch::display() { | ||||||
|  |     if(displayMode == StopwatchDisplayMode::MILLIS) { | ||||||
|  |         info << "Stopwatch: Operation took " << elapsedTime.tv_sec * 1000 + | ||||||
|  |                 elapsedTime.tv_usec * 1000 << " milliseconds" << std::endl; | ||||||
|  |     } | ||||||
|  |     else if(displayMode == StopwatchDisplayMode::SECONDS) { | ||||||
|  |         info <<"Stopwatch: Operation took "  << std::setprecision(3) | ||||||
|  |              << std::fixed << timevalOperations::toDouble(elapsedTime) | ||||||
|  |              << " seconds" << std::endl; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | Stopwatch::~Stopwatch() { | ||||||
|  |     if(displayOnDestruction) { | ||||||
|  |         stopInternal(); | ||||||
|  |         display(); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void Stopwatch::setDisplayMode(StopwatchDisplayMode displayMode) { | ||||||
|  |     this->displayMode = displayMode; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | StopwatchDisplayMode Stopwatch::getDisplayMode() const { | ||||||
|  |     return displayMode; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void Stopwatch::stopInternal() { | ||||||
|  |     elapsedTime = Clock::getUptime() - startTime; | ||||||
|  | } | ||||||
							
								
								
									
										71
									
								
								timemanager/Stopwatch.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								timemanager/Stopwatch.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,71 @@ | |||||||
|  | /** | ||||||
|  |  * @file Stopwatch.h | ||||||
|  |  * | ||||||
|  |  * @date 08.04.2020 | ||||||
|  |  */ | ||||||
|  |  | ||||||
|  | #ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ | ||||||
|  | #define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ | ||||||
|  | #include <framework/timemanager/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 | ||||||
|  |  * as a float. | ||||||
|  |  */ | ||||||
|  | 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) | ||||||
|  |      */ | ||||||
|  |     millis_t stop(); | ||||||
|  |     seconds_t stopSeconds(); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Displays the elapsed times on the osstream, depending on internal display | ||||||
|  |      * mode. | ||||||
|  |      */ | ||||||
|  |     void display(); | ||||||
|  |  | ||||||
|  |     StopwatchDisplayMode getDisplayMode() const; | ||||||
|  |     void setDisplayMode(StopwatchDisplayMode displayMode); | ||||||
|  | private: | ||||||
|  |     timeval startTime {0, 0}; | ||||||
|  |     timeval elapsedTime {0, 0}; | ||||||
|  |  | ||||||
|  |     StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS; | ||||||
|  |     bool displayOnDestruction = true; | ||||||
|  |     void stopInternal(); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #endif /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */ | ||||||
		Reference in New Issue
	
	Block a user