Added a new very simple class to cleanly perform non blocking delays
This commit is contained in:
parent
cd2b2ba624
commit
4f265e4c79
25
src/app/NonBlockingDelay.cpp
Normal file
25
src/app/NonBlockingDelay.cpp
Normal 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;
|
||||
}
|
27
src/app/NonBlockingDelay.h
Normal file
27
src/app/NonBlockingDelay.h
Normal 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
|
Loading…
Reference in New Issue
Block a user