new buzzer component
This commit is contained in:
108
main/main.c
108
main/main.c
@@ -15,7 +15,7 @@
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#include "wifi.h"
|
||||
#include "network.h"
|
||||
#include "board_config.h"
|
||||
#include "logger.h"
|
||||
#include "rest_main.h"
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "auth.h"
|
||||
#include "loadbalancer.h"
|
||||
#include "meter_manager.h"
|
||||
#include "buzzer.h"
|
||||
|
||||
|
||||
#define EVSE_MANAGER_TICK_PERIOD_MS 1000
|
||||
@@ -78,77 +79,134 @@ static void fs_init(void) {
|
||||
fs_info(&cfg_conf);
|
||||
fs_info(&data_conf);
|
||||
}
|
||||
|
||||
//
|
||||
// Wi-Fi event monitoring task
|
||||
//
|
||||
static void wifi_event_task_func(void *param) {
|
||||
EventBits_t mode_bits;
|
||||
while (1) {
|
||||
mode_bits = xEventGroupWaitBits(wifi_event_group, WIFI_AP_MODE_BIT | WIFI_STA_MODE_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
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
|
||||
portMAX_DELAY
|
||||
);
|
||||
|
||||
if (mode_bits & WIFI_AP_MODE_BIT) {
|
||||
if (xEventGroupWaitBits(wifi_event_group, WIFI_AP_CONNECTED_BIT, pdFALSE, pdFALSE, pdMS_TO_TICKS(AP_CONNECTION_TIMEOUT)) & WIFI_AP_CONNECTED_BIT) {
|
||||
xEventGroupWaitBits(wifi_event_group, WIFI_AP_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
// We're in AP mode: wait for a client to connect within the timeout
|
||||
if (xEventGroupWaitBits(
|
||||
wifi_event_group,
|
||||
WIFI_AP_CONNECTED_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
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,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY
|
||||
);
|
||||
} else {
|
||||
// Timeout expired with no client—optionally stop the AP
|
||||
if (xEventGroupGetBits(wifi_event_group) & WIFI_AP_MODE_BIT) {
|
||||
//wifi_ap_stop();
|
||||
// wifi_ap_stop();
|
||||
}
|
||||
}
|
||||
} else if (mode_bits & WIFI_STA_MODE_BIT) {
|
||||
xEventGroupWaitBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
// We're in STA mode: block until disconnected from the AP
|
||||
xEventGroupWaitBits(
|
||||
wifi_event_group,
|
||||
WIFI_STA_DISCONNECTED_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY
|
||||
);
|
||||
}
|
||||
|
||||
// Prevent this task from hogging the CPU when idle
|
||||
//vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Botão e tratamento
|
||||
// Button press handler
|
||||
//
|
||||
static void handle_button_press(void) {
|
||||
ESP_LOGI(TAG, "Ativando modo AP");
|
||||
// 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");
|
||||
wifi_ap_start();
|
||||
}
|
||||
}
|
||||
|
||||
// Task to handle button press/release notifications
|
||||
static void user_input_task_func(void *param) {
|
||||
uint32_t notification;
|
||||
while (1) {
|
||||
if (xTaskNotifyWait(0x00, 0xFF, ¬ification, portMAX_DELAY)) {
|
||||
for (;;) {
|
||||
// Wait for notification bits from ISR
|
||||
if (xTaskNotifyWait(
|
||||
0, // do not clear any bits on entry
|
||||
UINT32_MAX, // clear all bits on exit
|
||||
¬ification,
|
||||
portMAX_DELAY)) {
|
||||
// Handle button press event
|
||||
if (notification & PRESS_BIT) {
|
||||
press_tick = xTaskGetTickCount();
|
||||
pressed = true;
|
||||
ESP_LOGI(TAG, "Pressed Button");
|
||||
ESP_LOGI(TAG, "Button Pressed");
|
||||
handle_button_press();
|
||||
}
|
||||
|
||||
if (notification & RELEASED_BIT && pressed) {
|
||||
// Handle button release event (only if previously pressed)
|
||||
if ((notification & RELEASED_BIT) && pressed) {
|
||||
pressed = false;
|
||||
ESP_LOGI(TAG, "Reladead Buttton");
|
||||
ESP_LOGI(TAG, "Button Released");
|
||||
handle_button_press();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ISR for button GPIO interrupt (active-low)
|
||||
static void IRAM_ATTR button_isr_handler(void *arg) {
|
||||
BaseType_t higher_task_woken = pdFALSE;
|
||||
TickType_t now = xTaskGetTickCountFromISR();
|
||||
|
||||
if (now - last_interrupt_tick < pdMS_TO_TICKS(DEBOUNCE_TIME_MS)) return;
|
||||
// Debounce: ignore interrupts occurring too close together
|
||||
if (now - last_interrupt_tick < pdMS_TO_TICKS(DEBOUNCE_TIME_MS)) {
|
||||
return;
|
||||
}
|
||||
last_interrupt_tick = now;
|
||||
|
||||
if (!gpio_get_level(board_config.button_wifi_gpio)) {
|
||||
xTaskNotifyFromISR(user_input_task, RELEASED_BIT, eSetBits, &higher_task_woken);
|
||||
// Read GPIO level: 0 = button pressed, 1 = button released
|
||||
int level = gpio_get_level(board_config.button_wifi_gpio);
|
||||
if (level == 0) {
|
||||
// Notify task: button pressed
|
||||
xTaskNotifyFromISR(
|
||||
user_input_task,
|
||||
PRESS_BIT,
|
||||
eSetBits,
|
||||
&higher_task_woken);
|
||||
} else {
|
||||
xTaskNotifyFromISR(user_input_task, PRESS_BIT, eSetBits, &higher_task_woken);
|
||||
// Notify task: button released
|
||||
xTaskNotifyFromISR(
|
||||
user_input_task,
|
||||
RELEASED_BIT,
|
||||
eSetBits,
|
||||
&higher_task_woken);
|
||||
}
|
||||
|
||||
// Yield to higher priority task if unblocked
|
||||
if (higher_task_woken) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void button_init(void) {
|
||||
gpio_config_t conf = {
|
||||
.pin_bit_mask = BIT64(board_config.button_wifi_gpio),
|
||||
@@ -167,6 +225,7 @@ static void button_init(void) {
|
||||
static void init_modules(void) {
|
||||
peripherals_init();
|
||||
//api_init();
|
||||
buzzer_init();
|
||||
ESP_ERROR_CHECK(rest_server_init("/data"));
|
||||
protocols_init();
|
||||
evse_manager_init();
|
||||
@@ -174,11 +233,12 @@ static void init_modules(void) {
|
||||
button_init();
|
||||
auth_init();
|
||||
loadbalancer_init();
|
||||
meter_manager_grid_init();
|
||||
meter_manager_grid_start();
|
||||
meter_manager_evse_init();
|
||||
meter_manager_evse_start();
|
||||
meter_manager_init();
|
||||
meter_manager_start();
|
||||
|
||||
|
||||
|
||||
//wifi_ap_start();
|
||||
// Outros módulos (descomente conforme necessário)
|
||||
// meter_init();
|
||||
// ocpp_start();
|
||||
|
||||
Reference in New Issue
Block a user