// Upon activating the designated button, it facilitates the process of state toggling,
// followed by updating the value associated with the BUILDIN_LED.

#include "TCone.h"

// Define the 'BUTTON' pin.
#define BUTTON 12
// Define the task periodic in milliseconds
#define TASK_PERIOD 1
// Define the number of tasks
#define TASK_SIZE 1

// 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 = {10};

// Tasks declaration
// 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);

// Button read state
bool readState;

void setup() {
  // Set the pin mode of "BUTTON" as an input pulllup
  pinMode(BUTTON, INPUT_PULLUP);
  // Set the pin mode of "LED_BUILTIN" as an output
  pinMode(LED_BUILTIN, 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() {
  // If the task is completed,
  if (taskMember.completed) {
    // Save the previous state
    bool prevState = readState;
    // Read the button state
    readState = digitalRead(BUTTON);
    // If the previous state was true and the current state is now false,
    if (prevState & !readState) {
      // Toggle the state.
      taskMember.state = !taskMember.state;
      // Write the state value to the LED_BUILTIN.
      digitalWrite(LED_BUILTIN, taskMember.state);
    }
    // Manual Reset: Reset the elapsed time to zero
    // and mark the completed flag as false.
    Tasks.reset();
  }
}