// The device is equipped with three LED lights, each with a distinct blinking pattern.
// The RED LED blinks at a frequency of 500ms ON and 500ms OFF,
// the YELLOW LED blinks at a frequency of 1000ms ON and 1000ms OFF,
// and the GREEN LED blinks at a frequency of 2000ms ON and 2000ms OFF.

#include "TCone.h"

// Define the task periodic in milliseconds
#define TASK_PERIOD 1
// Define the number of tasks
#define TASK_SIZE 3

// TaskMember declaration
// Single:
// taskMember = {duration, manualReset, enable};
// Array:
// taskMembers[N] = {
//   {duration_0, manualReset_0, enable_0},
//   {duration_1, manualReset_1, enable_1},
//   {..., ..., ...}
//   {duration_N-1, manualReset_N-1, enable_N-1},
// };
TaskMember taskMember[TASK_SIZE] = {
  {500},
  {1000},
  {2000}
};

// Task declaration
// TCone Task (period, *tasks, size);
// Para:
// uint16_t period    - in milliseconds
// TaskMember *tasks  - Pointer of TaskMember
// uint16_t size      - Number of Tasks
// It is mandatory to name the TCone declaration as "Tasks". No other name should be used.
TCone Tasks(TASK_PERIOD, taskMember, TASK_SIZE);

// Define the 'ledPins' as a constant array.
const uint8_t ledPins[TASK_SIZE] = {4, 3, 2};

void setup() {
  // Set the pin mode of "LED_BUILTIN" as an output
  for (uint8_t index = 0; index < TASK_SIZE; index++) {
    pinMode(ledPins[index], OUTPUT);
  }

  // Manual reset when task completed.
  Tasks.manualReset(true);
  // It is imperative to utilize the begin() function to internally configure Timer0
  // and commence its operation.
  Tasks.begin();
}

void loop() {
  // Use a loop to write the value to LEDs to represent the state status.
  for (uint8_t index = 0; index < TASK_SIZE; index++) {
    // If the task is completed,
    if (taskMember[index].completed) {
      // Toggle the state.
      taskMember[index].state = !taskMember[index].state;
      // Manual Reset: Reset the elapsed time to zero
      // and mark the completed flag as false.
      Tasks.reset(index);
    }
    // Write the state value to the led.
    digitalWrite(ledPins[index], taskMember[index].state);
  }
}