Removed Timer and updated Countdown

This commit is contained in:
2021-09-27 19:57:42 +02:00
parent 1e380fe562
commit 8ec35f158c
4 changed files with 86 additions and 111 deletions

View File

@ -1,4 +1,6 @@
#include "Countdown.h"
#include "fsfw/serviceinterface/ServiceInterfaceStream.h"
Countdown::Countdown(uint32_t initialTimeout): timeout(initialTimeout) {
}
@ -6,16 +8,14 @@ Countdown::Countdown(uint32_t initialTimeout): timeout(initialTimeout) {
Countdown::~Countdown() {
}
ReturnValue_t Countdown::setTimeout(uint32_t miliseconds) {
ReturnValue_t return_value = Clock::getUptime( &startTime );
timeout = miliseconds;
return return_value;
ReturnValue_t Countdown::setTimeout(uint32_t milliseconds) {
ReturnValue_t returnValue = Clock::getUptime( &startTime );
timeout = milliseconds;
return returnValue;
}
bool Countdown::hasTimedOut() const {
uint32_t current_time;
Clock::getUptime( &current_time );
if ( uint32_t(current_time - startTime) >= timeout) {
if ( uint32_t( this->getCurrentTime() - startTime) >= timeout) {
return true;
} else {
return false;
@ -31,7 +31,23 @@ ReturnValue_t Countdown::resetTimer() {
}
void Countdown::timeOut() {
uint32_t current_time;
Clock::getUptime( &current_time );
startTime= current_time - timeout;
startTime = this->getCurrentTime() - timeout;
}
uint32_t Countdown::getRemainingMillis() const {
// 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;
}
}
uint32_t Countdown::getCurrentTime() const {
uint32_t current_time;
Clock::getUptime( &current_time );
return current_time;
}

View File

@ -4,28 +4,77 @@
#include "Clock.h"
/**
* @brief This file defines the Countdown class.
* @author baetz
*
* Countdown keeps track of a timespan.
*
* 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 {
public:
uint32_t timeout;
/**
* 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
*/
Countdown(uint32_t initialTimeout = 0);
~Countdown();
ReturnValue_t setTimeout(uint32_t miliseconds);
/**
* 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);
/**
* 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;
//!< Use last set timeout value and restart timer.
/**
* Uses last set timeout value and restarts timer.
*/
ReturnValue_t resetTimer();
//!< Make hasTimedOut() return true
/**
* Returns the remaining milliseconds (0 if timeout)
*/
uint32_t getRemainingMillis() const;
/**
* Makes hasTimedOut() return true
*/
void timeOut();
/**
* Internal countdown duration in milliseconds
*/
uint32_t timeout;
private:
/**
* Last time the timer was started (uptime)
*/
uint32_t startTime = 0;
uint32_t getCurrentTime() const;
};
#endif /* FSFW_TIMEMANAGER_COUNTDOWN_H_ */