143 lines
3.5 KiB
C
143 lines
3.5 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
|
|
#include "nvs_flash.h"
|
|
#include "esp_log.h"
|
|
#include "esp_system.h"
|
|
#include "esp_wifi.h"
|
|
|
|
#include "eeprom_virtual.h"
|
|
#include "eeprom_animacao.h"
|
|
|
|
#include "wifi_config_portal.h"
|
|
#include "mqtt_handler.h"
|
|
|
|
#include "led_driver.h"
|
|
#include "led_effects.h"
|
|
#include "led_task.h"
|
|
#include "creditos.h"
|
|
|
|
#include "driver/i2c.h"
|
|
#include "i2c_helper.h"
|
|
#include "display.h"
|
|
#include "buzzer.h"
|
|
|
|
// ======================================================
|
|
static const char *TAG = "APP";
|
|
void i2c_scan(void);
|
|
volatile bool hora_vem_do_mqtt = false;
|
|
bool modo_bloqueado = false;
|
|
static bool wifi_ready = false;
|
|
|
|
// ======================================================
|
|
// CONTADORES
|
|
// ======================================================
|
|
typedef struct {
|
|
int total_creditos;
|
|
int total_saidas;
|
|
} contadores_t;
|
|
|
|
// ======================================================
|
|
// SEGUNDOS (simples, local)
|
|
// ======================================================
|
|
static uint32_t segundos = 0;
|
|
|
|
static void segundos_task(void *pv)
|
|
{
|
|
while (1) {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
segundos++;
|
|
}
|
|
}
|
|
|
|
uint32_t get_segundos(void)
|
|
{
|
|
return segundos;
|
|
}
|
|
|
|
// ======================================================
|
|
// WIFI OK CALLBACK
|
|
// ======================================================
|
|
static void on_wifi_connected(void)
|
|
{
|
|
wifi_ready = true;
|
|
|
|
ESP_LOGI(TAG, "✅ Wi-Fi conectado — iniciando MQTT...");
|
|
mqtt_handler_start();
|
|
|
|
ESP_LOGI(TAG, "💡 Inicializando driver LED...");
|
|
led_driver_init();
|
|
|
|
ESP_LOGI(TAG, "🎬 Iniciando tasks LED e Créditos...");
|
|
// xTaskCreate(led_task, "led_task", 8192, NULL, 5, NULL);
|
|
// xTaskCreate(creditos_task, "creditos_task", 8192, NULL, 5, NULL);
|
|
}
|
|
|
|
// ======================================================
|
|
// MAIN
|
|
// ======================================================
|
|
void app_main(void)
|
|
{
|
|
// -------- EEPROM virtual --------
|
|
eeprom_virtual_init();
|
|
|
|
contadores_t contadores = {100, 25};
|
|
eeprom_virtual_write_bin("contadores", &contadores, sizeof(contadores));
|
|
|
|
contadores_t lidos = {0};
|
|
size_t len = sizeof(lidos);
|
|
if (eeprom_virtual_read_bin("contadores", &lidos, &len) == ESP_OK) {
|
|
ESP_LOGI("EEPROM", "📖 Lido: creditos=%d saidas=%d",
|
|
lidos.total_creditos, lidos.total_saidas);
|
|
}
|
|
|
|
// -------- NVS --------
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
|
|
// -------- Animação --------
|
|
animacao_load();
|
|
ESP_LOGI("ANIM", "🎨 Animação carregada = %u", animacao);
|
|
|
|
// -------- I2C --------
|
|
if (i2c_init() == ESP_OK) {
|
|
// i2c_scan();
|
|
}
|
|
|
|
esp_err_t ed = display_init();
|
|
ESP_LOGI(TAG, "display_init = %s", esp_err_to_name(ed));
|
|
display_text_top("INIT");
|
|
|
|
// -------- Buzzer --------
|
|
buzzer_init();
|
|
buzzer_beep(300);
|
|
|
|
// -------- Wi-Fi --------
|
|
wifi_config_t cfg;
|
|
bool have_creds = false;
|
|
|
|
if (esp_wifi_get_config(WIFI_IF_STA, &cfg) == ESP_OK) {
|
|
if (strlen((char *)cfg.sta.ssid) > 0) {
|
|
ESP_LOGI(TAG, "📂 Credenciais encontradas: %s", cfg.sta.ssid);
|
|
have_creds = true;
|
|
}
|
|
}
|
|
|
|
wifi_config_portal_init(on_wifi_connected, have_creds);
|
|
|
|
// -------- Tasks base --------
|
|
xTaskCreate(segundos_task, "segundos_task", 4096, NULL, 5, NULL);
|
|
|
|
// -------- Loop principal --------
|
|
// Tudo é event-driven (MQTT, UI, etc.)
|
|
while (1) {
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
}
|
|
}
|
|
|
|
|
|
|