#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#define TEMPO_DEBOUNCE 200 // ms
#define INPUT_PIN 15
#define LED_PIN 2
int contador_acionamentos = 0;
unsigned long timestamp_ultimo_acionamento = 0;
int state = 0;
xQueueHandle interputQueue;
static void IRAM_ATTR gpio_interrupt_handler(void *args)
{
if ((esp_timer_get_time() / 1000 - timestamp_ultimo_acionamento) >= TEMPO_DEBOUNCE)
{
contador_acionamentos++;
timestamp_ultimo_acionamento = esp_timer_get_time() / 1000;
int pinNumber = (int)args;
xQueueSendFromISR(interputQueue, &pinNumber, NULL);
}
}
void LED_Control_Task(void *params)
{
int pinNumber, count = 0;
while (true)
{
if (xQueueReceive(interputQueue, &pinNumber, portMAX_DELAY))
{
// printf("GPIO %d was pressed %d times. The state is %d\n", pinNumber, count++, gpio_get_level(INPUT_PIN));
gpio_set_level(LED_PIN, gpio_get_level(INPUT_PIN));
printf("\nContador: %d", contador_acionamentos);
}
}
}
void app_main()
{
gpio_pad_select_gpio(LED_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(INPUT_PIN);
gpio_set_direction(INPUT_PIN, GPIO_MODE_INPUT);
gpio_pulldown_en(INPUT_PIN);
gpio_pullup_dis(INPUT_PIN);
gpio_set_intr_type(INPUT_PIN, GPIO_INTR_POSEDGE);
interputQueue = xQueueCreate(10, sizeof(int));
xTaskCreate(LED_Control_Task, "LED_Control_Task", 2048, NULL, 1, NULL);
gpio_install_isr_service(0);
gpio_isr_handler_add(INPUT_PIN, gpio_interrupt_handler, (void *)INPUT_PIN);
}