The Storyboard library implements a series of timed tasks that execute sequentially after a certain time has elapsed in milliseconds. It is useful for giving a certain asynchronous behavior to our programs.
The Storyboard library contains a series of timed actions that are executed sequentially one after the other.
In a real project, for example, our assembly would receive a series of commands (such as the position and speed through which an actuator has to pass) and we would use a Storyboard to execute the actions repeatedly at the desired moments.
A StoryBoard object is instantiated by indicating in the constructor its capacity, the maximum number of tasks it can contain. The Count function indicates the number of tasks that are queued in the StoryBoard.
Each task has a time interval in milliseconds and an associated callback function. When the task expires, the callback function is executed, and the next task in the StoryBoard is executed.
The intervals of each task are in series. For example, 3 tasks of 2000 milliseconds will be executed at 2, 4, and 6 seconds respectively (not all at 2 seconds simultaneously).
There are various types of tasks that we can add
- Delay, does not execute an action.
- Single, is executed only once.
- Repetitive, is executed N times.
- Continuous, repeats indefinitely.
- Alternant, is executed N times, alternating between two actions.
- AlternantContinous, is executed 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.
The tasks are stored in a circular buffer. If the buffer is full, the task is not added.
Usage Manual
The Storyboard class can be instantiated as an object through one of its constructors,
StoryBoard(const size_t capacity);
Using Storyboard
void AddDelay(int time);
void AddDelay(int time, Action callback);
void AddSingle(int time, Action action);
void AddSingle(int time, Action action, Action callback);
void AddRepetitive(int time, Action action, unsigned int repetitions);
void AddRepetitive(int time, Action action, unsigned int repetitions, Action callback);
void AddContinuous(int time, Action action);
void AddContinuous(int time, Action action, Action callback);
void AddAlternant(int time, Action action, Action alternateAction, unsigned int repetitions);
void AddAlternant(int time, Action action, Action alternateAction, unsigned int repetitions, Action callback);
void AddAlternantContinuous(int time, Action action, Action alternateAction);
void AddAlternantContinuous(int time, Action action, Action alternateAction, Action callback);
// Update the task status
void Update();
// Buffer status
size_t Capacity() const;
size_t Count() const;
bool IsFull() const;
bool IsEmpty() const;
Examples
The Storyboard library includes the following examples to illustrate its use.
- Storyboard: Example that shows the use of some of the conversion functions.
#include "StoryBoard.h"
StoryBoard storyBoard(10);
void debug(String text)
{
Serial.print(millis());
Serial.print('\t');
Serial.println(text);
}
void setup()
{
Serial.begin(9600);
storyBoard.AddSingle(500, []() {debug("Debug1");});
storyBoard.AddDelay(1500);
storyBoard.AddRepetitive(500,
[]() {debug("Debug2");}, 3,
[]() {storyBoard.AddAlternant(500, []() {debug("Alternant1");}, []() {debug("Alternant2");}, 2);});
}
void loop()
{
storyBoard.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