Added a new very simple class to cleanly perform non blocking delays

This commit is contained in:
anschrammh 2022-04-04 21:39:33 +02:00
parent cd2b2ba624
commit 4f265e4c79
2 changed files with 52 additions and 0 deletions

View File

@ -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;
}

View File

@ -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 <Arduino.h>
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