Browse Source

Implement timer elapsed function

also fix build
Alex Voinea 1 year ago
parent
commit
cac2b9ae40
2 changed files with 14 additions and 0 deletions
  1. 13 0
      Firmware/Timer.cpp
  2. 1 0
      Firmware/Timer.h

+ 13 - 0
Firmware/Timer.cpp

@@ -64,5 +64,18 @@ bool Timer<T>::expired(T msPeriod)
     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 short>;

+ 1 - 0
Firmware/Timer.h

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