2021-07-13 20:22:54 +02:00
|
|
|
#include "fsfw/timemanager/Countdown.h"
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-04-26 09:07:03 +02:00
|
|
|
Countdown::Countdown(uint32_t initialTimeout, bool startImmediately) : timeout(initialTimeout) {
|
|
|
|
if (startImmediately) {
|
|
|
|
setTimeout(initialTimeout);
|
2022-05-11 11:11:39 +02:00
|
|
|
} else {
|
2022-04-26 09:07:03 +02:00
|
|
|
timeout = initialTimeout;
|
|
|
|
}
|
2022-03-08 11:52:33 +01:00
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
Countdown::~Countdown() {}
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2021-09-27 19:57:42 +02:00
|
|
|
ReturnValue_t Countdown::setTimeout(uint32_t milliseconds) {
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t returnValue = Clock::getUptime(&startTime);
|
|
|
|
timeout = milliseconds;
|
|
|
|
return returnValue;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Countdown::hasTimedOut() const {
|
2022-02-02 10:29:30 +01:00
|
|
|
if (uint32_t(this->getCurrentTime() - startTime) >= timeout) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2022-02-02 10:29:30 +01:00
|
|
|
ReturnValue_t Countdown::resetTimer() { return setTimeout(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
|
|
|
// We fetch the time before the if-statement
|
|
|
|
// to be sure that the return is in
|
|
|
|
// range 0 <= number <= timeout
|
|
|
|
uint32_t currentTime = this->getCurrentTime();
|
|
|
|
if (this->hasTimedOut()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return (startTime + timeout) - currentTime;
|
|
|
|
}
|
2021-09-27 19:57:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Countdown::getCurrentTime() const {
|
2022-02-02 10:29:30 +01:00
|
|
|
uint32_t currentTime;
|
|
|
|
Clock::getUptime(¤tTime);
|
|
|
|
return currentTime;
|
2018-07-12 16:29:32 +02:00
|
|
|
}
|