#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/timer.h"
#define LED_R 13 // LED VERMELHO NO PINO 13
#define LED_B 12 // LED AZUL NO PINO 12
#define LED_G 11 // LED VERDE NO PINO 11
#define BUZZER 21 // BUZZER NO PINO 21
uint columns[4] = {4, 3, 2, 28}; // COLUNAS DO TECLADO (GPIOs 4, 3, 2, 28)
uint rows[4] = {8, 7, 6, 5}; // LINHAS DO TECLADO (GPIOs 8, 7, 6, 5)
// CHAR MAP DO TECLADO
char KEY_MAP[16] = {
'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};
uint _columns[4];
uint _rows[4];
char _matrix_values[16];
uint all_columns_mask = 0x0;
uint column_mask[4];
void pico_keypad_init(uint columns[4], uint rows[4], char matrix_values[16]) {
for (int i = 0; i < 16; i++) {
_matrix_values[i] = matrix_values[i];
}
for (int k = 0; k < 4; k++) {
_columns[k] = columns[k];
_rows[k] = rows[k];
gpio_init(_columns[k]);
gpio_init(_rows[k]);
gpio_set_dir(_columns[k], GPIO_IN);
gpio_set_dir(_rows[k], GPIO_OUT);
gpio_put(_rows[k], 1);
all_columns_mask += (1 << _columns[k]);
column_mask[k] = 1 << _columns[k];
}
}
char pico_keypad_get_key(void) {
int row;
uint32_t cols;
cols = gpio_get_all();
cols = cols & all_columns_mask;
if (cols == 0x0) {
return 0;
}
for (int j = 0; j < 4; j++) {
gpio_put(_rows[j], 0);
}
for (row = 0; row < 4; row++) {
gpio_put(_rows[row], 1);
busy_wait_us(10000);
cols = gpio_get_all();
gpio_put(_rows[row], 0);
cols = cols & all_columns_mask;
if (cols != 0x0) {
break;
}
}
for (int w = 0; w < 4; w++) {
gpio_put(_rows[w], 1);
}
if (cols == column_mask[0]) {
return (char)_matrix_values[row * 4 + 0];
} else if (cols == column_mask[1]) {
return (char)_matrix_values[row * 4 + 1];
} else if (cols == column_mask[2]) {
return (char)_matrix_values[row * 4 + 2];
} else if (cols == column_mask[3]) {
return (char)_matrix_values[row * 4 + 3];
} else {
return 0;
}
}
int main() {
pico_keypad_init(columns, rows, KEY_MAP); // Inicializa o teclado matricial
// Configuração dos LEDs e do Buzzer
gpio_init(LED_R);
gpio_set_dir(LED_R, GPIO_OUT);
gpio_put(LED_R, 0);
gpio_init(LED_G);
gpio_set_dir(LED_G, GPIO_OUT);
gpio_put(LED_G, 0);
gpio_init(LED_B);
gpio_set_dir(LED_B, GPIO_OUT);
gpio_put(LED_B, 0);
gpio_init(BUZZER);
gpio_set_dir(BUZZER, GPIO_OUT);
gpio_put(BUZZER, 0);
char key_pressed;
while (true) {
key_pressed = pico_keypad_get_key(); // Obtém a tecla pressionada
if (key_pressed != 0) { // Verifica se uma tecla foi pressionada
printf("Tecla pressionada: %c\n", key_pressed); // Imprime no terminal
// Controle dos LEDs e do Buzzer
switch (key_pressed) {
case 'A':
gpio_put(LED_R, 1); // Liga o LED vermelho
gpio_put(LED_B, 0);
gpio_put(LED_G, 0);
break;
case 'B':
gpio_put(LED_B, 1); // Liga o LED azul
gpio_put(LED_R, 0);
gpio_put(LED_G, 0);
break;
case 'C':
gpio_put(LED_G, 1); // Liga o LED verde
gpio_put(LED_R, 0);
gpio_put(LED_B, 0);
break;
case 'D':
gpio_put(LED_R, 1); // Liga todos os LEDs
gpio_put(LED_B, 1);
gpio_put(LED_G, 1);
break;
case '#':
gpio_put(BUZZER, 1); // Liga o buzzer
sleep_ms(2000); // Delay de 2 segundos
gpio_put(BUZZER, 0); // Desliga o buzzer
break;
default:
gpio_put(LED_R, 0);
gpio_put(LED_B, 0);
gpio_put(LED_G, 0);
gpio_put(BUZZER, 0);
break;
}
}
sleep_ms(50); // Delay para evitar múltiplas leituras
}
return 0;
}