#include "funcao_atividade_.h"
#include "funcoes_neopixel.h"
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/multicore.h"
// Definição do botão do joystick (ajuste o pino conforme seu hardware)
#define JOYSTICK_BTN 15
typedef struct {
uint8_t r, g, b;
} CorRGB;
CorRGB cores[] = {
{255, 0, 0}, {255, 64, 0}, {255, 128, 0}, {255, 192, 0},
{255, 255, 0}, {192, 255, 0}, {128, 255, 0}, {0, 255, 0},
{0, 255, 128}, {0, 255, 255}, {0, 128, 255}, {0, 0, 255},
{128, 0, 255}, {255, 0, 255}, {255, 0, 128}, {255, 255, 255}
};
const size_t TOTAL_CORES = sizeof(cores) / sizeof(CorRGB);
volatile uint index_neo = 0;
// Flags de estado
volatile bool core1_pronto = false;
bool estado_leds[NUM_BOTOES] = {false};
// Callback de interrupção
void gpio_callback(uint gpio, uint32_t events) {
// Tratamento dos botões A e B
for (int i = 0; i < NUM_BOTOES; i++) {
if (gpio == BOTOES[i] && (events & GPIO_IRQ_EDGE_FALL)) {
estado_leds[i] = !estado_leds[i];
gpio_put(LEDS[i], estado_leds[i]);
// Atualiza índice da fila
index_neo = (index_neo + 1) % TOTAL_CORES;
// Atualiza NeoPixel
npSetRGB(index_neo, cores[index_neo].r, cores[index_neo].g, cores[index_neo].b);
npWrite();
}
}
// Tratamento do botão do joystick (RESET)
if (gpio == JOYSTICK_BTN && (events & GPIO_IRQ_EDGE_FALL)) {
// Zera o contador
index_neo = 0;
// Desliga todos os LEDs da matriz NeoPixel
npClear();
npWrite();
// Desliga todos os LEDs comuns
for (int i = 0; i < NUM_BOTOES; i++) {
estado_leds[i] = false;
gpio_put(LEDS[i], 0);
}
printf("Fila reiniciada!\n");
}
}
// Núcleo 1: Atualização dos LEDs
void tratar_eventos_leds() {
core1_pronto = true;
while (true) {
tight_loop_contents(); // Núcleo 1 permanece livre
}
}
int main() {
stdio_init_all();
// Inicializa NeoPixel
npInit(LED_PIN);
npClear();
npWrite();
// Inicializa LEDs
for (int i = 0; i < NUM_BOTOES; i++) {
inicializar_pino(LEDS[i], GPIO_OUT, false, false);
gpio_put(LEDS[i], 0);
estado_leds[i] = false;
}
// Inicializa botões A e B
for (int i = 0; i < NUM_BOTOES; i++) {
inicializar_pino(BOTOES[i], GPIO_IN, true, false);
}
// Inicializa botão do joystick
inicializar_pino(JOYSTICK_BTN, GPIO_IN, true, false);
// Inicia Core 1
multicore_launch_core1(tratar_eventos_leds);
// Aguarda Core 1 ficar pronto
while (!core1_pronto);
// Configura interrupções
gpio_set_irq_callback(gpio_callback);
irq_set_enabled(IO_IRQ_BANK0, true);
for (int i = 0; i < NUM_BOTOES; i++) {
gpio_set_irq_enabled(BOTOES[i], GPIO_IRQ_EDGE_FALL, true);
}
// Habilita interrupção no botão do joystick
gpio_set_irq_enabled(JOYSTICK_BTN, GPIO_IRQ_EDGE_FALL, true);
// Loop principal fica em espera
while (true) {
__wfi(); // Aguarda interrupções
}
}