ESP32 "Event Handling" event handling mechanism

1. Today, I will introduce the event handling mechanism of ESP32 "Event Handling".

Document link https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-guides/event-handling.html

Question 1. What is an event? What is the event handling mechanism?

In my opinion, events and signals are actually the same. An event (or signal) comes to execute a function to handle some things, similar to the interrupt of a single-chip computer.

The way to use events and event handling functions to solve problems becomes an event handling mechanism.

Some components of ESP32 use events to notify applications when their status changes, such as wifi connection and disconnection events. The following accepts ESP32's event handling mechanism.

Legacy Event Loop Legacy Event Loop

Before the introduction of the esp_event Library event library, the events of the wifi driver, Ethernet, and tcp/ip protocol stack were all processed through the traditional event loop, which can be regarded as a while(1); loop. The traditional event loop only supports the event id and event information structure that the system has defined, and it cannot handle user-defined events and Bluetooth mesh events. Traditional events only support one event processing function, and application components cannot handle wifi and ip events alone, and the application needs to throw these events.

esp_event Library Event Loop The event loop provided by the esp event library

The event library is used to replace the traditional event loop and provides a default event loop to handle wifi and ip events.

The capabilities of the event library:

The event library allows components to declare an event to the processing function registered by other components, and execute the corresponding processing function when the event occurs. This allows loosely coupled components to perform corresponding operations when the state of other components changes without being called by the application. For example, put all connection processing in an advanced library, subscribe to the connection events of other components, and when the connection status of other components changes, the advanced processing performs corresponding operations. This mechanism simplifies the handling of events in a serialized manner and delays the execution of code in another context. Simply put, it is to separate the event location and the event processing location, the event processing location to subscribe to the event that will occur at the event location, and the event processing of the entire system is placed in the same location for processing, which simplifies the system design .

2. Use of esp_event Library API.

API位置:esp-idf-v4.1\components\esp_event

Create a user event loop:
#include "esp_event.h"
#include "string.h"
// custom event family
ESP_EVENT_DECLARE_BASE (s_test_base1);
ESP_EVENT_DEFINE_BASE (s_test_base1);
// Event ID
enum {     TEST_EVENT_BASE1_EV1,     TEST_EVENT_BASE1_EV2,     TEST_EVENT_BASE1_MAX};/ /Event handling functionvoid handle(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data){     printf("event_data: %s\n", (char *)event_data);}








void app_main()
{     char buf[] = "test event handle";     //Define event loop object     esp_event_loop_handle_t loop1;     //Event loop configuration     esp_event_loop_args_t loop_config = {         .queue_size = 32, //Event queue, set the number of events that can be received         .task_name = "loop", //The event relies on a separate freertos task to set the task         name.task_priority = 1, //task priority.task_stack_size         = 2048, //task stack         size.task_core_id = 0 //used by the task cpu core id     };     //Create event loop     esp_event_loop_create(&loop_config, &loop1);     //Register events and event handlers to the loop     esp_event_handler_register_with(loop1, s_test_base1, TEST_EVENT_BASE1_EV1, ​​handle, NULL);     while(1) {















    

        //Trigger event, verify whether the processing function is called
        esp_event_post_to(loop1, s_test_base1, TEST_EVENT_BASE1_EV1, ​​buf, strlen(buf) + 1, portMAX_DELAY);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

Create a default event loop (compared with the user event loop, there are fewer handles representing the event loop)

#include "esp_event.h"
#include "string.h"
//User-defined event family
ESP_EVENT_DECLARE_BASE(s_test_base1);
ESP_EVENT_DEFINE_BASE(s_test_base1);
//event id
enum {     TEST_EVENT_BASE1_EV1,     TEST_EVENT_BASE1_EV1,     TEST_EVENT_BASE1 } 2, TEST_MAX



void handle(void* event_handler_arg, esp_event_base_t event_base,  int32_t event_id, void* event_data)
{
    printf("event_data: %s\n", (char *)event_data);
}

void app_main()
{
    char buf[] = "te's event handle";
    esp_event_loop_create_default();
    esp_event_handler_register(s_test_base1, TEST_EVENT_BASE1_EV1, handle, NULL);

    while(1) {
        esp_event_post(s_test_base1, TEST_EVENT_BASE1_EV1, buf, strlen(buf) + 1, portMAX_DELAY);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

Create event loops that do not use proprietary tasks (mainly correct esp_event_loop_run()函数本质的解读)

esp_err_t esp_event_loop_run(esp_event_loop_handle_t event_loop, TickType_t ticks_to_run)

The official description of this function is to assign an event to the event loop. In fact, it is difficult to understand clearly from this level. From the literal meaning of the function, it can be seen that an event loop event_loop runs ticks_to_run tick numbers. To put it bluntly, it means temporary operation. This event loops for a certain period of time. If you put this function in while(1), it will run all the time. This function is for the task name is not specified when the time loop is created (essentially, a freertos task is not opened separately for the event loop), then only the event loop can be run to receive the event and call the event processing function normally, so it is calledesp_event_loop_run函数启动事件循环的操作。

Below I will create a separate task to executeesp_event_loop_run()函数为例进行了测试。

#include "esp_event.h"

#include "string.h"

//User-defined event family

ESP_EVENT_DECLARE_BASE(s_test_base1);

ESP_EVENT_DEFINE_BASE(s_test_base1);

//Event id

enum {

    TEST_EVENT_BASE1_EV1,

    TEST_EVENT_BASE1_EV2,

    TEST_EVENT_BASE1_MAX

};

//Event processing function

void handle(void* event_handler_arg, esp_event_base_t event_base,  int32_t event_id, void* event_data)

{

    printf("event_data: %s\n", (char *)event_data);

}

 

static void event_loop(void* args)

{

    esp_event_loop_handle_t event_loop = (esp_event_loop_handle_t) args;

 

    while(1) {

        esp_event_loop_run(event_loop, portMAX_DELAY);

    }

}

 

void app_main()

{

    char buf[] = "test event handle 3";

    //Define the event loop object

    esp_event_loop_handle_t loop1;

    //Event loop configuration

    esp_event_loop_args_t loop_config = {

        .queue_size = 32, //Event queue, set the number of events that can be received

        .task_name = "loop", //The event relies on a separate freertos task to set the task name

        .task_priority = 1, //task priority

        .task_stack_size = 2048, //task stack size

        .task_core_id = 0 //The CPU core id used by the task

    };

    loop_config.task_name = NULL;

    //Create time loop

    esp_event_loop_create(&loop_config, &loop1);

    //Register events and event handlers to the loop

    esp_event_handler_register_with(loop1, s_test_base1, TEST_EVENT_BASE1_EV1, handle, NULL);

 

    TaskHandle_t mtask;

    xTaskCreate(event_loop, "task", 2584, (void*) loop1, 1, &mtask);

    //esp_event_loop_run(event_loop, portMAX_DELAY);

    while(1) {

        //Trigger event, verify whether the processing function is called

        esp_event_post_to(loop1, s_test_base1, TEST_EVENT_BASE1_EV1, buf, strlen(buf) + 1, portMAX_DELAY);

        vTaskDelay(1000 / portTICK_PERIOD_MS);

    }

}

to sum up:

Steps to use esp_event Library API:

1. Create an event loop;
2. Register events and event processing functions (for the definition of events and event processing functions, please refer to my case);
3. Trigger events and execute event processing functions;

Guess you like

Origin blog.csdn.net/qq_34473570/article/details/108548214