TimerRemaining.h 896 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. /**
  14. * @brief Time remaining to expiration
  15. *
  16. * @param msPeriod timer period in milliseconds
  17. * @return time remaining to expiration in milliseconds
  18. * @retval 0 Timer has expired, or was not even started.
  19. */
  20. unsigned long remaining(unsigned long msPeriod)
  21. {
  22. if (!m_isRunning) return 0;
  23. if (expired(msPeriod)) return 0;
  24. const unsigned long now = millis();
  25. if ((m_started <= m_started + msPeriod) || (now < m_started))
  26. {
  27. return (m_started + msPeriod - now);
  28. }
  29. else //(now >= m_started)
  30. {
  31. return ULONG_MAX - now + (m_started + msPeriod);
  32. }
  33. }
  34. };
  35. #endif // ifndef TIMERREMAINING_H