Add TimerRemaining class.

This commit is contained in:
Marek Bel 2018-06-11 21:19:58 +02:00
parent e559404448
commit 9772905ade
2 changed files with 40 additions and 1 deletions

View File

@ -22,7 +22,7 @@ public:
void stop(){m_isRunning = false;} void stop(){m_isRunning = false;}
bool running(){return m_isRunning;} bool running(){return m_isRunning;}
bool expired(T msPeriod); bool expired(T msPeriod);
private: protected:
bool m_isRunning; bool m_isRunning;
T m_started; T m_started;
}; };

39
Firmware/TimerRemaining.h Normal file
View File

@ -0,0 +1,39 @@
/**
* @file
* @author Marek Bel
*/
#ifndef TIMERREMAINING_H
#define TIMERREMAINING_H
#include "Timer.h"
#include "Arduino.h"
#include <limits.h>
class TimerRemaining : public LongTimer
{
public:
/**
* @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.
*/
unsigned long remaining(unsigned long msPeriod)
{
if (!m_isRunning) return 0;
if (expired(msPeriod)) return 0;
const unsigned long now = millis();
if ((m_started <= m_started + msPeriod) || (now < m_started))
{
return (m_started + msPeriod - now);
}
else //(now >= m_started)
{
return ULONG_MAX - now + (m_started + msPeriod);
}
}
};
#endif // ifndef TIMERREMAINING_H