// Franzininho WiFi ESP-IDF Multitasking Example

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

#define LED_RED   GPIO_NUM_17
#define LED_GREEN GPIO_NUM_16
#define LED_BLUE  GPIO_NUM_15

struct led_task_parameters_t {
    gpio_num_t led_gpio;
    TickType_t blink_time;
};

static led_task_parameters_t red_led_gpio = {LED_RED, 2000};
static led_task_parameters_t green_led_gpio = {LED_GREEN, 1000};
static led_task_parameters_t blue_led_gpio = {LED_BLUE, 500};

void led_task(void *pvParameter)
{
    uint8_t led_value = 0;

    gpio_num_t led_gpio = ((led_task_parameters_t *)pvParameter)->led_gpio;
    TickType_t blink_time = ((led_task_parameters_t *)pvParameter)->blink_time;

    gpio_reset_pin(led_gpio);
    gpio_set_direction(led_gpio, GPIO_MODE_OUTPUT);

    while (1) {
        gpio_set_level(led_gpio, led_value);
        led_value = !led_value;
        vTaskDelay(blink_time / portTICK_PERIOD_MS);
    }
  
    vTaskDelete(NULL);
}

extern "C" void app_main()
{
    xTaskCreate(
        &led_task,        // Task function
        "red_led_task",   // Task name
        2048,             // Stack size in words
        &red_led_gpio,    // Pointer to parameters
        5,                // Priority
        NULL);            // Out pointer to task handle

    xTaskCreate(
        &led_task,        // Task function
        "green_led_task", // Task name
        2048,             // Stack size in words
        &green_led_gpio,  // Pointer to parameters
        5,                // Priority
        NULL);            // Out pointer to task handle

    xTaskCreate(
        &led_task,        // Task function
        "blue_led_task",  // Task name
        2048,             // stack size in words
        &blue_led_gpio,   // Pointer to parameters
        5,                // Priority
        NULL);            // Out pointer to task handle
}