2021-07-13 20:22:54 +02:00
|
|
|
#include "fsfw/timemanager/Countdown.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2023-03-03 14:30:35 +01:00
|
|
|
#include "fsfw/globalfunctions/timevalOperations.h"
|
|
|
|
|
|
|
|
Countdown::Countdown(uint32_t initialTimeout, bool startImmediately) {
|
2022-04-26 09:07:03 +02:00
|
|
|
if (startImmediately) {
|
|
|
|
setTimeout(initialTimeout);
|
2022-05-11 11:11:39 +02:00
|
|
|
} else {
|
2023-03-03 14:30:35 +01:00
|
|
|
timeout.tv_sec = initialTimeout / 1000;
|
|
|
|
timeout.tv_usec = (initialTimeout % 1000) * 1000;
|
2022-04-26 09:07:03 +02:00
|
|
|
}
|
2022-03-08 11:52:33 +01:00
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2023-03-03 14:30:35 +01:00
|
|
|
Countdown::~Countdown() = default;
|
2016-06-15 23:48:41 +02:00
|
|
|
|
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);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
bool Countdown::isBusy() const { return !hasTimedOut(); }
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2023-03-03 14:30:35 +01:00
|
|
|
ReturnValue_t Countdown::resetTimer() { return setTimeoutTv(timeout); }
|
2018-07-12 16:29:32 +02:00
|
|
|
|
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(¤tTime);
|
2022-02-02 10:29:30 +01:00
|
|
|
return currentTime;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|