// 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


// Use only core 1
static const BaseType_t app1_cpu = 1;
static const BaseType_t app2_cpu = 1;
static const BaseType_t app3_cpu = 1;
static const BaseType_t app4_cpu = 1;


static const int led_pin = 18;
static const int led_pin1 = 5;
static const int led_pin2 = 19;
static const int buzzer = 3;
static const int pir = 33;


void OpenDoor(void *param)
{
  if (digitalRead(pir) == HIGH)
  {
    toggleRedLED(NULL);
    vTaskDelay(1500 / portTICK_PERIOD_MS);
    digitalWrite(led_pin1, LOW);
  }
}

void toggleBlueLED(void *param)
{
  while (1)
  {
    digitalWrite(led_pin, HIGH);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    digitalWrite(led_pin, LOW);
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}

void toggleRedLED(void *param)
{
  while (1)
  {
    digitalWrite(led_pin1, HIGH);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    digitalWrite(led_pin1, LOW);
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}

void toggleYellowLED(void *param)
{
  while (1)
  {
    digitalWrite(led_pin2, HIGH);
    vTaskDelay(500 / portTICK_PERIOD_MS);
    digitalWrite(led_pin2, LOW);
    vTaskDelay(500 / portTICK_PERIOD_MS);
  }
}



void setup(void)
{
  pinMode(led_pin, OUTPUT);
  pinMode(led_pin1, OUTPUT);
  pinMode(led_pin2, OUTPUT);
  pinMode(pir, INPUT);
  pinMode(buzzer, OUTPUT);

  digitalWrite(pir, LOW);
  digitalWrite(buzzer, LOW);



  xTaskCreatePinnedToCore (
    toggleBlueLED, // Function to call
    "Toggle", // Task name
    1024, // Stack size in bytes
    NULL, // Parameter to pass
    1, // Task priority (0 to config MAX_PRIORITIES)
    NULL, // Task handle from another one
    app1_cpu // cpu core we want the task to run in (Not present in vanillaFreeRTOS)
  );

  xTaskCreatePinnedToCore (
    toggleYellowLED,
    "ToggleYellow",
    1024,
    NULL,
    1,
    NULL,
    app3_cpu
  );

  xTaskCreatePinnedToCore (
    OpenDoor,
    "Open",
    1024,
    NULL,
    1,
    NULL,
    app4_cpu
  );
}

void loop(void)
{

}