How to pass variables between components except event handlers?

sazanof
Posts: 22
Joined: Wed Sep 13, 2023 10:22 am

How to pass variables between components except event handlers?

Postby sazanof » Fri Sep 22, 2023 10:37 am

Hello!

I'm new to C and ESP-IDF. I have a project that has app_main.c and a components directory consisting of components
components
-- wifi
------wifi.h
------wifi.c
------CMakeList.txt
-- ethernet
------ethernet.h
------ethernet.c
------CMakeList.txt
-- webserver
------webserver.h
------webserver.c
------CMakeList.txt
-- ntc
------ntc.h
------ntc.c
------CMakeList.txt

Each component includes only those header files that are necessary for its operability (ntc will include only files for working with ADC, and the webserver - only for working with the webserver)

I register (calling functions) these components in the file app_main

Code: Select all

init_wifi();
init_ethernet();
init_ntc();
init_webserver();
(Maybe I'm doing it wrong)

Now, to transfer data from the ADC reading task, I use esp_event library

ntc.c

Code: Select all

esp_err_t err = esp_event_post(APP_EVENTS, S_ADC_GOT_TEMP, &data, sizeof(data), portMAX_DELAY);
app_main

Code: Select all

ESP_ERROR_CHECK(esp_event_handler_register(APP_EVENTS, S_ADC_GOT_TEMP, adc_got_temp_event_handler, NULL));

Maybe there is a way how to transfer data between components, except in events?

However, in what way is it preferable to transfer data between application components?

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

Re: How to pass variables between components except event handlers?

Postby MicroController » Fri Sep 22, 2023 2:26 pm

Maybe there is a way how to transfer data between components, except in events?

However, in what way is it preferable to transfer data between application components?
There are multiple options. Which one is "best" suited depends on the use case.
1. synchronous processing: Code may call a function of another component to directly receive some data/value, either via the function's return value or by passing a pointer to a variable to the function for the function to put data into.
2. synchronous callbacks: Code in one component (A) can register a function pointer with another component (B), so that B can call the function in component A whenever it wants to pass some data to A. ("listener pattern"/"subscriber pattern")
3. asynchronous processing: In case data isn't instantly available at any time, there are again multiple options:
3.1. Events: As you have noted, (data) events can be sent to the "event loop" which then in turn uses the callback approach to pass the event on to one or more receivers
3.2. Queues or buffers: This may be the most "native" way of inter-task communication in a multi-tasking environment and often yields the cleanest code. Whenever data becomes available in component B, it places a message into a queue (xQueueSend()). In component A the code can then just block on that queue (xQueueReceive()) and handle the message as soon as the queue-receive call returns.
The same principle applies to "stream buffers" (or ESP-IDF's ring buffers) which are more suited for variably-sized items or streams of bytes.

sazanof
Posts: 22
Joined: Wed Sep 13, 2023 10:22 am

Re: How to pass variables between components except event handlers?

Postby sazanof » Fri Sep 22, 2023 2:48 pm

Thank you again very much! My PHP brain is now trying to understand the principles of how the code works in C :D . I think everything will work out.

Thanks!

Who is online

Users browsing this forum: Google [Bot] and 94 guests