Timer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * @file
  3. * @author Marek Bel
  4. */
  5. #ifndef TIMER_H
  6. #define TIMER_H
  7. /**
  8. * @brief simple timer
  9. *
  10. * Simple and memory saving implementation. Should handle timer register wrap around well.
  11. * Resolution is one millisecond. To save memory, doesn't store timer period.
  12. * If you wish timer which is storing period, derive from this.
  13. */
  14. template <class T>
  15. class Timer
  16. {
  17. public:
  18. Timer();
  19. void start();
  20. void stop(){m_isRunning = false;}
  21. bool running()const {return m_isRunning;}
  22. bool expired(T msPeriod); // returns true only once after expiration, then stops running
  23. T elapsed(); // returns the time in milliseconds since the timer was started or 0 otherwise
  24. bool expired_cont(T msPeriod); // return true when continuosly when expired / not running
  25. protected:
  26. T started()const {return m_started;}
  27. private:
  28. bool m_isRunning;
  29. T m_started;
  30. };
  31. /**
  32. * @brief Timer unsigned long specialization
  33. *
  34. * Maximum period is at least 49 days.
  35. */
  36. #if __cplusplus>=201103L
  37. using LongTimer = Timer<unsigned long>;
  38. #else
  39. typedef Timer<unsigned long> LongTimer;
  40. #endif
  41. /**
  42. * @brief Timer unsigned short specialization
  43. *
  44. * Maximum period is at least 65 seconds.
  45. */
  46. #if __cplusplus>=201103L
  47. using ShortTimer = Timer<unsigned short>;
  48. #else
  49. typedef Timer<unsigned short> ShortTimer;
  50. #endif
  51. #endif /* TIMER_H */