2018-06-11 19:19:58 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @author Marek Bel
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TIMERREMAINING_H
|
|
|
|
#define TIMERREMAINING_H
|
|
|
|
|
|
|
|
#include "Timer.h"
|
2019-01-28 18:29:31 +00:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "system_timer.h"
|
2018-06-11 19:19:58 +00:00
|
|
|
#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;
|
2019-01-27 21:48:51 +00:00
|
|
|
const unsigned long now = _millis();
|
2018-06-12 00:24:52 +00:00
|
|
|
return (started() + m_period - now);
|
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
|