From 4f265e4c79db416886a0d8bf6aadcd5cb3a083cb Mon Sep 17 00:00:00 2001 From: anschrammh Date: Mon, 4 Apr 2022 21:39:33 +0200 Subject: [PATCH] Added a new very simple class to cleanly perform non blocking delays --- src/app/NonBlockingDelay.cpp | 25 +++++++++++++++++++++++++ src/app/NonBlockingDelay.h | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/app/NonBlockingDelay.cpp create mode 100644 src/app/NonBlockingDelay.h 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