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
Diff: Pacer.h
- Revision:
- 0:ffaf699a4eea
- Child:
- 1:415086687bce
--- /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