new module

This commit is contained in:
2025-12-09 11:48:31 +00:00
parent 4820d9111e
commit e6e2622a95
98 changed files with 5349 additions and 8607 deletions

View File

@@ -1,3 +1,4 @@
// === Início de: main/main.c ===
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>
@@ -30,8 +31,9 @@
#include "buzzer.h"
#include "evse_link.h"
#include "ocpp.h"
#include "led.h"
#include "scheduler.h"
#define EVSE_MANAGER_TICK_PERIOD_MS 1000
#define AP_CONNECTION_TIMEOUT 120000
#define RESET_HOLD_TIME 30000
#define DEBOUNCE_TIME_MS 50
@@ -41,9 +43,9 @@
static const char *TAG = "app_main";
static TaskHandle_t user_input_task;
static TaskHandle_t user_input_task = NULL;
static TickType_t press_tick = 0;
static TickType_t last_interrupt_tick = 0;
static volatile TickType_t last_interrupt_tick = 0;
static bool pressed = false;
//
@@ -79,6 +81,7 @@ static void fs_init(void)
fs_info(&cfg_conf);
fs_info(&data_conf);
}
//
// Wi-Fi event monitoring task
//
@@ -87,17 +90,15 @@ static void wifi_event_task_func(void *param)
EventBits_t mode_bits;
for (;;)
{
// Wait indefinitely until either AP or STA mode is entered
mode_bits = xEventGroupWaitBits(
wifi_event_group,
WIFI_AP_MODE_BIT | WIFI_STA_MODE_BIT,
pdFALSE, // do not clear bits on exit
pdFALSE, // wait for any bit
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (mode_bits & WIFI_AP_MODE_BIT)
{
// We're in AP mode: wait for a client to connect within the timeout
if (xEventGroupWaitBits(
wifi_event_group,
WIFI_AP_CONNECTED_BIT,
@@ -106,7 +107,6 @@ static void wifi_event_task_func(void *param)
pdMS_TO_TICKS(AP_CONNECTION_TIMEOUT)) &
WIFI_AP_CONNECTED_BIT)
{
// Once connected, block until the client disconnects
xEventGroupWaitBits(
wifi_event_group,
WIFI_AP_DISCONNECTED_BIT,
@@ -116,7 +116,6 @@ static void wifi_event_task_func(void *param)
}
else
{
// Timeout expired with no client—optionally stop the AP
if (xEventGroupGetBits(wifi_event_group) & WIFI_AP_MODE_BIT)
{
// wifi_ap_stop();
@@ -125,7 +124,6 @@ static void wifi_event_task_func(void *param)
}
else if (mode_bits & WIFI_STA_MODE_BIT)
{
// We're in STA mode: block until disconnected from the AP
xEventGroupWaitBits(
wifi_event_group,
WIFI_STA_DISCONNECTED_BIT,
@@ -133,9 +131,6 @@ static void wifi_event_task_func(void *param)
pdFALSE,
portMAX_DELAY);
}
// Prevent this task from hogging the CPU when idle
// vTaskDelay(pdMS_TO_TICKS(10));
}
}
@@ -144,7 +139,6 @@ static void wifi_event_task_func(void *param)
//
static void handle_button_press(void)
{
// If not already in AP mode, start it
if (!(xEventGroupGetBits(wifi_event_group) & WIFI_AP_MODE_BIT))
{
ESP_LOGI(TAG, "Starting Wi-Fi AP mode");
@@ -158,26 +152,26 @@ static void user_input_task_func(void *param)
uint32_t notification;
for (;;)
{
// Wait for notification bits from ISR
if (xTaskNotifyWait(
0, // do not clear any bits on entry
UINT32_MAX, // clear all bits on exit
0,
UINT32_MAX,
&notification,
portMAX_DELAY))
{
// Handle button press event
if (notification & PRESS_BIT)
{
press_tick = xTaskGetTickCount();
pressed = true;
ESP_LOGI(TAG, "Button Pressed");
handle_button_press(); // só aqui
handle_button_press();
}
if ((notification & RELEASED_BIT) && pressed)
{
pressed = false;
TickType_t held = xTaskGetTickCount() - press_tick;
ESP_LOGI(TAG, "Button Released (held %u ms)", (unsigned)pdTICKS_TO_MS(held));
if (held >= pdMS_TO_TICKS(RESET_HOLD_TIME))
{
ESP_LOGW(TAG, "Long press: erasing NVS + reboot");
@@ -195,18 +189,20 @@ static void IRAM_ATTR button_isr_handler(void *arg)
BaseType_t higher_task_woken = pdFALSE;
TickType_t now = xTaskGetTickCountFromISR();
// Debounce: ignore interrupts occurring too close together
if (now - last_interrupt_tick < pdMS_TO_TICKS(DEBOUNCE_TIME_MS))
{
return;
}
last_interrupt_tick = now;
// Read GPIO level: 0 = button pressed, 1 = button released
if (user_input_task == NULL)
{
return;
}
int level = gpio_get_level(board_config.button_wifi_gpio);
if (level == 0)
{
// Notify task: button pressed
xTaskNotifyFromISR(
user_input_task,
PRESS_BIT,
@@ -215,7 +211,6 @@ static void IRAM_ATTR button_isr_handler(void *arg)
}
else
{
// Notify task: button released
xTaskNotifyFromISR(
user_input_task,
RELEASED_BIT,
@@ -223,7 +218,6 @@ static void IRAM_ATTR button_isr_handler(void *arg)
&higher_task_woken);
}
// Yield to higher priority task if unblocked
if (higher_task_woken)
{
portYIELD_FROM_ISR();
@@ -238,40 +232,31 @@ static void button_init(void)
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_ENABLE,
.intr_type = GPIO_INTR_ANYEDGE};
ESP_ERROR_CHECK(gpio_config(&conf));
ESP_ERROR_CHECK(gpio_isr_handler_add(board_config.button_wifi_gpio, button_isr_handler, NULL));
}
//
// Inicialização dos módulos do sistema
// Inicialização dos módulos do sistema (SEM botão)
//
static void init_modules(void)
{
peripherals_init();
led_init();
wifi_ini();
buzzer_init();
ESP_ERROR_CHECK(rest_server_init("/data"));
protocols_init();
evse_manager_init();
evse_init(); // Cria a task para FSM
button_init();
evse_init();
auth_init();
loadbalancer_init();
meter_manager_init();
meter_manager_start();
evse_link_init();
ocpp_start();
// wifi_ap_start();
// Outros módulos (descomente conforme necessário)
// meter_init();
// ocpp_start();
// orno_modbus_start();
// currentshaper_start();
// initWiegand();
// meter_zigbee_start();
// master_sync_start();
// slave_sync_start();
scheduler_init();
}
//
@@ -300,8 +285,18 @@ void app_main(void)
ESP_ERROR_CHECK(gpio_install_isr_service(0));
board_config_load();
// 1) cria a task que recebe notificações do botão
xTaskCreate(user_input_task_func, "user_input_task", 4 * 1024, NULL, 3, &user_input_task);
// 2) agora é seguro registrar ISR do botão
button_init();
// 3) inicia o resto do sistema
init_modules();
// 4) tasks auxiliares
xTaskCreate(wifi_event_task_func, "wifi_event_task", 8 * 1024, NULL, 3, NULL);
xTaskCreate(user_input_task_func, "user_input_task", 4 * 1024, NULL, 3, &user_input_task);
}
// === Fim de: main/main.c ===