#include <stdlib.h>

// Use only core 1 for demo purposes
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif

const int led_task_priority = 1;
const int button_task_priority = 2;
const int led_pin = 19;
const int button_pin = 2;
const int led_delay = 500;

TaskHandle_t xHandle = NULL;

bool hasChanged = false;
int previous = 1;
int current = 1;

void vLedTask(void* parameters) {
  pinMode(led_pin, OUTPUT);

  while(1) {
    digitalWrite(led_pin, HIGH);
    vTaskDelay(led_delay / portTICK_PERIOD_MS);
    digitalWrite(led_pin, LOW);
    vTaskDelay(led_delay  / portTICK_PERIOD_MS);
  }
}

void vButtonTask(void* parameters) {
  pinMode(button_pin, INPUT);

  while(1) {
    int output = digitalRead(button_pin);

    if(output) {
      vTaskResume(xHandle);
      Serial.println("Task resume");
    } else {
      vTaskSuspend(xHandle);
      Serial.println("Task suspend");
    }   
  }
}

void setup() {
  Serial.begin(9600);

  xTaskCreate(
    vLedTask,
    "LED Task",
    1024,
    NULL,
    led_task_priority,
    &xHandle
  );

  xTaskCreate(
    vButtonTask,
    "Button Task",
    1024,
    NULL,
    button_task_priority,
    NULL
  );
  
  vTaskDelete(NULL);
}

void loop() {
  
}