#include <Arduino.h>
#include "magxTasksCoordinator.h"
/**
* author: Alex Gabriel Malisa
* email : [email protected]
*
*
* simpleTask: In this task, we create 1 task:
* 1. LED blinker on pin 13
* This task will blink the LED at a frequency of 10Hz (100ms).
* 2. LED_OFF_TIMER
* This task will control amount of time an LED stays OFF
* timeOutMs for both tasks will be different to give a feel of asymetric control
*/
// Create magxTasksCoordinator object
/**
* Expected arguments:
* timeOutMs - This is the time in milliseconds after which the callback will be invoked.
* NOTE: This object can receive a few more arguments, but I limit to 1 arg for this example's scope.
*/
magxTasksCoordinator LED_ON_TIMER(400); //LED will be on for 400ms
magxTasksCoordinator LED_OFF_TIMER(3000); //LED will be off for 3000ms
magxTasksManager manager;
// Then define timerType
/**
* This is the enum class with only two elements { Periodic, OneShot }
* Periodic means the event will be repeated continuously at an interval set by the timeoutMs variable.
* OneShot means the event will execute only once after timeoutMs has elapsed.
*/
// Define the callback function for LED_ON_TIMER task
void LED_ON_TIMER_CB(){
digitalWrite(LED_BUILTIN,LOW);
LED_OFF_TIMER.start();
}
// Define the callback function for LED_OFF_TIMER task
void LED_OFF_TIMER_CB(){
digitalWrite(LED_BUILTIN,HIGH);
LED_ON_TIMER.start();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize LED pin
pinMode(LED_BUILTIN, OUTPUT);
// Set the callback function for LED_ blinking task
LED_ON_TIMER.setCallback(LED_ON_TIMER_CB);
// Set the callback function for LED blinking task
LED_OFF_TIMER.setCallback(LED_OFF_TIMER_CB);
// Set the timer type (Periodic or OneShot)
// Example:
// LED_ON_TIMER.setType(magxTaskCoordinator::OneShot);
LED_ON_TIMER.setType(magxTasksCoordinator::OneShot);
LED_OFF_TIMER.setType(magxTasksCoordinator::OneShot);
manager.addTimer(&LED_ON_TIMER);
manager.addTimer(&LED_OFF_TIMER);
// Start the LED LED_ON_TIMER task
LED_ON_TIMER.start();
}
void loop() {
// Update the task coordinator
/**
* magxTasksCoordinator class has the method to update time.
* LED_ON_TIMER.update();
* LED_OFF_TIMER.update();
* ..
* ..
* ..
* 10TH_TIMER.update();
*
* this method is practical when only 1 task is defined iny our sketch..
*
* NOTE: The magxTasksManager class can register all instances of tasks and update time
* using only one line off code.
*/
manager.updateTimers();
}