The Timeout Task library allows you to evaluate a specific condition for a user-specified interval of microseconds, and execute actions based on the result obtained during the wait.
The Timeout Task class is designed for the execution of computationally expensive tasks without affecting the main execution loop. For example, in communications, we can wait for a start signal for a brief period, and if the signal is received, initiate the actual reception/transmission process. Another example could be checking if the state of certain inputs is as expected, and if so, perform a calculation or initiate an actuator.
On the other hand, Timeout Task is also useful for easily implementing a watchdog. For example, turning on a motor for a certain time and waiting for it to reach an end-of-travel button. If a reasonable amount of time elapses, stop the motor for safety.
A Timeout Task instance has a condition and two callback functions, OkCallback and TimeOutCallback. When the task is executed, the condition is continuously evaluated for the specified time. If the condition becomes true at any point, the OkCallback function is executed. If, on the other hand, the Timeout time expires, the TimeOutCallback action is executed, and control flow is returned.
There are two ways to use the library. One is to instantiate an object of the TimeoutTask class and call its Run() method. The alternative is to call the static Run() method directly.
User Manual
Object Usage
The TimeoutTask class can be instantiated as an object through its constructor, and activated with the Run() method.
// Constructor
TimeoutTask(unsigned long timeout, bool(*conditionFunc)(),
void(*okCallback)(),
void(*timeoutCallback)())
{
}
// Execute the TimeoutTask
void Run() const;
Usage with Static Method
Alternatively, we can execute the TimeoutTAsk through the static Run() method.
static void Run(unsigned long timeout,
ConditionFunc conditionFunc,
CallbackAction okCallback,
CallbackAction timeoutCallback);
Examples
The Timeout Task library includes the following examples to illustrate its use.
- TimeoutTask: Example as an instance acting as a Timeout
#include "TimeoutTask.h"
const size_t dataLength = 10;
int data[dataLength];
const int bytesLengths = dataLength * sizeof data[0];
TimeoutTask timeoutTask(50000,
[]() { return Serial.available() >= dataLength; },
[]() { Serial.readBytes(reinterpret_cast<byte*>(data), dataLength * sizeof data[0]); },
nullPtr);
void setup()
{
timeoutTask.Run();
}
void loop()
{
}
- StaticTimeOutTask: Example with static Run() method
#include "TimeoutTask.h"
const size_t dataLength = 10;
int data[dataLength];
const int bytesLengths = dataLength * sizeof data[0];
void setup()
{
TimeoutTask::Run(50000,
[]() { return Serial.available() >= dataLength; },
[]() { Serial.readBytes(reinterpret_cast<byte*>(data), dataLength * sizeof data[0]); },
[]() {});
}
void loop()
{
}
- Watchdog: Example as a static method acting as a Watchdog
#include "TimeoutTask.h"
const int pinA = 8;
const int pinB = 9;
void moveActuator() {}
void stopActuator() {}
void setup()
{
TimeoutTask::Run(10000,
[]() { return digitalRead(pinA) && digitalRead(pinB); },
moveActuator,
stopActuator);
}
void loop()
{
}
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy it to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE