/* Giovanna Fiuza - 5º Semestre engenharia da computação
Exercício 1 - Controle do Led por Interrupção e Timer
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#define LED_ROSA 16
#define BTN 10
static int contador = 0;
volatile bool btn_evento = false;
volatile uint32_t ultimo_press_ms = 0;
struct repeating_timer timer;
/* Definindo antes pra evitar erro */
bool repeating_timer_callback(struct repeating_timer *t);
void gpio_callback(uint gpio, uint32_t events);
void elementos_init() {
gpio_init(LED_ROSA);
gpio_set_dir(LED_ROSA, GPIO_OUT);
gpio_put(LED_ROSA, 0);
gpio_init(BTN);
gpio_set_dir(BTN, GPIO_IN);
gpio_pull_up(BTN);
gpio_set_irq_enabled_with_callback(BTN, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
}
bool repeating_timer_callback(struct repeating_timer *t){
gpio_put(LED_ROSA, !gpio_get(LED_ROSA));
return true;
}
void gpio_callback(uint gpio, uint32_t events){
if (gpio == BTN) {
btn_evento = true;
}
}
int main(){
stdio_init_all();
elementos_init();
while(true){
if (btn_evento){
btn_evento = false;
contador++;
if (contador > 2){
contador = 0;
}
cancel_repeating_timer(&timer);
gpio_put(LED_ROSA, 0);
if (contador == 1) {
add_repeating_timer_ms(200, repeating_timer_callback, NULL, &timer);
}
else if (contador == 2) {
add_repeating_timer_ms(500, repeating_timer_callback, NULL, &timer);
}
else {
gpio_put(LED_ROSA, 0);
}
}
tight_loop_contents();
}
}