#include <stdio.h>
#include <stddef.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define gpio_led_pin GPIO_NUM_2
#define gpio_button_pin GPIO_NUM_13
#define ESP_INTR_FLAG_DEFAULT GPIO_INTR_DISABLE
static void button_handler(void *arg);
static void init_hw(void)
{
gpio_config_t io_conf;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = gpio_led_pin;
io_conf.intr_type = ESP_INTR_FLAG_DEFAULT;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = gpio_button_pin;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
gpio_isr_handler_add(gpio_button_pin, button_handler, NULL);
}
static TickType_t next = 0;
static bool led_state = false;
static void IRAM_ATTR button_handler(void *arg)
{
TickType_t now = xTaskGetTickCountFromISR();
if (now > next)
{
led_state = !led_state;
gpio_set_level(gpio_led_pin, led_state);
next = now + 500 / portTICK_PERIOD_MS;
}
}
void app_main(void)
{
init_hw();
vTaskSuspend(NULL);
}