Katsuhiro Morishita / TimerExtended

Dependents:   gps_com

Files at this revision

API Documentation at this revision

Comitter:
morimoriYNCT
Date:
Tue Jul 15 04:47:11 2014 +0000
Child:
1:2441ccdcd627
Commit message:
24????????????????????????????????????????????????mbed???Timer????2148????????????????????????...

Changed in this revision

TimerExtended.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TimerExtended.h	Tue Jul 15 04:47:11 2014 +0000
@@ -0,0 +1,49 @@
+#include "mbed.h"
+
+
+class TimerExtended
+{
+private:
+    Timer _t;
+    Ticker _tk;
+    long milli_second;
+    
+    void update()
+    {
+        if(this->milli_second < 2147483645)
+            this->milli_second += this->_t.read_ms();
+        this->_t.reset();
+    }
+public:
+    TimerExtended()
+    {
+        this->milli_second = 0l;
+    }
+    
+    // start timer
+    void start()
+    {
+        this->_tk.attach(this, &TimerExtended::update, 2000.0);  // 2000.0 should be lower then max value of Timer obj.
+        this->_t.start();
+    }
+    
+    // like Arduino lib.
+    long millis()
+    {
+        (void)this->update();
+        return this->milli_second;
+    }
+    
+    // return time [s] in double
+    double read()
+    {
+        return (double)this->millis() / 1000.0;
+    }
+    
+    // return time [ms]
+    long read_ms()
+    {
+        return this->millis();
+    }
+    
+};
\ No newline at end of file