#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
// Bibliotecas para NeoPixel e teclado
#include "ws2818b.pio.h"
// Definição do número de LEDs e pino
#define LED_COUNT 25
#define LED_PIN 7
// Teclado matricial
#define ROW_COUNT 4
#define COL_COUNT 4
// Pinos do teclado
const uint rows[ROW_COUNT] = {2, 3, 4, 5}; // Ajuste os pinos conforme necessário
const uint cols[COL_COUNT] = {6, 8, 10, 11};
// Mapas do teclado matricial
char keymap[ROW_COUNT][COL_COUNT] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Definição de pixel GRB
struct pixel_t {
uint8_t G, R, B;
};
typedef struct pixel_t pixel_t;
typedef pixel_t npLED_t;
// Buffer de LEDs
npLED_t leds[LED_COUNT];
// Variáveis da máquina PIO
PIO np_pio;
uint sm;
/**
* Inicializa a máquina PIO para controle da matriz de LEDs.
*/
void npInit(uint pin) {
uint offset = pio_add_program(pio0, &ws2818b_program);
np_pio = pio0;
sm = pio_claim_unused_sm(np_pio, true);
ws2818b_program_init(np_pio, sm, offset, pin, 800000.f);
// Limpa buffer de LEDs
for (uint i = 0; i < LED_COUNT; ++i) {
leds[i].R = 0;
leds[i].G = 0;
leds[i].B = 0;
}
}
/**
* Atribui uma cor RGB a um LED.
*/
void npSetLED(const uint index, const uint8_t r, const uint8_t g, const uint8_t b) {
leds[index].R = r;
leds[index].G = g;
leds[index].B = b;
}
/**
* Limpa o buffer de LEDs.
*/
void npClear() {
for (uint i = 0; i < LED_COUNT; ++i) {
npSetLED(i, 0, 0, 0);
}
}
/**
* Escreve os dados do buffer nos LEDs.
*/
void npWrite() {
for (uint i = 0; i < LED_COUNT; ++i) {
pio_sm_put_blocking(np_pio, sm, leds[i].G);
pio_sm_put_blocking(np_pio, sm, leds[i].R);
pio_sm_put_blocking(np_pio, sm, leds[i].B);
}
sleep_us(100); // Reset dos LEDs
}
/**
* Inicializa o teclado matricial.
*/
void init_keypad() {
for (int i = 0; i < ROW_COUNT; i++) {
gpio_init(rows[i]);
gpio_set_dir(rows[i], GPIO_OUT);
gpio_put(rows[i], 1);
gpio_init(cols[i]);
gpio_set_dir(cols[i], GPIO_IN);
gpio_pull_down(cols[i]);
}
}
/**
* Lê uma tecla do teclado matricial.
*/
char read_keypad() {
for (int row = 0; row < ROW_COUNT; row++) {
gpio_put(rows[row], 0);
for (int col = 0; col < COL_COUNT; col++) {
if (gpio_get(cols[col]) == 1) {
while (gpio_get(cols[col]) == 1); // Aguarda soltar a tecla
gpio_put(rows[row], 1);
return keymap[row][col];
}
}
gpio_put(rows[row], 1);
}
return 0; // Nenhuma tecla pressionada
}
/**
* Desenha padrões diferentes na matriz de LEDs com base na tecla pressionada.
*/
void draw_pattern(char key) {
npClear();
switch (key) {
case '1': // Desenha um quadrado
for (int i = 0; i < LED_COUNT; i++) {
npSetLED(i, 255, 0, 0); // Vermelho
}
break;
case '2': // Desenha uma cruz
for (int i = 0; i < 5; i++) {
npSetLED(i * 5 + 2, 0, 255, 0); // Verde na coluna central
npSetLED(10 + i, 0, 255, 0); // Verde na linha central
}
break;
case '3': // Desenha um "X"
for (int i = 0; i < 5; i++) {
npSetLED(i * 5 + i, 0, 0, 255); // Azul diagonal principal
npSetLED(i * 5 + (4 - i), 0, 0, 255); // Azul diagonal inversa
}
break;
case '4': // Pisca todos os LEDs
for (int i = 0; i < LED_COUNT; i++) {
npSetLED(i, 255, 255, 0); // Amarelo
}
sleep_ms(500);
npClear();
break;
default:
break;
}
npWrite();
}
int main() {
stdio_init_all();
// Inicializa o teclado e a matriz de LEDs
init_keypad();
npInit(LED_PIN);
printf("Sistema iniciado!\n");
while (true) {
char key = read_keypad();
if (key) {
printf("Tecla pressionada: %c\n", key);
draw_pattern(key);
}
sleep_ms(100); // Reduz o uso da CPU
}
return 0;
}