TimerRemaining.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. return (started() + m_period - now);
  38. }
  39. /**
  40. * @brief Timer has expired.
  41. * @retval true Timer has expired.
  42. * @retval false Timer has not expired.
  43. */
  44. bool expired()
  45. {
  46. return LongTimer::expired(m_period);
  47. }
  48. private:
  49. unsigned long m_period; //!< Timer period in milliseconds.
  50. };
  51. #endif // ifndef TIMERREMAINING_H