Library to help schedule events to run regularly in the main loop. This library does not have much documentation and is not really intended for public use yet.

Dependents:   LineFollowing DeadReckoning

Files at this revision

API Documentation at this revision

Comitter:
DavidEGrayson
Date:
Thu Feb 20 18:54:56 2014 +0000
Child:
1:415086687bce
Commit message:
Initial commit. It works!

Changed in this revision

Pacer.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Pacer.h	Thu Feb 20 18:54:56 2014 +0000
@@ -0,0 +1,50 @@
+#include <mbed.h>
+
+class Pacer
+{
+    public:
+    Pacer(int32_t pace_us)
+      : pace_us(pace_us), last_time(0)
+    {
+        timer.start();
+    }
+    
+    bool ready()
+    {
+        return (time() - last_time) >= pace_us;
+    }
+    
+    // This should generally only be called when ready() is true.    
+    void advance()
+    {
+        last_time += pace_us;
+    }
+    
+    void clear()
+    {
+        last_time = time();   
+    }
+    
+    bool pace()
+    {
+        if (ready())
+        {
+            clear();
+            return true;   
+        }
+        else
+        {
+            return false;
+        }
+    }
+    
+    private:
+    uint32_t time()
+    {
+        return (uint32_t)timer.read_us();  // Cast int32_t to uint32_t.
+    }
+    
+    uint32_t pace_us;
+    uint32_t last_time;
+    Timer timer;
+};
\ No newline at end of file