#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#define current_time esp_timer_get_time()
#define INPUT_PIN 17
#define LED_PIN 2
int state = 0;
QueueHandle_t handlerQueue;
static int64_t past_time = 0;
static void IRAM_ATTR gpio_interrupt_handler(void *args) {
int pinNumber = (int)args;
if (((current_time-past_time)>=150000)) {
past_time = current_time;
xQueueSendFromISR(handlerQueue, &pinNumber, NULL);
}
}
void LED_Control_Task(void *params) {
int pinNumber, count = 0;
while (1)
{
if (xQueueReceive(handlerQueue, &pinNumber, portMAX_DELAY))
{
printf("GPIO %d was pressed %d times. The state is %d\n", pinNumber, count++, gpio_get_level(INPUT_PIN));
//printf("%ld \n", (long) past_time);
gpio_set_level(LED_PIN, gpio_get_level(INPUT_PIN));
}
}
}
void app_main () {
gpio_reset_pin(LED_PIN);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
gpio_reset_pin(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);
handlerQueue = 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);
}