diff --git a/Firmware/Timer.h b/Firmware/Timer.h index 07d1f111..3d929fa1 100644 --- a/Firmware/Timer.h +++ b/Firmware/Timer.h @@ -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; }; diff --git a/Firmware/TimerRemaining.h b/Firmware/TimerRemaining.h new file mode 100644 index 00000000..1e684dea --- /dev/null +++ b/Firmware/TimerRemaining.h @@ -0,0 +1,39 @@ +/** + * @file + * @author Marek Bel + */ + +#ifndef TIMERREMAINING_H +#define TIMERREMAINING_H + +#include "Timer.h" +#include "Arduino.h" +#include + +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