#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 CHAVE1 16
#define CHAVE2 15
#define CHAVE3 14
#define CHAVE4 13
#define CHAVE5 12
#define CHAVE6 11
#define CHAVE7 10
#define CHAVE8 9
void system_init(void);
void button_callback(uint gpio);
void alarm_irq();
unsigned char texto_um_CHAVE1[17] = "1ON ";
unsigned char texto_dois_CHAVE1[17] = "1OFF ";
unsigned char texto_um_CHAVE2[17] = "2ON ";
unsigned char texto_dois_CHAVE2[17] = "2OFF ";
unsigned char texto_um_CHAVE3[17] = "3ON ";
unsigned char texto_dois_CHAVE3[17] = "3OFF ";
unsigned char texto_um_CHAVE4[17] = "4ON ";
unsigned char texto_dois_CHAVE4[17] = "4OFF ";
unsigned char texto_um_CHAVE5[17] = "5ON ";
unsigned char texto_dois_CHAVE5[17] = "5OFF ";
unsigned char texto_um_CHAVE6[17] = "6ON ";
unsigned char texto_dois_CHAVE6[17] = "6OFF ";
unsigned char texto_um_CHAVE7[17] = "7ON ";
unsigned char texto_dois_CHAVE7[17] = "7OFF ";
unsigned char texto_um_CHAVE8[17] = "8ON ";
unsigned char texto_dois_CHAVE8[17] = "8OFF ";
int main()
{
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 ";
//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++;
}
sleep_ms(5);
}
}
void system_init(void)
{
gpio_init(CHAVE1);
gpio_init(CHAVE2);
gpio_init(CHAVE3);
gpio_init(CHAVE4);
gpio_init(CHAVE5);
gpio_init(CHAVE6);
gpio_init(CHAVE7);
gpio_init(CHAVE8);
gpio_set_dir(CHAVE1, IN);
gpio_set_dir(CHAVE2, IN);
gpio_set_dir(CHAVE3, IN);
gpio_set_dir(CHAVE4, IN);
gpio_set_dir(CHAVE5, IN);
gpio_set_dir(CHAVE6, IN);
gpio_set_dir(CHAVE7, IN);
gpio_set_dir(CHAVE8, IN);
gpio_pull_up(CHAVE1);
gpio_pull_up(CHAVE2);
gpio_pull_up(CHAVE3);
gpio_pull_up(CHAVE4);
gpio_pull_up(CHAVE5);
gpio_pull_up(CHAVE6);
gpio_pull_up(CHAVE7);
gpio_pull_up(CHAVE8);
gpio_set_irq_enabled_with_callback(CHAVE1, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE2, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE3, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE4, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE5, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE6, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE7, GPIO_IRQ_EDGE_FALL|GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
gpio_set_irq_enabled_with_callback(CHAVE8, 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)
{
unsigned int estado1=0;
if(gpio==CHAVE1){
estado1=gpio_get(CHAVE1);
if(estado1==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE1);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE1);
}
}
unsigned int estado2=0;
if(gpio==CHAVE2){
estado2=gpio_get(CHAVE2);
if(estado2==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE2);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE2);
}
}
unsigned int estado3=0;
if(gpio==CHAVE3){
estado3=gpio_get(CHAVE3);
if(estado3==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE3);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE3);
}
}
unsigned int estado4=0;
if(gpio==CHAVE4){
estado4=gpio_get(CHAVE4);
if(estado4==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE4);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE4);
}
}
unsigned int estado5=0;
if(gpio==CHAVE5){
estado5=gpio_get(CHAVE5);
if(estado5==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE5);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE5);
}
}
unsigned int estado6=0;
if(gpio==CHAVE6){
estado6=gpio_get(CHAVE6);
if(estado6==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE6);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE6);
}
}
unsigned int estado7=0;
if(gpio==CHAVE7){
estado7=gpio_get(CHAVE7);
if(estado7==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE7);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE7);
}
}
unsigned int estado8=0;
if(gpio==CHAVE8){
estado8=gpio_get(CHAVE8);
if(estado8==0){
LIMPA_DISPLAY();
posicao_cursor_lcd(1,1);
escreve_frase_ram_lcd(texto_um_CHAVE8);
}else{
LIMPA_DISPLAY();
posicao_cursor_lcd(2,1);
escreve_frase_ram_lcd(texto_dois_CHAVE8);
}
}
}