Page 1 of 1

Can not figure out esp_event_base , and event_id are known in default event loop example

Posted: Fri Nov 19, 2021 4:32 pm
by noweare
Hello,

I am going through the example default_event_loop example from the examples directory
and I don't understand how esp_event_base and event_id variables are chosen in the example.

I would think that there was a list of each variable for each esp32 component, but I can
not find one.

I guess I am trying to figure out what links actual events to these variables ?

These variables are used in both registering a callback and the handler itself.

For example, to register an instance of a handler function this function is used

Code: Select all


esp_err_t esp_event_handler_instance_register(esp_event_base_t event_base,
                                              int32_t event_id,
                                              esp_event_handler_t event_handler,
                                              void *event_handler_arg,
                                              esp_event_handler_instance_t *context)



Re: Can not figure out esp_event_base , and event_id are known in default event loop example

Posted: Fri Nov 19, 2021 9:49 pm
by chegewara

Re: Can not figure out esp_event_base , and event_id are known in default event loop example

Posted: Sat Nov 20, 2021 12:53 am
by noweare
So I am understanding the event loop stuff a little better.
I was pretty confused about it and not out of the woods yet.

For event_base and event_id I was looking for documented events like the uart has i.e. UART_DATA, UART_BREAK etc...
but after spending time on the default event loop and user event loop examples the event_base and event_id are user defined.

Thanks for gathering the documentation. I had read them but it just didn't sink in.

Re: Can not figure out esp_event_base , and event_id are known in default event loop example

Posted: Sat Nov 20, 2021 11:52 am
by chegewara
It is not easy, but its what for you have docs and IDEs with different tools.
For example i am using VS code, which has search across all files tool. When i searched ESP_EVENT_DECLARE_BASE there was some amount results, then i had to remove docs and example files and finally i found 13 declarations of different base_id.
noweare wrote: For event_base and event_id I was looking for documented events like the uart has i.e. UART_DATA, UART_BREAK etc...
but after spending time on the default event loop and user event loop examples the event_base and event_id are user defined.
This is another case, because not all events belongs to event loops. UART is not handling events with with event loop, but its using Queue.
https://github.com/espressif/esp-idf/bl ... main.c#L46

And again, you can find all events used by UART in docs:
https://docs.espressif.com/projects/esp ... ent_type_t


This is simple snippet code you can use to learn base_id for all events handled in your app (any app):

Code: Select all

static void evt_handler(void* arg, esp_event_base_t event_base,
                                    int32_t event_id, void* event_data)
{
    ESP_LOGW("", "ANY base: %s, event: %d\n", event_base, event_id);
}


void app_main()
{
...
// remember to create default event loop before this line
// esp_event_loop_create_default()
    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_register(ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, evt_handler, NULL, &instance_any_id);
...
}

Re: Can not figure out esp_event_base , and event_id are known in default event loop example

Posted: Sat Nov 20, 2021 9:06 pm
by noweare
Yeah, not easy but the documentation is very good. Just a lot of it. The examples are really good too.

The event loop library gives us the freedom of making up our own events and what we name them.

The idf has components with built-in events like the uart so I thought the library was for built-in system events.
The example got me thinking correctly about it, though. That's not to mean I know when best to use the event loop library in my application. Hopefully that will be more clear the longer i spend learning this system.