Event Loop Base issues

Fundeckhermit
Posts: 7
Joined: Fri Apr 01, 2022 2:45 pm

Event Loop Base issues

Postby Fundeckhermit » Wed Oct 02, 2024 4:20 pm

Hi,

I've been trying to implement the Event Loop Library. Posting and registering events using a hardcoded static string works fine.
When the string is dynamic and identical to the base it fails to trigger.

I'm registering an event using:

Code: Select all

esp_event_handler_instance_register("COMMANDBASE1", 0, commandfunction, NULL, NULL);
And posting it with an hardcoded string works:

Code: Select all

esp_event_post("COMMANDBASE1", 0, NULL, 0, portMAX_DELAY)
When I change the base to a char* variable containing "COMMANDBASE1" it fails:
This string is from UART so I cannot make it hardcoded.

Code: Select all

esp_event_post(basevar, 0, NULL, 0, portMAX_DELAY)
With a hack and a lookup table I can trigger the event again:

Code: Select all

inline esp_event_base_t filterEvents(char* eventBase)
{
    if(strcmp(eventBase, "COMMANDBASE1") == 0)
    {
        return (esp_event_base_t)"COMMANDBASE1";
    }  
    return (esp_event_base_t)"UNKNOWN";
}
esp_event_post(filterEvents(basevar),0, NULL, 0, portMAX_DELAY)
How do I convert a char* to esp_event_base_t as the esp_event_post function only seems to work with hardcoded strings?

MicroController
Posts: 1605
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Event Loop Base issues

Postby MicroController » Wed Oct 02, 2024 5:47 pm

See e.g. the example at https://github.com/espressif/esp-idf/tr ... event_loop

It uses the provided macros to declare and define event bases.

Notice that an "event base" is not a string but actually only a pointer, i.e. a memory address. An event base is identified by this address only. Two strings will not live at the same address, unless they are identical compile-time constants/literals and merged into one by the compiler/linker.

Fundeckhermit
Posts: 7
Joined: Fri Apr 01, 2022 2:45 pm

Re: Event Loop Base issues

Postby Fundeckhermit » Thu Oct 03, 2024 7:49 am

Thank you for the reply,

Now it makes sense to me on what is happening under the hood.
So the only method of converting char* to a base address is by mapping?

I'll study the macro's a bit more, the documentation about them was quite sparse.

Who is online

Users browsing this forum: No registered users and 76 guests