TimerRemaining.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "system_timer.h"
  10. #include <limits.h>
  11. class TimerRemaining : public LongTimer
  12. {
  13. public:
  14. TimerRemaining() : m_period(){}
  15. void start() = delete;
  16. bool expired(unsigned long msPeriod) = delete;
  17. /**
  18. * @brief Start timer
  19. * @param msPeriod Time to expire in milliseconds
  20. */
  21. void start(unsigned long msPeriod)
  22. {
  23. m_period = msPeriod;
  24. LongTimer::start();
  25. }
  26. /**
  27. * @brief Time remaining to expiration
  28. *
  29. * @param msPeriod timer period in milliseconds
  30. * @return time remaining to expiration in milliseconds
  31. * @retval 0 Timer has expired, or was not even started.
  32. */
  33. unsigned long remaining()
  34. {
  35. if (!running()) return 0;
  36. if (expired()) return 0;
  37. const unsigned long now = _millis();
  38. return (started() + m_period - now);
  39. }
  40. /**
  41. * @brief Timer has expired.
  42. * @retval true Timer has expired.
  43. * @retval false Timer has not expired.
  44. */
  45. bool expired()
  46. {
  47. return LongTimer::expired(m_period);
  48. }
  49. private:
  50. unsigned long m_period; //!< Timer period in milliseconds.
  51. };
  52. #endif // ifndef TIMERREMAINING_H