fsfw/src/fsfw/timemanager/Countdown.h

98 lines
2.4 KiB
C
Raw Normal View History

2020-12-15 23:00:30 +01:00
#ifndef FSFW_TIMEMANAGER_COUNTDOWN_H_
#define FSFW_TIMEMANAGER_COUNTDOWN_H_
#include "Clock.h"
/**
2021-09-27 19:57:42 +02:00
*
* Countdown keeps track of a timespan.
2023-03-03 14:30:35 +01:00
* This class uses the system clock internally to achieve
* a high resolution. This means that the API is only partially
* resistant against time jumps. The user must take care to account
* for time jumps in some from if this relevant.
2021-09-27 19:57:42 +02:00
*
* Countdown::resetTimer restarts the timer.
* Countdown::setTimeout sets a new countdown duration and resets.
*
* Can be checked with Countdown::hasTimedOut or
* Countdown::isBusy.
*
* Countdown::timeOut will force the timer to time out.
*
*/
class Countdown {
2022-02-02 10:29:30 +01:00
public:
/**
* Constructor which sets the countdown duration in milliseconds
*
* It does not start the countdown!
* Call resetTimer or setTimeout before usage!
* Otherwise a call to hasTimedOut might return True.
*
* @param initialTimeout Countdown duration in milliseconds
* @param startImmediately Set to false if countdown should not be started immediately
2022-02-02 10:29:30 +01:00
*/
Countdown(uint32_t initialTimeout = 0, bool startImmediately = true);
2022-02-02 10:29:30 +01:00
~Countdown();
/**
* Call to set a new countdown duration.
*
* Resets the countdown!
*
* @param milliseconds new countdown duration in milliseconds
* @return Returnvalue from Clock::getUptime
*/
ReturnValue_t setTimeout(uint32_t milliseconds);
2023-03-03 14:30:35 +01:00
ReturnValue_t setTimeoutTv(timeval tv);
2022-02-02 10:29:30 +01:00
/**
* Returns true if the countdown duration has passed.
*
* @return True if the countdown has passed
* False if it is still running
*/
bool hasTimedOut() const;
/**
* Complementary to hasTimedOut.
*
* @return True if the countdown is till running
* False if it is still running
*/
bool isBusy() const;
/**
* Uses last set timeout value and restarts timer.
*/
ReturnValue_t resetTimer();
/**
* Returns the remaining milliseconds (0 if timeout)
*/
uint32_t getRemainingMillis() const;
2023-03-03 14:30:35 +01:00
uint32_t getTimeoutMs() const;
timeval getTimeout() const;
2022-02-02 10:29:30 +01:00
/**
* Makes hasTimedOut() return true
*/
void timeOut();
2023-03-03 14:30:35 +01:00
static inline uint32_t timevalToMs(timeval& tv);
private:
2022-02-02 10:29:30 +01:00
/**
2023-03-03 14:30:35 +01:00
* Start time of the countdown.
2022-02-02 10:29:30 +01:00
*/
2023-03-03 14:30:35 +01:00
timeval startTime{};
2021-09-27 19:57:42 +02:00
2022-02-02 10:29:30 +01:00
/**
2023-03-03 14:30:35 +01:00
* Timeout as timeval type. The countdown has timed out when the
* current time exceeds the start time plus the timeout.
2022-02-02 10:29:30 +01:00
*/
2023-03-03 14:30:35 +01:00
timeval timeout{};
2022-02-02 10:29:30 +01:00
2023-03-03 14:30:35 +01:00
timeval getCurrentTime() const;
};
2020-12-15 23:00:30 +01:00
#endif /* FSFW_TIMEMANAGER_COUNTDOWN_H_ */