Implement timer elapsed function

also fix build
This commit is contained in:
Alex Voinea 2022-07-02 21:09:36 +02:00
parent 490754a0f0
commit cac2b9ae40
2 changed files with 14 additions and 0 deletions

View File

@ -64,5 +64,18 @@ bool Timer<T>::expired(T msPeriod)
return expired; return expired;
} }
/**
* @brief Ticks since the timer was started
*
* This function returns 0 if the timer is not started. Otherwise, it returns
* the time in milliseconds since the timer was started.
* This function is expected to handle wrap around of time register well.
* The maximum elapsed time is dictated by the template type
*/
template<typename T>
T Timer<T>::elapsed() {
return m_isRunning ? (_millis() - m_started) : 0;
}
template class Timer<unsigned long>; template class Timer<unsigned long>;
template class Timer<unsigned short>; template class Timer<unsigned short>;

View File

@ -22,6 +22,7 @@ public:
void stop(){m_isRunning = false;} void stop(){m_isRunning = false;}
bool running()const {return m_isRunning;} bool running()const {return m_isRunning;}
bool expired(T msPeriod); bool expired(T msPeriod);
T elapsed();
protected: protected:
T started()const {return m_started;} T started()const {return m_started;}
private: private: