/******************************************************************************
* Nome do Arquivo : main.c
*
* Descrição : Arquivo principal aula 4, exemplo 3
*
* Ambiente : WOKWI e RP2040 C/C++ SDK
*
* Responsável : Weslley M. Torres
*
* Versão/Data : 1.0.0 - 10/03/2024 - Initial version
*
*****************************************************************************/
/******************************************************************************
* HEADER-FILES (Somente os arquivos necessários nesse arquivo)
******************************************************************************/
#include <stdio.h>
#include "pico/stdlib.h"
#include "display_lcd.h"
#include "hardware/timer.h"
#include "hardware/gpio.h"
/*****************************************************************************/
/******************************************************************************
* Variaveis Globais
******************************************************************************/
#define OUT_H 1
#define OUT_L 0
#define OUT 1
#define IN 0
#define DELAY 1000
#define ALARM_NUM 0
#define ALARM_IRQ timer_hardware_alarm_get_irq_num(timer_hw, ALARM_NUM)
#define chave_1 16
#define chave_2 15
#define chave_3 14
#define chave_4 13
#define chave_5 12
#define chave_6 11
#define chave_7 10
#define chave_8 9
uint8_t chaves[8]={chave_1,chave_2,chave_3,chave_4,chave_5,chave_6,chave_7,chave_8};
uint8_t estados_chaves[8]={1,1,1,1,1,1,1,1};
uint8_t trava_muda_nomes=1;
uint8_t trava_muda_estados_chaves_1=1;
uint8_t trava_muda_estados_chaves_2=1;
uint16_t contagem=3000;
uint8_t sequencia=0;
uint8_t texto1_chave[17]="CH1:OFF CH2:OFF ";
uint8_t texto2_chave[17]="CH3:OFF CH4:OFF ";
uint8_t texto3_chave[17]="CH5:OFF CH6:OFF ";
uint8_t texto4_chave[17]="CH7:OFF CH8:OFF ";
/*****************************************************************************/
/******************************************************************************
* Prototipos das funções
******************************************************************************/
void alarm_irq(void);
void button_callback(uint gpio);
void system_init(void);
void chaves_init(void);
void mostra_estados(void);
uint8_t mostra_nomes(void);
/*****************************************************************************/
/******************************************************************************
* Funcao: int main(void)
* Entrada: Nenhuma (void)
* Saída: Nenhuma (void)
* Descrição: Função principal
*****************************************************************************/
void main(void)
{
stdio_init_all();
system_init();
sequencia = mostra_nomes();
chaves_init();
while(true)
{
mostra_estados();
sleep_ms(1); //Sleep apenas para não travar a simulação.
}
}
/******************************************************************************
* Funcao: void system_init(void)
* Entrada: Nenhuma (void)
* Saída: Nenhuma (void)
* Descrição: Inicializa sistema (microcontrolador e periféricos)
*****************************************************************************/
void system_init(void)
{
init_lcd();
hw_set_bits(&timer_hw->inte, 1u << ALARM_NUM);
irq_set_exclusive_handler(ALARM_IRQ, alarm_irq);
uint64_t target = timer_hw->timerawl + DELAY;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
irq_set_enabled(ALARM_IRQ, true);
}
void chaves_init(void)
{
for(uint8_t posicao=0;posicao<8;posicao++)
{
gpio_init(chaves[posicao]);
gpio_set_dir(chaves[posicao],IN);
gpio_pull_up(chaves[posicao]);
gpio_set_irq_enabled_with_callback(chaves[posicao],GPIO_IRQ_EDGE_FALL | GPIO_IRQ_EDGE_RISE, true, (gpio_irq_callback_t)&button_callback);
}
if(!(gpio_get(chave_1)))
{
texto1_chave[5]='N';
texto1_chave[6]=' ';
estados_chaves[0]=0;
}
else
{
texto1_chave[5]='F';
texto1_chave[6]='F';
estados_chaves[0]=1;
}
if(!(gpio_get(chave_2)))
{
texto1_chave[13]='N';
texto1_chave[14]=' ';
estados_chaves[1]=0;
}
else
{
texto1_chave[13]='F';
texto1_chave[14]='F';
estados_chaves[1]=1;
}
if(!(gpio_get(chave_3)))
{
texto2_chave[5]='N';
texto2_chave[6]=' ';
estados_chaves[2]=0;
}
else
{
texto2_chave[5]='F';
texto2_chave[6]='F';
estados_chaves[2]=1;
}
if(!(gpio_get(chave_4)))
{
texto2_chave[13]='N';
texto2_chave[14]=' ';
estados_chaves[3]=0;
}
else
{
texto2_chave[13]='F';
texto2_chave[14]='F';
estados_chaves[3]=1;
}
if(!(gpio_get(chave_5)))
{
texto3_chave[5]='N';
texto3_chave[6]=' ';
estados_chaves[4]=0;
}
else
{
texto3_chave[5]='F';
texto3_chave[6]='F';
estados_chaves[4]=1;
}
if(!(gpio_get(chave_6)))
{
texto3_chave[13]='N';
texto3_chave[14]=' ';
estados_chaves[5]=0;
}
else
{
texto3_chave[13]='F';
texto3_chave[14]='F';
estados_chaves[5]=1;
}
if(!(gpio_get(chave_7)))
{
texto4_chave[5]='N';
texto4_chave[6]=' ';
estados_chaves[6]=0;
}
else
{
texto4_chave[5]='F';
texto4_chave[6]='F';
estados_chaves[6]=1;
}
if(!(gpio_get(chave_8)))
{
texto4_chave[13]='N';
texto4_chave[14]=' ';
estados_chaves[7]=0;
}
else
{
texto4_chave[13]='F';
texto4_chave[14]='F';
estados_chaves[7]=1;
}
}
void mostra_estados(void)
{
if((trava_muda_estados_chaves_1)||(trava_muda_estados_chaves_2))
{
if((sequencia==1)&&(trava_muda_estados_chaves_1))
{
LIMPA_DISPLAY();
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(texto1_chave);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(texto2_chave);
trava_muda_estados_chaves_1=0;
}
else if((sequencia==2)&&(trava_muda_estados_chaves_2))
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(texto3_chave);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(texto4_chave);
trava_muda_estados_chaves_2=0;
}
DesligaCursor();
}
}
void alarm_irq(void)
{
hw_clear_bits(&timer_hw->intr, 1u << ALARM_NUM);
uint64_t target = timer_hw->timerawl + DELAY;
timer_hw->alarm[ALARM_NUM] = (uint32_t) target;
contagem--;
if(!sequencia)
{
if(!contagem)
{
trava_muda_nomes=1;
contagem=3000;
}
}
else
{
if(!contagem)
{
if(sequencia==1)
{
sequencia=2;
trava_muda_estados_chaves_1=0;
trava_muda_estados_chaves_2=1;
}
else if(sequencia==2)
{
sequencia=1;
trava_muda_estados_chaves_1=1;
trava_muda_estados_chaves_2=0;
}
else
{
sequencia=1;
trava_muda_estados_chaves_2=0;
trava_muda_estados_chaves_1=1;
}
contagem=2000;
}
}
}
void button_callback(uint gpio)
{
if(gpio==chave_1)
{
if(!(gpio_get(chave_1)))
{
texto1_chave[5]='N';
texto1_chave[6]=' ';
estados_chaves[0]=0;
}
else
{
texto1_chave[5]='F';
texto1_chave[6]='F';
estados_chaves[0]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_2)
{
if(!(gpio_get(chave_2)))
{
texto1_chave[13]='N';
texto1_chave[14]=' ';
estados_chaves[1]=0;
}
else
{
texto1_chave[13]='F';
texto1_chave[14]='F';
estados_chaves[1]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_3)
{
if(!(gpio_get(chave_3)))
{
texto2_chave[5]='N';
texto2_chave[6]=' ';
estados_chaves[2]=0;
}
else
{
texto2_chave[5]='F';
texto2_chave[6]='F';
estados_chaves[2]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_4)
{
if(!(gpio_get(chave_4)))
{
texto2_chave[13]='N';
texto2_chave[14]=' ';
estados_chaves[3]=0;
}
else
{
texto2_chave[13]='F';
texto2_chave[14]='F';
estados_chaves[3]=1;
}
trava_muda_estados_chaves_1=1;
}
else if(gpio==chave_5)
{
if(!(gpio_get(chave_5)))
{
texto3_chave[5]='N';
texto3_chave[6]=' ';
estados_chaves[4]=0;
}
else
{
texto3_chave[5]='F';
texto3_chave[6]='F';
estados_chaves[4]=1;
}
trava_muda_estados_chaves_2=1;
}
else if(gpio==chave_6)
{
if(!(gpio_get(chave_6)))
{
texto3_chave[13]='N';
texto3_chave[14]=' ';
estados_chaves[5]=0;
}
else
{
texto3_chave[13]='F';
texto3_chave[14]='F';
estados_chaves[5]=1;
}
trava_muda_estados_chaves_2=1;
}
else if(gpio==chave_7)
{
if(!(gpio_get(chave_7)))
{
texto4_chave[5]='N';
texto4_chave[6]=' ';
estados_chaves[6]=0;
}
else
{
texto4_chave[5]='F';
texto4_chave[6]='F';
estados_chaves[6]=1;
}
trava_muda_estados_chaves_2=1;
}
else if(gpio==chave_8)
{
if(!(gpio_get(chave_8)))
{
texto4_chave[13]='N';
texto4_chave[14]=' ';
estados_chaves[7]=0;
}
else
{
texto4_chave[13]='F';
texto4_chave[14]='F';
estados_chaves[7]=1;
}
trava_muda_estados_chaves_2=1;
}
}
uint8_t mostra_nomes(void)
{
uint8_t nome_1[17] = "Carlos E. S. ";
uint8_t nome_2[17] = "Carlos H. C. O. ";
uint8_t nome_3[17] = "Guilherme Akaki";
uint8_t nome_4[17] = "Julio Cesar ";
uint8_t nome_5[17] = "Nathan S. Soares";
uint8_t ra_1[17] = "1600732313022";
uint8_t ra_2[17] = "1600732213005";
uint8_t ra_3[17] = "1600732321042";
uint8_t ra_4[17] = "1600732311020";
uint8_t ra_5[17] = "1600732411020";
uint8_t nome_ra=1;
while(nome_ra<7)
{
if(trava_muda_nomes)
{
LIMPA_DISPLAY();
if (nome_ra==1)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_1);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_1);
}
else if (nome_ra==2)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_2);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_2);
}
else if (nome_ra==3)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_3);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_3);
}
else if (nome_ra==4)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_4);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_4);
}
else if (nome_ra==5)
{
posicao_cursor_lcd(1,0);
escreve_frase_ram_lcd(nome_5);
posicao_cursor_lcd(2,0);
escreve_frase_ram_lcd(ra_5);
}
trava_muda_nomes=0;
nome_ra++;
DesligaCursor();
}
sleep_ms(50);
}
LIMPA_DISPLAY();
return 1;
}