#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "rom/gpio.h"
// define os nomes dos endereços dos registradores
#define GPIO_ENABLE_REG 0x3FF44020
#define GPIO_OUT_REG 0x3FF44004
#define GPIO_IN1_REG 0x3FF44040
#define GPIO_ENABLE_W1TC_REG 0x3FF44028
//define os pinos das chaves
#define chave1 34
#define chave2 35
#define chave3 32
#define chave4 33
#define led0 19
#define led1 21
#define led2 22
#define led3 23
//cria um vetor com as chaves
uint8_t chave[4] = { chave1, chave2, chave3, chave4 };
uint8_t leds[4] = { led0, led1, led2, led3 };
void app_main() {
//configura as chaves como entrada
for (uint8_t index = 0; index < 4; index++ ){
gpio_pad_select_gpio( chave[ index ] );
gpio_set_direction ( chave[ index ], GPIO_MODE_INPUT );
gpio_set_pull_mode ( chave[ index ], GPIO_PULLUP_ONLY);
}
// --- Configura LEDs como saída ---
for (uint8_t i = 0; i < 4; i++) {
gpio_pad_select_gpio(leds[i]);
gpio_set_direction(leds[i], GPIO_MODE_OUTPUT);
}
// --- Configura os 4 GPIOs como entrada ---
// (limpa os bits 0,1,2,3 do registrador ENABLE)
REG_WRITE(GPIO_ENABLE_W1TC_REG,
(1 << chave1) |
(1 << chave2) |
(1 << chave3) |
(1 << chave4));
while (1) {
// lê os níveis lógicos dos GPIOs (0 = apertado se tiver pull-up)
uint32_t estado = REG_READ(GPIO_IN1_REG);
// extrai os 4 primeiros bits (botões 0-3)
uint8_t botoes = estado & 0x0F;
printf("Estado dos botões (bits 3..0) = 0x%X\n", botoes);
// Atualiza LEDs conforme chaves
for (uint8_t i = 0; i < 4; i++) {
// extrai o bit da chave i
uint8_t nivel = (chave[i] >> i) & 0x01;
// chave fechada (0) → LED aceso
gpio_set_level(leds[i], nivel == 0 ? 1 : 0);
}
vTaskDelay(pdMS_TO_TICKS(500));
}
}