TimerRemaining.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @file
  3. * @author Marek Bel
  4. */
  5. #ifndef TIMERREMAINING_H
  6. #define TIMERREMAINING_H
  7. #include "Timer.h"
  8. #include "Arduino.h"
  9. #include <limits.h>
  10. class TimerRemaining : public LongTimer
  11. {
  12. public:
  13. TimerRemaining() : m_period(){}
  14. void start() = delete;
  15. bool expired(unsigned long msPeriod) = delete;
  16. /**
  17. * @brief Start timer
  18. * @param msPeriod Time to expire in milliseconds
  19. */
  20. void start(unsigned long msPeriod)
  21. {
  22. m_period = msPeriod;
  23. LongTimer::start();
  24. }
  25. /**
  26. * @brief Time remaining to expiration
  27. *
  28. * @param msPeriod timer period in milliseconds
  29. * @return time remaining to expiration in milliseconds
  30. * @retval 0 Timer has expired, or was not even started.
  31. */
  32. unsigned long remaining()
  33. {
  34. if (!running()) return 0;
  35. if (expired()) return 0;
  36. const unsigned long now = millis();
  37. if ((started() <= started() + m_period) || (now < started()))
  38. {
  39. return (started() + m_period - now);
  40. }
  41. else //(now >= m_started)
  42. {
  43. return ULONG_MAX - now + (started() + m_period);
  44. }
  45. }
  46. /**
  47. * @brief Timer has expired.
  48. * @retval true Timer has expired.
  49. * @retval false Timer has not expired.
  50. */
  51. bool expired()
  52. {
  53. return LongTimer::expired(m_period);
  54. }
  55. private:
  56. unsigned long m_period; //!< Timer period in milliseconds.
  57. };
  58. #endif // ifndef TIMERREMAINING_H