//Aluno: Antonio Portela
#include "pico/stdlib.h"
#include <stdio.h>
#define ROWS 4
#define COLS 4
#define LED_R_PIN 13
#define LED_G_PIN 12
#define LED_B_PIN 11
const uint8_t row_pins[ROWS]={8,7,6,5};
const uint8_t col_pins[COLS]={4,3,2,9};
const char key_map[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
void keypad_init();
char read_keypad();
void rgb_init();
void set_rgb(int red, int green, int blue);
int main(){
stdio_init_all();
keypad_init();
rgb_init();
while(true){
char key = read_keypad();
if (key == '1' || key == '4' || key == '7' || key == '*'){
printf("Tecla pressionada: %c\n", key);
set_rgb(1,0,0);
sleep_ms(200);
}
else if (key == '2' || key == '5' || key == '8' || key == '0'){
printf("Tecla pressionada: %c\n", key);
set_rgb(0,1,0);
sleep_ms(200);
}
else if (key == '3' || key == '6' || key == '9' || key == '#'){
printf("Tecla pressionada: %c\n", key);
set_rgb(0,0,1);
sleep_ms(200);
}
else if (key == 'A' || key == 'B' || key == 'C' || key == 'D'){
printf("Tecla pressionada: %c\n", key);
set_rgb(0,0,0);
sleep_ms(200);
}
else{
sleep_ms(200);
}
}
return 0;
}
void keypad_init(){
for (int i = 0; i<ROWS; i++){
gpio_init(row_pins[i]);
gpio_set_dir(row_pins[i], GPIO_OUT);
gpio_put(row_pins[i],0);
}
for (int i = 0; i<COLS; i++){
gpio_init(col_pins[i]);
gpio_set_dir(col_pins[i], GPIO_IN);
gpio_pull_down(col_pins[i]);
}
}
char read_keypad(){
for (int row = 0; row < ROWS; row++){
gpio_put(row_pins[row], 1);
for (int col = 0; col < COLS; col++){
if (gpio_get(col_pins[col])){
gpio_put(row_pins[row],0);
return key_map[row][col];
}
}
gpio_put(row_pins[row],0);
}
return '\0';
}
void rgb_init(){
gpio_init(LED_R_PIN);
gpio_set_dir(LED_R_PIN, GPIO_OUT);
gpio_init(LED_G_PIN);
gpio_set_dir(LED_G_PIN, GPIO_OUT);
gpio_init(LED_B_PIN);
gpio_set_dir(LED_B_PIN, GPIO_OUT);
}
void set_rgb(int red, int green, int blue){
gpio_put(LED_R_PIN, red);
gpio_put(LED_G_PIN, green);
gpio_put(LED_B_PIN, blue);
}