Add TimerRemaining class.
This commit is contained in:
parent
e559404448
commit
9772905ade
@ -22,7 +22,7 @@ public:
|
||||
void stop(){m_isRunning = false;}
|
||||
bool running(){return m_isRunning;}
|
||||
bool expired(T msPeriod);
|
||||
private:
|
||||
protected:
|
||||
bool m_isRunning;
|
||||
T m_started;
|
||||
};
|
||||
|
39
Firmware/TimerRemaining.h
Normal file
39
Firmware/TimerRemaining.h
Normal 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
|
Loading…
Reference in New Issue
Block a user