Espressif ESP32-S2 Programming Manual page 846

Table of Contents

Advertisement

Chapter 2. API Reference
events get posted by event sources and handled by event handler functions. These two appear prominently in the event
loop library APIs.
Using this library roughly entails the following flow:
1. A user defines a function that should run when an event is posted to a loop. This function is referred to as the
event handler. It should have the same signature as esp_event_handler_t.
2. An event loop is created using esp_event_loop_create(), which outputs a handle to the loop of type
esp_event_loop_handle_t. Event loops created using this API are referred to as user event loops.
There is, however, a special type of event loop called the default event loop which are discussed here.
3. Components register event handlers to the loop using esp_event_handler_register_with(). Han-
dlers can be registered with multiple loops, more on that here.
4. Event sources post an event to the loop using esp_event_post_to().
5. Components wanting to remove their handlers from being called can do so by unregistering from the loop using
esp_event_handler_unregister_with().
6. Event loops which are no longer needed can be deleted using esp_event_loop_delete().
In code, the flow above may look like as follows:
// 1. Define the event handler
void
run_on_event(void*
event_data)
{
// Event handler logic
}
void
app_main()
{
// 2. A configuration structure of type esp_event_loop_args_t is needed
specify the properties of the loop to be
// created. A handle of type esp_event_loop_handle_t is obtained, which
needed by the other APIs to reference the loop
// to perform their operations on.
esp_event_loop_args_t loop_args
.queue_size
.task_name
=
.task_priority
.task_stack_size
.task_core_id
};
esp_event_loop_handle_t loop_handle;
esp_event_loop_create(&loop_args, &loop_handle);
// 3. Register event handler defined in (1). MY_EVENT_BASE and
specifies a hypothetical
// event that handler run_on_event should execute on when it gets posted
the loop.
esp_event_handler_register_with(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, run_
on_event, ...);
...
// 4. Post events to the loop. This queues the event on the event loop.
some point in time
// the event loop executes the event handler registered to the posted
in this case run_on_event.
// For simplicity sake this example calls esp_event_post_to from app_main,
posting can be done from
// any other tasks (which is the more interesting use case).
esp_event_post_to(loop_handle, MY_EVENT_BASE, MY_EVENT_ID, ...);
Espressif Systems
handler_arg, esp_event_base_t base,
=
{
=
...,
...
=
...,
=
...,
=
...
Submit Document Feedback
835
int32_t
id,
void*␣
to␣
is␣
MY_EVENT_ID␣
to␣
At␣
event,␣
but␣
(continues on next page)
Release v4.4

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ESP32-S2 and is the answer not in the manual?

Subscribe to Our Youtube Channel

Table of Contents

Save PDF