Store timer period in TimerRemaining.
This commit is contained in:
parent
67356ce356
commit
490f0c9620
@ -23,6 +23,8 @@ public:
|
|||||||
bool running(){return m_isRunning;}
|
bool running(){return m_isRunning;}
|
||||||
bool expired(T msPeriod);
|
bool expired(T msPeriod);
|
||||||
protected:
|
protected:
|
||||||
|
T started(){return m_started;}
|
||||||
|
private:
|
||||||
bool m_isRunning;
|
bool m_isRunning;
|
||||||
T m_started;
|
T m_started;
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,18 @@
|
|||||||
class TimerRemaining : public LongTimer
|
class TimerRemaining : public LongTimer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
TimerRemaining() : m_period(){}
|
||||||
|
void start() = delete;
|
||||||
|
bool expired(unsigned long msPeriod) = delete;
|
||||||
|
/**
|
||||||
|
* @brief Start timer
|
||||||
|
* @param msPeriod Time to expire in milliseconds
|
||||||
|
*/
|
||||||
|
void start(unsigned long msPeriod)
|
||||||
|
{
|
||||||
|
m_period = msPeriod;
|
||||||
|
LongTimer::start();
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @brief Time remaining to expiration
|
* @brief Time remaining to expiration
|
||||||
*
|
*
|
||||||
@ -20,20 +32,31 @@ public:
|
|||||||
* @return time remaining to expiration in milliseconds
|
* @return time remaining to expiration in milliseconds
|
||||||
* @retval 0 Timer has expired, or was not even started.
|
* @retval 0 Timer has expired, or was not even started.
|
||||||
*/
|
*/
|
||||||
unsigned long remaining(unsigned long msPeriod)
|
unsigned long remaining()
|
||||||
{
|
{
|
||||||
if (!m_isRunning) return 0;
|
if (!running()) return 0;
|
||||||
if (expired(msPeriod)) return 0;
|
if (expired()) return 0;
|
||||||
const unsigned long now = millis();
|
const unsigned long now = millis();
|
||||||
if ((m_started <= m_started + msPeriod) || (now < m_started))
|
if ((started() <= started() + m_period) || (now < started()))
|
||||||
{
|
{
|
||||||
return (m_started + msPeriod - now);
|
return (started() + m_period - now);
|
||||||
}
|
}
|
||||||
else //(now >= m_started)
|
else //(now >= m_started)
|
||||||
{
|
{
|
||||||
return ULONG_MAX - now + (m_started + msPeriod);
|
return ULONG_MAX - now + (started() + m_period);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* @brief Timer has expired.
|
||||||
|
* @retval true Timer has expired.
|
||||||
|
* @retval false Timer has not expired.
|
||||||
|
*/
|
||||||
|
bool expired()
|
||||||
|
{
|
||||||
|
return LongTimer::expired(m_period);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
unsigned long m_period; //!< Timer period in milliseconds.
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ifndef TIMERREMAINING_H
|
#endif // ifndef TIMERREMAINING_H
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "catch.hpp"
|
#include "catch.hpp"
|
||||||
#include "../Firmware/Timer.h"
|
#include "../Firmware/Timer.h"
|
||||||
|
#include "../Firmware/TimerRemaining.h"
|
||||||
|
|
||||||
unsigned long millis()
|
unsigned long millis()
|
||||||
{
|
{
|
||||||
@ -31,4 +32,7 @@ TEST_CASE( "LongTimer tested.", "[timer]" )
|
|||||||
timer.start();
|
timer.start();
|
||||||
REQUIRE( timer.expired(1) == false );
|
REQUIRE( timer.expired(1) == false );
|
||||||
REQUIRE( timer.running() == true);
|
REQUIRE( timer.running() == true);
|
||||||
|
|
||||||
|
TimerRemaining otherTimer;
|
||||||
|
otherTimer.start(100);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user