diff --git a/src/app/NonBlockingDelay.cpp b/src/app/NonBlockingDelay.cpp new file mode 100644 index 0000000..f849885 --- /dev/null +++ b/src/app/NonBlockingDelay.cpp @@ -0,0 +1,25 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created on : 03/04/2022 + * Licence : MIT + * + * Dead simple object implementing a non blocking delay using the Arduino framework. + */ +#include "NonBlockingDelay.h" + +NonBlockingDelay::NonBlockingDelay(const unsigned long delay, unsigned long tickReference) : _delay(delay), _tickReference(tickReference){} + +void NonBlockingDelay::reset() +{ + _tickReference = millis(); +} + +NonBlockingDelay::operator bool() +{ + bool isTimeElapsed(millis() - _tickReference > _delay); + + if(isTimeElapsed) + reset(); + + return isTimeElapsed; +} \ No newline at end of file diff --git a/src/app/NonBlockingDelay.h b/src/app/NonBlockingDelay.h new file mode 100644 index 0000000..3a9ffa8 --- /dev/null +++ b/src/app/NonBlockingDelay.h @@ -0,0 +1,27 @@ +/** + * Author : Anatole SCHRAMM-HENRY + * Created on : 03/04/2022 + * Licence : MIT + * + * Dead simple object implementing a non blocking delay using the Arduino framework. + */ +#ifndef NONBLOCKINGDELAY_H +#define NONBLOCKINGDELAY_H + +#include + +class NonBlockingDelay +{ + public: + NonBlockingDelay(const unsigned long delay, unsigned long tickReference = millis()); + + // Manually reset the internal tick reference + void reset(); + operator bool(); + protected: + private: + const unsigned long _delay; + unsigned long _tickReference; +}; + +#endif //NONBLOCKINGDELAY_H \ No newline at end of file