#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <driver/gpio.h>

#define LED_RED 5
#define LED_GREEN 4
#define LED_BLUE 2

void blink_task(void *pvParameters) {
  int pin = (int)pvParameters; // Lấy chân GPIO từ tham số truyền vào
  int delay_time = (pin == LED_RED) ? 1000 : (pin == LED_GREEN) ? 2000 : 4000;

  while (true) {
    gpio_set_level(pin, 1);           // Bật LED
    vTaskDelay(delay_time / portTICK_PERIOD_MS);
    gpio_set_level(pin, 0);           // Tắt LED
    vTaskDelay(delay_time / portTICK_PERIOD_MS);
  }
}

void app_main() {
  // Cấu hình các chân GPIO làm đầu ra
  gpio_reset_pin(LED_RED);
  gpio_set_direction(LED_RED, GPIO_MODE_OUTPUT);

  gpio_reset_pin(LED_GREEN);
  gpio_set_direction(LED_GREEN, GPIO_MODE_OUTPUT);

  gpio_reset_pin(LED_BLUE);
  gpio_set_direction(LED_BLUE, GPIO_MODE_OUTPUT);

  // Tạo các task nhấp nháy LED
  xTaskCreate(blink_task, "Blink Red LED", 1024, (void *)LED_RED, 1, NULL);
  xTaskCreate(blink_task, "Blink Green LED", 1024, (void *)LED_GREEN, 1, NULL);
  xTaskCreate(blink_task, "Blink Blue LED", 1024, (void *)LED_BLUE, 1, NULL);
}