-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_auxiliar.c
More file actions
139 lines (122 loc) · 4.46 KB
/
Copy pathmain_auxiliar.c
File metadata and controls
139 lines (122 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @file main_auxiliar.c
* @brief Funções auxiliares do núcleo 0 no projeto multicore com Raspberry Pi Pico W.
*
* Este arquivo complementa a lógica do núcleo 0, com foco na visualização e interpretação
* das mensagens trocadas entre os núcleos via FIFO. Ele oferece suporte ao sistema de
* exibição no display OLED e ao controle visual do estado da rede por meio de um LED RGB.
*/
#include "inc/fila_circular.h"
#include "inc/rgb_pwm_control.h"
#include "configura_geral.h"
//#include "oled_utils.h"
//#include "ssd1306_i2c.h"
#include "inc/mqtt_lwip.h"
#include "lwip/ip_addr.h"
#include "pico/multicore.h"
#include <stdio.h>
#include "estado_mqtt.h"
/**
@brief Aguarda até que a conexão USB esteja pronta para comunicação.
*/
void espera_usb() {
while (!stdio_usb_connected()) {
sleep_ms(200);
}
printf("Conexão USB estabelecida!\n");
}
//função de callback para tornar led verde novamente após um segundo
int64_t turn_off_callback_verde(alarm_id_t id, void *user_data) {
//set_rgb_pwm(0, 65535, 0);
printf("Sinal Verde\n");
return 0;
}
/**
* @brief Trata mensagens recebidas pela FIFO — status Wi-Fi ou retorno de PING.
*
* - Status Wi-Fi (tentativa normal)
* - Retorno de publicação PING (tentativa == 0x9999)
*/
void tratar_mensagem(MensagemWiFi msg) {
struct repeating_timer timer;
const char *descricao = "";
// ======= NOVA LÓGICA: resposta ao PING =======
if (msg.tentativa == 0x9999) {
if (msg.status == 0) {
//chama as funções de gerar numeros aleatórios, coloca o retorno na função em uma variavel inteira para configurar o set_rgb_pwm
//de forma aleatória logo após ocorrer o ping
//int r = numero_aleatorio(0, 65535);
//int g = numero_aleatorio(0, 65535);
//int b = numero_aleatorio(0, 65535);
//ssd1306_draw_utf8_multiline(buffer_oled, 0, 32, "ACK do PING OK");
//set_rgb_pwm(r, g, b); //pisca cor aleatoria no led
add_alarm_in_ms(1000, turn_off_callback_verde, NULL, &timer); //torna o led verde após um segundo da cor aleatória
}
else {
//ssd1306_draw_utf8_multiline(buffer_oled, 0, 32, "ACK do PING FALHOU");
//set_rgb_pwm(65535, 0, 0); // vermelho
printf("ACK do PING FALHOU\n");
}
//render_on_display(buffer_oled, &area);
return;
}
// ======= STATUS Wi-Fi padrão =======
switch (msg.status) {
case 0:
descricao = "INICIALIZANDO";
printf("INICIALIZANDO\n");
//set_rgb_pwm(PWM_STEP, 0, 0); // LED vermelho
break;
case 1:
descricao = "CONECTADO";
printf("CONECTADO\n");
//set_rgb_pwm(0, PWM_STEP, 0); // LED verde
break;
case 2:
descricao = "FALHA";
printf("FALHA\n");
//set_rgb_pwm(0, 0, PWM_STEP); // LED azul
break;
default:
descricao = "DESCONHECIDO";
printf("DESCONHECIDO\n");
//set_rgb_pwm(PWM_STEP, PWM_STEP, PWM_STEP); // LED branco
break;
}
char linha_status[32];
/*snprintf(linha_status, sizeof(linha_status), "Status do Wi-Fi : %s", descricao);
ssd1306_draw_utf8_multiline(buffer_oled, 0, 0, linha_status);
render_on_display(buffer_oled, &area);
sleep_ms(3000);
oled_clear(buffer_oled, &area);
render_on_display(buffer_oled, &area);*/
printf("[NÚCLEO 0] Status: %s (%s)\n", descricao, msg.tentativa > 0 ? descricao : "evento");
}
/**
* @brief Converte IP binário em string, exibe no OLED e salva para uso posterior.
*/
void tratar_ip_binario(uint32_t ip_bin) {
char ip_str[20];
uint8_t ip[4];
ip[0] = (ip_bin >> 24) & 0xFF;
ip[1] = (ip_bin >> 16) & 0xFF;
ip[2] = (ip_bin >> 8) & 0xFF;
ip[3] = ip_bin & 0xFF;
/*
snprintf(ip_str, sizeof(ip_str), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
oled_clear(buffer_oled, &area);
ssd1306_draw_utf8_string(buffer_oled, 0, 0, ip_str);
render_on_display(buffer_oled, &area);*/
printf("[NÚCLEO 0] Endereço IP: %s\n", ip_str);
ultimo_ip_bin = ip_bin;
}
/**
* @brief Exibe status textual do MQTT no OLED e terminal.
*/
void exibir_status_mqtt(const char *texto) {
/*
ssd1306_draw_utf8_string(buffer_oled, 0, 16, "MQTT: ");
ssd1306_draw_utf8_string(buffer_oled, 40, 16, texto);
render_on_display(buffer_oled, &area);*/
printf("[MQTT] %s\n", texto);
}