Task usage understanding

HitecSmartHome
Posts: 11
Joined: Mon Mar 18, 2024 9:22 am

Re: Task usage understanding

Postby HitecSmartHome » Mon May 27, 2024 9:51 am

MicroController wrote:
Mon May 27, 2024 8:43 am
HitecSmartHome wrote:
Mon May 27, 2024 7:02 am
For example the Time class ... just loops and checks and stores the uptime of the chip.
Why does it "store" the uptime? Can't it just "calculate"/provide the uptime when requested?
Hardware class ... pushes data to uart, waits for the response and it pushes the next packet as soon as it can.
This task should use the blocking UART API. I.e.,
do not do something like

Code: Select all

// BAD:
// Busy waiting:
while(!UartDataAvailable()) {
// Nothing here, just burning CPU cycles.
}
but instead:

Code: Select all

size_t rxLen;
while((rxLen = uart_read_bytes(..., portMAX_DELAY)) >= 0) {
// Process rxLen bytes of received data...
}
Component class ... needs live data from the Hardware class and update the states of the components accordingly )
The Component task could wait for the Hardware task to provide new data, e.g. via a queue (xQueueSend() in Hardware task, xQueueReceive() in Component).

Well, it is all true. You are right.
The reason it calculates it in every second is because it sends the uptime as an SSE to a http client.
It also sends the uptime data to a server every second. It also more expensive to calculate it when requested because the request usually comes in a critical context. For example one of the tasks is a Logger class. When some task calls Logger.log(data) the Logger class will request the time and the uptime too. The log must happen as fast as it can. If the Logger would call the Time class and the Time class had to calculate the uptime and the readable time string when the logging happens, it would be really slow. What if the logging happens in a critical section? It could trigger a watchdog or the esp would restart before the log written.

But you are 100% right, I should rethink my approach of the application. Thank you for the explanations.

Who is online

Users browsing this forum: No registered users and 96 guests