// HUGO FELIPE DOS SANTOS ROCHA
// https://wokwi.com/projects/420431412377100289
/* Faça um projeto no wokwi utilizando um teclado matricial de forma que:
Pressionando as teclas 1, 4, 7 e "*" - Acenda o led vermelho;
Pressionando as teclas 2, 5, 8 e 0 - Acenda o led verde;
Pressionando as teclas 3, 6, 9 e "#" - Acenda o led azul;
Pressionando as teclas A, B, C e D - Nenhum led deve acender;
*/
#include <stdio.h>
#include "pico/stdlib.h"
//Definição dos pinos GPIO do teclado
#define L1 2
#define L2 3
#define L3 4
#define L4 5
#define C1 6
#define C2 7
#define C3 8
#define C4 9
//Definição dos pinos do LED RGB
#define LED_R 13 // LED vermelho
#define LED_G 11 // LED verde
#define LED_B 12 // LED azul
//Verifica a quantidade de pinos, os inicializa e atribui o tipo
void setPinsType(uint pins[], int length, bool type){
for(int i = 0; i < length; i++){
gpio_init(pins[i]);
gpio_set_dir(pins[i], type);
//Liga o pino se for de saída
if(type){
gpio_put(pins[i], true);
}
}
}
int main(){
stdio_init_all();
//Agrupa linhas do teclado, define o tipo e as liga
uint rows[] = {L1, L2, L3, L4};
setPinsType(rows, 4, GPIO_OUT);
//Agrupa colunas do teclado e define o tipo
uint columns[] = {C1, C2, C3, C4};
setPinsType(columns, 4, GPIO_IN);
//Agrupa os LEDS e define o tipo
uint leds[] = {LED_R, LED_G, LED_B};
setPinsType(leds, 3, GPIO_OUT);
//Armazena o estado de cada coluna
bool status[4];
while(true){
//Captura os estados das colunas
for(int i = 0; i < 4; i++){
status[i] = gpio_get(columns[i]);
}
//Liga cada led de acordo com o valor das respectivas colunas, verificando se a coluna 4 está ativa
gpio_put(LED_R, status[3] ? false : status[0]);
gpio_put(LED_G, status[3] ? false : status[1]);
gpio_put(LED_B, status[3] ? false : status[2]);
sleep_ms(100);
}
}