Merge pull request 'Stopwatch-Update' (#200) from KSat/fsfw:mueller/Stopwatch-Update into master

Reviewed-on: fsfw/fsfw#200
This commit is contained in:
Steffen Gaisser 2020-09-22 14:56:06 +02:00
commit da4f6cf447
4 changed files with 32 additions and 30 deletions

View File

@ -75,24 +75,25 @@ timeval Clock::getUptime() {
} }
ReturnValue_t Clock::getUptime(timeval* uptime) { ReturnValue_t Clock::getUptime(timeval* uptime) {
//TODO This is not posix compatible and delivers only seconds precision
// is the OS not called Linux?
//Linux specific file read but more precise
double uptimeSeconds;
if(std::ifstream("/proc/uptime",std::ios::in) >> uptimeSeconds){
uptime->tv_sec = uptimeSeconds;
uptime->tv_usec = uptimeSeconds *(double) 1e6 - (uptime->tv_sec *1e6);
}
//TODO This is not posix compatible and delivers only seconds precision //TODO This is not posix compatible and delivers only seconds precision
struct sysinfo sysInfo; // I suggest this is moved into another clock function which will
int result = sysinfo(&sysInfo); // deliver second precision later.
if(result != 0){ // struct sysinfo sysInfo;
return HasReturnvaluesIF::RETURN_FAILED; // int result = sysinfo(&sysInfo);
} // if(result != 0){
uptime->tv_sec = sysInfo.uptime; // return HasReturnvaluesIF::RETURN_FAILED;
uptime->tv_usec = 0;
//Linux specific file read but more precise
// double uptimeSeconds;
// if(std::ifstream("/proc/uptime",std::ios::in) >> uptimeSeconds){
// uptime->tv_sec = uptimeSeconds;
// uptime->tv_usec = uptimeSeconds *(double) 1e6 - (uptime->tv_sec *1e6);
// } // }
// return sysInfo.uptime;
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) { ReturnValue_t Clock::getUptime(uint32_t* uptimeMs) {

View File

@ -9,8 +9,7 @@
#include <sys/time.h> #include <sys/time.h>
//! Don't use these for time points, type is not large enough for UNIX epoch. //! Don't use these for time points, type is not large enough for UNIX epoch.
typedef uint32_t dur_millis_t; using dur_millis_t = uint32_t;
typedef double dur_seconds_t;
class Clock { class Clock {
public: public:

View File

@ -6,19 +6,22 @@ Stopwatch::Stopwatch(bool displayOnDestruction,
StopwatchDisplayMode displayMode): displayOnDestruction( StopwatchDisplayMode displayMode): displayOnDestruction(
displayOnDestruction), displayMode(displayMode) { displayOnDestruction), displayMode(displayMode) {
// Measures start time on initialization. // Measures start time on initialization.
Clock::getClock_timeval(&startTime); Clock::getUptime(&startTime);
} }
void Stopwatch::start() { void Stopwatch::start() {
Clock::getClock_timeval(&startTime); Clock::getUptime(&startTime);
} }
dur_millis_t Stopwatch::stop() { dur_millis_t Stopwatch::stop(bool display) {
stopInternal(); stopInternal();
if(display) {
this->display();
}
return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000; return elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
} }
dur_seconds_t Stopwatch::stopSeconds() { double Stopwatch::stopSeconds() {
stopInternal(); stopInternal();
return timevalOperations::toDouble(elapsedTime); return timevalOperations::toDouble(elapsedTime);
} }
@ -52,6 +55,6 @@ StopwatchDisplayMode Stopwatch::getDisplayMode() const {
void Stopwatch::stopInternal() { void Stopwatch::stopInternal() {
timeval endTime; timeval endTime;
Clock::getClock_timeval(&endTime); Clock::getUptime(&endTime);
elapsedTime = endTime - startTime; elapsedTime = endTime - startTime;
} }

View File

@ -1,5 +1,6 @@
#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ #ifndef FSFW_TIMEMANAGER_STOPWATCH_H_
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ #define FSFW_TIMEMANAGER_STOPWATCH_H_
#include "Clock.h" #include "Clock.h"
enum class StopwatchDisplayMode { enum class StopwatchDisplayMode {
@ -40,12 +41,12 @@ public:
* Calculates the elapsed time since start and returns it * Calculates the elapsed time since start and returns it
* @return elapsed time in milliseconds (rounded) * @return elapsed time in milliseconds (rounded)
*/ */
dur_millis_t stop(); dur_millis_t stop(bool display = false);
/** /**
* Calculates the elapsed time since start and returns it * Calculates the elapsed time since start and returns it
* @return elapsed time in seconds (double precision) * @return elapsed time in seconds (double precision)
*/ */
dur_seconds_t stopSeconds(); double stopSeconds();
/** /**
* Displays the elapsed times on the osstream, depending on internal display * Displays the elapsed times on the osstream, depending on internal display
@ -66,6 +67,4 @@ private:
}; };
#endif /* FSFW_TIMEMANAGER_STOPWATCH_H_ */
#endif /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */