A class to call a function at the main function at regular intervals (using Ticker class)
Dependents: GPS_0002 optWingforHAPS_Eigen hexaTest_Eigen
Revision 0:d9fd30e1ebe4, committed 2021-01-21
- Comitter:
- cocorlow
- Date:
- Thu Jan 21 15:45:43 2021 +0000
- Commit message:
- LoopTicker
Changed in this revision
LoopTicker.cpp | Show annotated file Show diff for this revision Revisions of this file |
LoopTicker.hpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r d9fd30e1ebe4 LoopTicker.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LoopTicker.cpp Thu Jan 21 15:45:43 2021 +0000 @@ -0,0 +1,38 @@ +#include "mbed.h" +#include "LoopTicker.hpp" + +LoopTicker::LoopTicker() +{ + fptr = NULL; + time = 0.0f; + updated = false; +} + +void LoopTicker::attach(void (*fptr_)(), float time_) +{ + fptr = fptr_; + time = time_; + ticker_.attach(this, &LoopTicker::interrupt, time); +} + +void LoopTicker::detach() +{ + fptr = NULL; + time = 0.0f; + updated = false; + ticker_.detach(); +} + +void LoopTicker::loop() +{ + if (updated) + { + fptr(); + updated = false; + } +} + +void LoopTicker::interrupt() +{ + updated = true; +} \ No newline at end of file
diff -r 000000000000 -r d9fd30e1ebe4 LoopTicker.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LoopTicker.hpp Thu Jan 21 15:45:43 2021 +0000 @@ -0,0 +1,21 @@ +#ifndef _LOOPTICKER_HPP_ +#define _LOOPTICKER_HPP_ + +#include "mbed.h" + +class LoopTicker +{ +public: + LoopTicker(); + void attach(void (*fptr_)(), float time_); + void detach(); + void loop(); +private: + Ticker ticker_; + void (*fptr)(); + float time; + bool updated; + void interrupt(); +}; + +#endif \ No newline at end of file