#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_timer.h"
// Definição dos Pinos
#define GPIO_LED_BIT0 4
#define GPIO_LED_BIT1 5
#define GPIO_LED_BIT2 6
#define GPIO_LED_BIT3 7
#define GPIO_BTN_A 1
#define GPIO_BTN_B 2
#define LED_PIN_SEL ((1ULL<<GPIO_LED_BIT0) | (1ULL<<GPIO_LED_BIT1) | (1ULL<<GPIO_LED_BIT2) | (1ULL<<GPIO_LED_BIT3))
#define BTN_PIN_SEL ((1ULL<<GPIO_BTN_A) | (1ULL<<GPIO_BTN_B))
void atualizar_leds(uint8_t cont) {
gpio_set_level(GPIO_LED_BIT0, (cont >> 0) & 0x01);
gpio_set_level(GPIO_LED_BIT1, (cont >> 1) & 0x01);
gpio_set_level(GPIO_LED_BIT2, (cont >> 2) & 0x01);
gpio_set_level(GPIO_LED_BIT3, (cont >> 3) & 0x01);
}
void app_main() {
// Configuração dos GPIOs
gpio_config_t io_conf = {
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = LED_PIN_SEL,
.pull_down_en = 0,
.pull_up_en = 0
};
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = BTN_PIN_SEL;
io_conf.pull_up_en = 1;
gpio_config(&io_conf);
// var
uint8_t contador = 0;
uint8_t passo = 1;
int64_t ultimo_tempo_A = 0;
int64_t ultimo_tempo_B = 0;
int estado_ant_A = 1;
int estado_ant_B = 1;
atualizar_leds(contador);
while (1) {
int64_t tempo_atual = esp_timer_get_time();
int leitura_A = gpio_get_level(GPIO_BTN_A);
int leitura_B = gpio_get_level(GPIO_BTN_B);
// Incremento
if (leitura_A == 0 && estado_ant_A == 1) {
if ((tempo_atual - ultimo_tempo_A) >= 50000) {
contador = (contador + passo) % 16;
atualizar_leds(contador);
ultimo_tempo_A = tempo_atual;
}
}
estado_ant_A = leitura_A;
// mudar passo
if (leitura_B == 0 && estado_ant_B == 1) {
if ((tempo_atual - ultimo_tempo_B) >= 50000) {
passo = (passo == 1) ? 2 : 1;
printf("Passo alterado para: %d\n", passo);
ultimo_tempo_B = tempo_atual;
}
}
estado_ant_B = leitura_B;
vTaskDelay(pdMS_TO_TICKS(10));
}
}