Coder Social home page Coder Social logo

aps2-embarcados's Introduction

Hi there 👋

aps2-embarcados's People

Contributors

elijose55 avatar pedrooa avatar

aps2-embarcados's Issues

Cuidado com o tipo Bool. Não existe booleano em C.

volatile Bool locked = false;
volatile Bool flag_back = false;
volatile Bool flag_next = false;
volatile Bool isWashing = false;
volatile Bool isOpen = false;
volatile Bool isDrawn = false;
volatile Bool flag_button = false;
volatile int flag_started = 0;
volatile Bool flag_rtc_alarme = false;
volatile Bool flag_animation_alarm = false;
volatile Bool f_rtt_alarme = false;

Em C, não é definido o tipo booleano (verdadeiro ou falso) em seu padrão.
Se você utilizar o Atmel Studio e clicar em Go To Definition em cima do Bool e nos valores 'true' e 'false'

Irá descobrir que nas bibliotecas tem:

typedef unsigned char           Bool; //!< Boolean.
#define true	1
#define false	0

Portanto, na verdade o Bool utilizado nao passa de um unsigned char (8-bits).
E qual o problema disso?

Bem, imagine que por algum motivo alguém faça o seguinte código.

Bool teste = false;
teste++;
Ou ainda teste = 3;
if(teste == true) { // ISTO FALHA pois true é 1
if(teste) { // Isto execute, pois C verifica se teste != 0

O código acima compila sem warnings e ainda te mascara do erro (true = 1). Por este motivo, este Bool existe por conveniência, mas pode te mascarar de alguns erros.

Não está errado utiliza-lo, mas é desencorajado por muitos.

Pode ser trocado literalmente por

uint8_t teste = 0u;
if(teste) { // teste != 0
if(!teste) { // teste = 0

Neste caso, você sabe que são 8-bits, mas o que importa pra você é se é 0 (falso) ou diferente de zero (verdadeiro).

Structs não deixam de ser variáveis, deveriam vir após ou antes das outras variáveis

/************************************************************************/
/* STRUCTS */
/************************************************************************/
struct botao botaoNext = {.x=384-32,.y=160-32,.size=64,.p_handler = next_callback, .image = &next_colorido};
struct botao botaoBack = {.x=106-32,.y=160-32,.size=64,.p_handler = back_callback, .image = &back_colorido};
struct botao botaoStart = {.x=240-32,.y=280-32,.size=64,.p_handler = start_callback, .image = &fast2};
struct botao bLocked = {.x=380-64,.y=305-64,.size=64,.p_handler = lock_callback, .image = &unlocked_colorido};
struct botao bUnlocked = {.x=475-64,.y=305-64,.size=64,.p_handler = lock_callback, .image = &locked_colorido};
struct botao botaoStop = {.x=240-32,.y=280-32,.size=64,.p_handler = start_callback, .image = &stop};
struct icone diarioAzul = {.x=icon_x,.y=icon_y,.size=128, .image = &diario_azul};
struct icone centrifuga = {.x=icon_x,.y=icon_y,.size=128, .image = &centrifuga_colorido};
struct icone enxague = {.x=icon_x,.y=icon_y,.size=128, .image = &enxague_colorido};
struct icone fast = {.x=icon_x,.y=icon_y,.size=128, .image = &fast_colorido};
struct icone pesado = {.x=icon_x,.y=icon_y,.size=128, .image = &pesado_colorido};
struct icone loading1 = {.x=240-32,.y=icon_y+32,.size=64, .image = &load1};
struct icone loading2 = {.x=240-32,.y=icon_y+32,.size=64, .image = &load2};
struct icone loading3 = {.x=240-32,.y=icon_y+32,.size=64, .image = &load3};
struct icone loading4 = {.x=240-32,.y=icon_y+32,.size=64, .image = &load4};

Executa função antes de inicializar placa (board_init), poderia ocorrer o evento do WatchDog alem de outros problemas

int tempo_total = calcula_tempo(c_diario);

Quando se usa uma atribuição de uma variável com uma função, a função é executada, como se tivesse chamado no código. O problema é que desta forma a função está sendo chamada antes mesmo de executar o board_init do projeto, no board_init controla diversos aspectos essenciais da placa (como Clock e Watchdog) e este deveria ser executado sempre antes de qualquer processamento.

Ideal dividir este trecho longo em diversas funções menores (ex. check_door, calc_total_time,draw_remaning_time...)

/* Check for any pending messages and run message handler if any
* message is found in the queue */
if (mxt_is_message_pending(&device)) {
mxt_handler(&device, botoes, sizeof(botoes) / sizeof(struct botao));
}
if(locked){
if(!isDrawn){
draw_lock_screen();
}
}
if(flag_button){
if(flag_started == 0){ //so abre a porta se a maquina nao estiver funcionando
if(isOpen){
isOpen = false;
pio_clear(LED_PIO, LED_PIO_IDX_MASK);
}
else{
isOpen = true;
pio_set(LED_PIO, LED_PIO_IDX_MASK);
}
flag_button = false;
if(!locked){
isDrawn = false;
}
}
}
if(flag_started == 0){
if(!isDrawn){
ili9488_set_foreground_color(COLOR_CONVERT(COLOR_WHITE));
ili9488_draw_filled_rectangle(0, 0, 500, 500);
tempo_total = calcula_tempo(c_diario);
draw_menu_screen();
}
if(flag_next == true || flag_back == true){
if(flag_next == true){
tipo_lavagem += 1;
}
else if(flag_back == true){
tipo_lavagem -= 1;
}
if (tipo_lavagem%5 == 0 ){
draw_cicle(diarioAzul, c_diario);
tempo_total = calcula_tempo(c_diario);
}
if (tipo_lavagem%5 == 1 ){
draw_cicle(pesado, c_pesado);
tempo_total = calcula_tempo(c_pesado);
}
if (tipo_lavagem%5 == 2 ){
draw_cicle(enxague, c_enxague);
tempo_total = calcula_tempo(c_enxague);
}
if (tipo_lavagem%5 == 3 ){
draw_cicle(centrifuga, c_centrifuga);
tempo_total = calcula_tempo(c_centrifuga);
}
if (tipo_lavagem%5 == 4 ){
draw_cicle(fast, c_rapido);
tempo_total = calcula_tempo(c_rapido);
}
flag_next = false;
flag_back = false;
}
}
if(flag_started == 1 && isOpen == false){
if(!isDrawn){
f_rtt_alarme = true;
flag_animation_alarm = true;
ili9488_set_foreground_color(COLOR_CONVERT(COLOR_WHITE));
ili9488_draw_filled_rectangle(0, 0, 500, 500);
int hora, min, sec;
rtc_get_time(RTC, &hora, &min, &sec);
rtc_set_time_alarm(RTC, 1, hora, 1, min+1,1, sec);
char b[512];
sprintf(b, "Tempo restante: 00 : %02d", tempo_total);
font_draw_text(&calibri_36, b, 12, 12, 1);
draw_started_screen();
}
if (flag_rtc_alarme){
tempo_total -= 1;
if(tempo_total == 0) {
pio_set(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_ms(20);
pio_clear(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
flag_END = true;
}
else{
int hora, min, sec;
rtc_get_time(RTC, &hora, &min, &sec);
rtc_set_time_alarm(RTC, 1, hora, 1, min+1,1, sec);
char b[512];
sprintf(b, "Tempo restante: 00 : %02d", tempo_total);
font_draw_text(&calibri_36, b, 12, 12, 1);
}
flag_rtc_alarme = false;
}
if(f_rtt_alarme & flag_animation_alarm & !locked){
// Gera uma interrupcao a cada 1 segundo
uint16_t pllPreScale = (int) (((float) 32768) / 2.0);
uint32_t irqRTTvalue = 2;
// reinicia RTT para gerar um novo IRQ
RTT_init(pllPreScale, irqRTTvalue);
// Animacao
animation++;
animation = animation%4;
ili9488_set_foreground_color(COLOR_CONVERT(COLOR_WHITE));
ili9488_draw_filled_rectangle(240-32, 56+32, 64, 64);
ili9488_draw_pixmap(load_icons[animation].x, load_icons[animation].y, load_icons[animation].image->width, load_icons[animation].image->height, load_icons[animation].image->data);
/*
* CLEAR FLAG
*/
f_rtt_alarme = false;
}
if(flag_END){
ili9488_set_foreground_color(COLOR_CONVERT(COLOR_WHITE));
ili9488_draw_filled_rectangle(0, 0, 500, 200);
font_draw_text(&calibri_24, "Lavagem finalizada" ,130, ILI9488_LCD_HEIGHT/2, 1);
pio_set(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_s(1.4);
pio_clear(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_ms(500);
pio_set(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_s(1.4);
pio_clear(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_ms(500);
pio_set(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_s(1.4);
pio_clear(BUZZER_PIO, BUZZER_PIO_IDX_MASK);
delay_ms(500);
flag_END = false;
flag_animation_alarm = false;
}
}
else if(flag_started == 1 && isOpen == true){
if(!isDrawn){
draw_door_screen();
}
}
}

Poderia virar um switch para ficar mais legível

if (tipo_lavagem%5 == 0 ){
draw_cicle(diarioAzul, c_diario);
tempo_total = calcula_tempo(c_diario);
}
if (tipo_lavagem%5 == 1 ){
draw_cicle(pesado, c_pesado);
tempo_total = calcula_tempo(c_pesado);
}
if (tipo_lavagem%5 == 2 ){
draw_cicle(enxague, c_enxague);
tempo_total = calcula_tempo(c_enxague);
}
if (tipo_lavagem%5 == 3 ){
draw_cicle(centrifuga, c_centrifuga);
tempo_total = calcula_tempo(c_centrifuga);
}
if (tipo_lavagem%5 == 4 ){
draw_cicle(fast, c_rapido);
tempo_total = calcula_tempo(c_rapido);
}

Exemplo:

uint32_t temp = tipo_lavagem%5;
switch(temp) {
   case 0:
...
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.