2018-06-05 16:27:36 +00:00
|
|
|
/**
|
2018-03-12 22:35:50 +00:00
|
|
|
* @file
|
|
|
|
* @author Marek Bel
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TIMER_H
|
|
|
|
#define TIMER_H
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief simple timer
|
|
|
|
*
|
|
|
|
* Simple and memory saving implementation. Should handle timer register wrap around well.
|
2018-05-11 14:31:42 +00:00
|
|
|
* Resolution is one millisecond. To save memory, doesn't store timer period.
|
|
|
|
* If you wish timer which is storing period, derive from this.
|
2018-03-12 22:35:50 +00:00
|
|
|
*/
|
2018-05-11 14:31:42 +00:00
|
|
|
template <class T>
|
2018-03-12 22:35:50 +00:00
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Timer();
|
|
|
|
void start();
|
|
|
|
void stop(){m_isRunning = false;}
|
2021-01-20 10:46:25 +00:00
|
|
|
bool running()const {return m_isRunning;}
|
2018-05-11 14:31:42 +00:00
|
|
|
bool expired(T msPeriod);
|
2022-07-02 19:09:36 +00:00
|
|
|
T elapsed();
|
2018-06-11 19:19:58 +00:00
|
|
|
protected:
|
2021-01-20 10:46:25 +00:00
|
|
|
T started()const {return m_started;}
|
2018-06-11 21:41:36 +00:00
|
|
|
private:
|
2018-03-12 22:35:50 +00:00
|
|
|
bool m_isRunning;
|
2018-05-11 14:31:42 +00:00
|
|
|
T m_started;
|
2018-03-12 22:35:50 +00:00
|
|
|
};
|
|
|
|
|
2018-05-11 14:31:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Timer unsigned long specialization
|
|
|
|
*
|
|
|
|
* Maximum period is at least 49 days.
|
|
|
|
*/
|
2018-06-05 19:55:54 +00:00
|
|
|
#if __cplusplus>=201103L
|
2018-05-11 14:31:42 +00:00
|
|
|
using LongTimer = Timer<unsigned long>;
|
2018-06-05 19:55:54 +00:00
|
|
|
#else
|
|
|
|
typedef Timer<unsigned long> LongTimer;
|
|
|
|
#endif
|
2018-05-11 14:31:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Timer unsigned short specialization
|
|
|
|
*
|
|
|
|
* Maximum period is at least 65 seconds.
|
|
|
|
*/
|
2018-06-05 19:55:54 +00:00
|
|
|
#if __cplusplus>=201103L
|
2018-05-11 14:31:42 +00:00
|
|
|
using ShortTimer = Timer<unsigned short>;
|
2018-06-05 19:55:54 +00:00
|
|
|
#else
|
|
|
|
typedef Timer<unsigned short> ShortTimer;
|
|
|
|
#endif
|
2018-05-11 14:31:42 +00:00
|
|
|
|
2018-03-12 22:35:50 +00:00
|
|
|
#endif /* TIMER_H */
|