Language: EN

libreria-arduino-multitask

Arduino MultiTask Library

The purpose of the MultiTask library is to facilitate the execution of multiple actions in a non-blocking manner, providing a certain asynchronous behavior. In a project where timed tasks are needed, we would instantiate a MultiTask object for the number of tasks we estimate we will need, and create and start them as needed.

MultiTask is initiated by indicating the capacity, the maximum number of tasks that a MultiTask can hold. The number of tasks actually used is stored in Count.

Tasks are stored in an array. Each time a task is added, the index of the added task is returned. If the buffer is full, the task is not added.

There are several types of tasks that we can add

  • Delay, does not execute an action.
  • Single, executes once.
  • Repetitive, executes N times.
  • Continuous, repeats indefinitely.
  • Alternat, executes N times, alternating between two actions.
  • AlternantContinuous, executes indefinitely, alternating between two actions.

To update the status of the tasks, we must call the Update method frequently. The Update method checks the status of all tasks and, if any have expired, executes the permanent actions.

User Manual

The MultiTask class can be instantiated as an object through one of its constructors,

MultiTask(const uint8_t capacity);

Using MultiTask

// Add tasks. Returns the number of the occupied slot
uint8_t AddDelay(int time, Action callback);
uint8_t AddSingle(int time, Action action);
uint8_t AddSingle(int time, Action action, Action callback);

uint8_t AddRepetitive(int time, Action action, unsigned int repetitions);
uint8_t AddRepetitive(int time, Action action, unsigned int repetitions, Action callback);
uint8_t AddContinuous(int time, Action action);
uint8_t AddContinuous(int time, Action action, Action callback);

uint8_t AddAlternant(int time, Action action, Action alternateAction, unsigned int repetitions);
uint8_t AddAlternant(int time, Action action, Action alternateAction, unsigned int repetitions, Action callback);
uint8_t AddAlternantContinuous(int time, Action action, Action alternateAction);
uint8_t AddAlternantContinuous(int time, Action action, Action alternateAction, Action callback);

// Update the state of the tasks
void Update();

// Cancel a task
void CancelTask(uint8_t index);

// Buffer status
uint8_t Capacity() const;
uint8_t Count() const;
bool IsFull() const;
bool IsEmpty() const;

Examples

The MultiTask library includes the following examples to illustrate its use.

  • MultiTask: Example that shows the use of some of the conversion functions.
#include "MultiTaskLib.h"

MultiTask multitask(4);

void debug(String text)
{
  Serial.print(millis());
  Serial.print('\t');
  Serial.println(text);
}

void setup()
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  multitask.AddRepetitive(500,
    []() {debug("Repetitive"); }, 5,
    []() {multitask.AddSingle(500, []()  {debug("Callback");});
  });

  multitask.AddAlternantContinuous(200,
    []() {digitalWrite(LED_BUILTIN, HIGH); }, 
    []() {digitalWrite(LED_BUILTIN, LOW); }  
  );
}

void loop()
{
  Serial.println("Main Loop");
  while (1)
  {
    multitask.Update();
  }
}

Installation

  • Download the latest version from GitHub
  • Unzip the file
  • Copy to your libraries folder (usually My Documents\Arduino\libraries)
  • Restart the Arduino IDE

github-full