#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#define LED_PIN 5
#define SWITCH1_PIN 25
#define SWITCH2_PIN 26
SemaphoreHandle_t accessSemaphore;
void switch1_isr_handler(void* arg)
{
xSemaphoreGiveFromISR(accessSemaphore, NULL);
}
void switch2_isr_handler(void* arg)
{
xSemaphoreGiveFromISR(accessSemaphore, NULL);
}
void task1(void* pvParameters)
{
while(1)
{
if (gpio_get_level(SWITCH1_PIN))
{
if (xSemaphoreTake(accessSemaphore, portMAX_DELAY)==pdTRUE)
{
gpio_set_level(LED_PIN, 1);
printf("LED turning ON...\n");
vTaskDelay(pdMS_TO_TICKS(5000));
gpio_set_level(LED_PIN, 0);
printf("LED turning OFF...\n");
xSemaphoreGive(accessSemaphore);
}
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void task2(void* pvParameters)
{
while(1)
{
if (gpio_get_level(SWITCH2_PIN))
{
if (xSemaphoreTake(accessSemaphore, portMAX_DELAY)==pdTRUE)
{
for (int i = 0; i < 6; i++)
{
gpio_set_level(LED_PIN, 1);
vTaskDelay(pdMS_TO_TICKS(1000));
gpio_set_level(LED_PIN, 0);
vTaskDelay(pdMS_TO_TICKS(1000));
}
xSemaphoreGive(accessSemaphore);
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
void app_main()
{
accessSemaphore = xSemaphoreCreateMutex();
esp_rom_gpio_pad_select_gpio(LED_PIN);
esp_rom_gpio_pad_select_gpio(SWITCH1_PIN);
esp_rom_gpio_pad_select_gpio(SWITCH2_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(SWITCH1_PIN, GPIO_MODE_INPUT);
gpio_set_direction(SWITCH2_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(SWITCH1_PIN, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(SWITCH2_PIN, GPIO_PULLUP_ONLY);
gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1);
gpio_isr_handler_add(SWITCH1_PIN, switch1_isr_handler, NULL);
xTaskCreate(task1, "Task1", 1024, NULL, 1, NULL);
xTaskCreate(task2, "Task2", 1024, NULL, 2, NULL);
}