Prusa-Firmware/Firmware/TimerRemaining.h

63 lines
1.4 KiB
C
Raw Normal View History

2018-06-11 19:19:58 +00:00
/**
* @file
* @author Marek Bel
*/
#ifndef TIMERREMAINING_H
#define TIMERREMAINING_H
#include "Timer.h"
#include "Arduino.h"
#include <limits.h>
class TimerRemaining : public LongTimer
{
public:
2018-06-11 21:41:36 +00:00
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();
}
2018-06-11 19:19:58 +00:00
/**
* @brief Time remaining to expiration
*
* @param msPeriod timer period in milliseconds
* @return time remaining to expiration in milliseconds
* @retval 0 Timer has expired, or was not even started.
*/
2018-06-11 21:41:36 +00:00
unsigned long remaining()
2018-06-11 19:19:58 +00:00
{
2018-06-11 21:41:36 +00:00
if (!running()) return 0;
if (expired()) return 0;
2018-06-11 19:19:58 +00:00
const unsigned long now = millis();
2018-06-11 21:41:36 +00:00
if ((started() <= started() + m_period) || (now < started()))
2018-06-11 19:19:58 +00:00
{
2018-06-11 21:41:36 +00:00
return (started() + m_period - now);
2018-06-11 19:19:58 +00:00
}
else //(now >= m_started)
{
2018-06-11 21:41:36 +00:00
return ULONG_MAX - now + (started() + m_period);
2018-06-11 19:19:58 +00:00
}
}
2018-06-11 21:41:36 +00:00
/**
* @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.
2018-06-11 19:19:58 +00:00
};
#endif // ifndef TIMERREMAINING_H