#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_log.h"
#define delay(value) vTaskDelay(value / portTICK_PERIOD_MS)
#define GPIO_KEY GPIO_NUM_9
#define GPIO_LED GPIO_NUM_1
#define DEBOUNCE 100
static const char *TAG = "GPIO_INPUT";
volatile uint32_t contador = 0;
void app_main() {
//zero-initialize the config structure.
gpio_config_t out_conf = {};
//disable interrupt
out_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
out_conf.mode = GPIO_MODE_OUTPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
// io_conf.pin_bit_mask = 0B00000000000000000000000000000010;
out_conf.pin_bit_mask = (1 << GPIO_LED);
//disable pull-down mode
out_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
//disable pull-up mode
out_conf.pull_up_en = GPIO_PULLDOWN_DISABLE;
//configure GPIO with the given settings
gpio_config(&out_conf);
//zero-initialize the config structure.
gpio_config_t in_conf = {};
//disable interrupt
in_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
in_conf.mode = GPIO_MODE_INPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
// io_conf.pin_bit_mask = 0B00000000000000000000000000000010;
in_conf.pin_bit_mask = (1 << GPIO_KEY);
//disable pull-down mode
in_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
//disable pull-up mode
in_conf.pull_up_en = GPIO_PULLUP_ENABLE;
//configure GPIO with the given settings
gpio_config(&in_conf);
int statusLastKey = gpio_get_level(GPIO_KEY);
for (;;) {
if (statusLastKey != gpio_get_level(GPIO_KEY)) {
statusLastKey = gpio_get_level(GPIO_KEY);
for (int test = 0; test <= DEBOUNCE; test++) {
if (statusLastKey != gpio_get_level(GPIO_KEY)) {
test = 0;
}
}
if (statusLastKey == 1) contador++;
}
ESP_LOGE("MAIN", "Contagem: %d", contador);
gpio_set_level(GPIO_LED, contador % 2);
// delay(500);
}
}