fsfw/src/fsfw/timemanager/Countdown.cpp

63 lines
1.7 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/timemanager/Countdown.h"
2023-03-03 14:30:35 +01:00
#include "fsfw/globalfunctions/timevalOperations.h"
Countdown::Countdown(uint32_t initialTimeout, bool startImmediately) {
if (startImmediately) {
setTimeout(initialTimeout);
} else {
2023-03-03 14:30:35 +01:00
timeout.tv_sec = initialTimeout / 1000;
timeout.tv_usec = (initialTimeout % 1000) * 1000;
}
2022-03-08 11:52:33 +01:00
}
2023-03-03 14:30:35 +01:00
Countdown::~Countdown() = default;
2021-09-27 19:57:42 +02:00
ReturnValue_t Countdown::setTimeout(uint32_t milliseconds) {
2023-03-03 14:30:35 +01:00
timeout.tv_sec = milliseconds / 1000;
timeout.tv_usec = (milliseconds % 1000) * 1000;
return Clock::getClock_timeval(&startTime);
}
bool Countdown::hasTimedOut() const {
2023-03-03 14:30:35 +01:00
// Account for system clock going back in time.
if (getCurrentTime() < startTime) {
2022-02-02 10:29:30 +01:00
return true;
}
2023-03-03 14:30:35 +01:00
if (getCurrentTime() - startTime >= timeout) {
return true;
}
return false;
}
2022-02-02 10:29:30 +01:00
bool Countdown::isBusy() const { return !hasTimedOut(); }
2023-03-03 14:30:35 +01:00
ReturnValue_t Countdown::resetTimer() { return setTimeoutTv(timeout); }
2022-02-02 10:29:30 +01:00
void Countdown::timeOut() { startTime = this->getCurrentTime() - timeout; }
2021-09-27 19:57:42 +02:00
uint32_t Countdown::getRemainingMillis() const {
2022-02-02 10:29:30 +01:00
if (this->hasTimedOut()) {
return 0;
}
2023-03-03 14:30:35 +01:00
timeval remainingMillisTv = (startTime + timeout) - this->getCurrentTime();
return remainingMillisTv.tv_sec * 1000 + remainingMillisTv.tv_usec / 1000;
2021-09-27 19:57:42 +02:00
}
2023-03-03 14:30:35 +01:00
uint32_t Countdown::timevalToMs(timeval &tv) { return tv.tv_sec * 1000 + tv.tv_usec / 1000; }
ReturnValue_t Countdown::setTimeoutTv(timeval tv) {
timeout = tv;
return Clock::getClock_timeval(&startTime);
}
uint32_t Countdown::getTimeoutMs() const { return timeout.tv_sec * 1000 + timeout.tv_usec / 1000; }
timeval Countdown::getTimeout() const { return timeout; }
timeval Countdown::getCurrentTime() const {
timeval currentTime{};
Clock::getClock_timeval(&currentTime);
2022-02-02 10:29:30 +01:00
return currentTime;
}