#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 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 os valores em hexadecimal de 0 a F
uint32_t display[16] = { 0x60035, 0x70004,0x70035,0x20031,0x40004, 0x30025,
0x70025,0x50015,0x50031,0x30035,0x70034,0x30031,0x50025,0x40024, 0x30015,0x30030 };
//cria um vetor com as chaves
uint8_t chave[4] = { chave1, chave2, chave3, chave4 };
//cria um vetor com os leds
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 os 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);
}
//habilita os pinos
REG_WRITE(GPIO_ENABLE_REG, 0b111011110000000000110101);
uint32_t valor_data = REG_READ(GPIO_IN1_REG) & 0xF;
while (true) {
//le o valor do registrador para as chaves
valor_data = REG_READ(GPIO_IN1_REG);
uint8_t chaves = valor_data & 0x0F;
//coloca como saída o valor do vetor dsplay
REG_WRITE(GPIO_OUT_REG, display[valor_data] );
vTaskDelay(200 / portTICK_PERIOD_MS);
// Atualiza LEDs conforme chaves
for (uint8_t i = 0; i < 4; i++) {
// extrai o bit da chave i
uint8_t nivel = (chaves >> i) & 0x01;
// chave fechada (0) → LED aceso
gpio_set_level(leds[i], nivel == 0 ? 1 : 0);
}
}
}