#include <stdio.h>
#include "pico/stdlib.h"
#include "display.lcd.h"
#include "hardware/gpio.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#define DELAY 100000 //interrupção Timer é a cada 100ms
#define ALARM_NUM 0
#define ALARM_IRQ TIMER_IRQ_0
uint16_t timer_display=0;
#define ON 1
#define OFF 0
#define CHAVE1 16
#define CHAVE2 15
#define CHAVE3 14
#define CHAVE4 13
#define CHAVE5 12
#define CHAVE6 11
#define CHAVE7 10
#define CHAVE8 9
#define LED1 22
#define LED2 21
#define LED3 20
#define LED4 19
#define LED5 18
#define LED6 17
const uint64_t CHAVES[] = { CHAVE1, CHAVE2, CHAVE3, CHAVE4, CHAVE5, CHAVE6, CHAVE7, CHAVE8 };
const uint16_t LEDS[] = { LED1, LED2, LED3, LED4, LED5, LED6 };
void system_init(void);
void button_callback(uint gpio);
void alarm_irq();
int main()
{
uint8_t estado_chaves[8];
unsigned int estado=1;
unsigned char texto_um_D[17] = "DALTON SOUSA ";
unsigned char texto_dois_D[17] = "RA:1600732413026 ";
unsigned char texto_um_F[17] = "FABIO HENRIQUE ";
unsigned char texto_dois_F[17] = "RA:1600732413007 ";
unsigned char texto_um_V[17] = "VICTORIA SERRA ";
unsigned char texto_dois_V[17] = "RA:1600732413009 ";
unsigned char texto_um_Y[17] = "YAN KLEBER ";
unsigned char texto_dois_Y[17] = "RA:1600732413025 ";
uint8_t mostra_estado_chaves[]="Chave=00000000";
//obs: uint8_t = unsigned char,por que uint8_t??
//Porque, o "uint8_t" é universal para todos os micros
stdio_init_all();
system_init();
while (true) {
if((estado==1)&&(timer_display==0))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_D);
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_D);
timer_display = 30;//é cada 100ms,então se multiplicar por 30,eu chego nos meus 3s
estado++;
}else if((estado==2)&&(timer_display==0))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_F);
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_F);
timer_display = 30;
estado++;
}else if ((estado==3)&&(timer_display==0))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_V);
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_V);
timer_display = 30;
estado++;
}
else if ((estado==4)&&(timer_display==0))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_Y);
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_Y);
timer_display = 30;
estado++;
}
if((for(uint8_t idx = 0; idx < 8; idx++)))
{
LIMPA_DISPLAY();
estado_chaves[idx] = gpio_get(CHAVES[idx]);
mostra_estado_chaves[6+idx];
}
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(mostra_estado_chaves);
sleep_ms(500);
}
sleep_ms(5);
}
void system_init(void)
{
for (uint8_t cont = 0; cont < 8; cont++)
{
gpio_init(CHAVES[cont]);
gpio_set_dir(CHAVES[cont], IN);
gpio_pull_up(CHAVES[cont]);
}
for(uint8_t cont1=0;cont1<7;cont1++){
gpio_init(LEDS[cont1]);
gpio_set_dir(LEDS[cont1], OUT);
}
gpio_set_irq_enabled_with_callback(CHAVES[8], GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
uint64_t target = 0;
sleep_ms(10);
hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM) ;
irq_set_exclusive_handler(ALARM_IRQ, alarm_irq) ;
irq_set_enabled(ALARM_IRQ, true) ;
target = timer_hw->timerawl + DELAY;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
init_lcd();
}
void alarm_irq(void)
{
static bool toggle = false;
printf("Timer interrupt occurred!\n");
hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
uint64_t target = timer_hw->timerawl + DELAY;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
if(timer_display) timer_display--;
}
void button_callback(uint gpio)
{
static uint8_t estado1 = OFF;
uint32_t timer_led=0;
if(CHAVES[8] == 0)
{
if(estado1 == OFF)
{
if(timer_led == 0)
{
gpio_put(LEDS[6], 1);
estado1 = ON;
timer_led = 100;
}
}
else
{
if(timer_led == 0)
{
gpio_put(LEDS[6], 0);
estado1 = OFF;
timer_led = 100;
}
}
}
else
{
gpio_put(LEDS[6], 0);
estado1 = OFF;
timer_led = 0;
}
}