#include <stdio.h>
#include "pico/stdlib.h"
#include <string.h>
#define SET 1
#define RESET 0
#define COMP 4
char senha1[COMP+1] = {0};
char senha2[COMP+1] = {0};
const uint8_t Linha[] = {2, 3, 4, 5}; // Linhas do teclado
const uint8_t Coluna[] = {6, 7, 8, 9}; // Colunas do teclado
// Mapeamento das teclas
char teclas[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const uint8_t segment_pins[] = {21, 22, 26, 27, 28, 20, 19};
const uint8_t mux_display_pins[] = {10, 11, 12, 13};
const uint8_t num_display = 4;
const uint8_t LedR = 15; // LED Vermelho
const uint8_t LedG = 14; // LED Verde
// Mapeamento dos dígitos para os segmentos dos displays de 7 segmentos
const int display[16][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 0, 0, 1, 1}, // 9
{1, 1, 1, 0, 1, 1, 1}, // A
{0, 0, 1, 1, 1, 1, 1}, // B
{1, 0, 0, 1, 1, 1, 0}, // C
{0, 1, 1, 1, 1, 0, 1}, // D
{0, 1, 1, 0, 1, 1, 1}, // #
{1, 1, 0, 0, 0, 1, 1} // *
};
// Função de conversão de caractere para número
int charToNum(char ch) {
switch(ch) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case '*': return 14;
case '#': return 15;
default: return -1; // Retorna -1 para caracteres inválidos
}
}
void setup() {
gpio_init(LedR);
gpio_init(LedG);
gpio_set_dir(LedR, GPIO_OUT);
gpio_set_dir(LedG, GPIO_OUT);
gpio_put(LedR, 0);
gpio_put(LedG, 0);
// Inicialização das linhas e colunas do teclado
for (int i = 0; i < 4; i++) {
gpio_init(Linha[i]);
gpio_set_dir(Linha[i], GPIO_OUT);
gpio_put(Linha[i], RESET);
gpio_init(Coluna[i]);
gpio_set_dir(Coluna[i], GPIO_IN);
gpio_pull_down(Coluna[i]);
}
// Inicialização dos pinos dos displays
for (int i = 0; i < 7; i++) {
gpio_init(segment_pins[i]);
gpio_set_dir(segment_pins[i], GPIO_OUT);
}
for (int i = 0; i < num_display; i++) {
gpio_init(mux_display_pins[i]);
gpio_set_dir(mux_display_pins[i], GPIO_OUT);
gpio_put(mux_display_pins[i], 1);
}
}
void show_digit(uint8_t digit, uint8_t value) {
// Atualiza o display de forma eficiente
for (int i = 0; i < 7; i++) {
gpio_put(segment_pins[i], display[value][i]);
}
// Desliga todos os displays
for (int i = 0; i < num_display; i++) {
gpio_put(mux_display_pins[i], 1);
}
// Liga o display atual
gpio_put(mux_display_pins[digit], 0);
}
void clear_digits(void) {
// Limpa todos os displays
for (int i = 0; i < num_display; i++) {
gpio_put(mux_display_pins[i], 1);
}
}
// Função otimizada para ler o teclado, sem delay de polling
char leitura_teclado() {
static bool linha_ativa[4] = {false, false, false, false}; // Para manter o estado de cada linha
for (int row = 0; row < 4; row++) {
if (!linha_ativa[row]) {
gpio_put(Linha[row], SET);
linha_ativa[row] = true;
return 0; // Espera pela interrupção (sem retornar uma tecla agora)
}
for (int col = 0; col < 4; col++) {
if (gpio_get(Coluna[col])) {
linha_ativa[row] = false;
gpio_put(Linha[row], RESET); // Desativa a linha
return teclas[row][col]; // Retorna a tecla pressionada
}
}
gpio_put(Linha[row], RESET);
linha_ativa[row] = false;
}
return 0; // Retorna 0 se nenhuma tecla foi pressionada
}
void get_senha(char *senha, int tam) {
int index = 0;
while (index < tam) {
char key = leitura_teclado();
if (key != 0) {
senha[index] = key;
show_digit(index, charToNum(key)); // Exibe o número no display
index++;
while (leitura_teclado() != 0); // Espera até que a tecla seja solta
}
}
senha[tam] = '\0';
clear_digits();
}
bool compare_senhas(const char *senha1, const char *senha2) {
return strcmp(senha1, senha2) == 0;
}
int main() {
stdio_init_all();
setup();
printf("Digite uma senha de 4 digitos:\n");
get_senha(senha1, COMP);
printf("Senha armazenada!\n");
int cont = 0;
bool acesso = 1;
while (acesso) {
printf("Digite a senha de 4 digitos: \n");
get_senha(senha2, COMP);
if (compare_senhas(senha2, senha1)) {
printf("Senha Correta!\n");
gpio_put(LedG, 1);
acesso = 0;
}
else {
printf("Senha Incorreta!\n");
cont++;
gpio_put(LedR, 1);
sleep_ms(500);
gpio_put(LedR, 0);
}
if (cont > 2) {
printf("Três tentativas incorretas! Bloqueado por 3 segundos!\n");
gpio_put(LedR, 1);
sleep_ms(3000);
gpio_put(LedR, 0);
cont = 0;
}
}
}