Task for monitoring a variable

Let’s say I have a device where I want to show a battery level indicator. What would be the best way of refreshing it?
My first thought was creating a task that would check the battery level and update the label with the battery icon if needed. But I am not sure of how the lvgl task internals work. What would be the hit on cpu cycles and battery consumption if I have a permanent task running every minute or something like that? Is it negligible?
Is there a better way that I am missing?
Thank you for the help!

In general, when you want to save power you put your device to sleep.
What costs power/time/processing is usually not lvgl task, but the screen rendering and refresh cycle.
The screen itself also consumes power when it’s on, especially if there is some backlight.

So best practice would be to put the device to sleep and not refresh the screen for a while.
The screen itself can sometimes be configured to sleep mode, depending on the hardware.

If you are using ESP32 you can choose between modem sleep, light sleep, deep sleep and hibernation.
With a GUI you probably would not select deep-sleep / hibernation. since a wakeup from them requires booting the device.
If you choose light sleep, for example, the CPU and RAM are clock-gated, so everything is actually “frozen”. If you have an lvgl task, it will not execute and screen will not refresh until leaving this sleep mode.
Take into account, however, that light sleep power consumption is significantly higher (around 1 mA) compared to deep sleep (10-150 uA). You need to add the power consumption of your other peripherals of course, such as your screen, your power regulator, etc.

So, to summarize,

  • Select your sleep mode and make sure you configure it correctly
  • When the device sleeps lvgl does not run and screen is not refreshed.
  • Schedule periodic wakeups to refresh the screen
  • You can rely on lvgl tasks to update the screen when the device is not sleeping, they usually don’t cost much.
1 Like

Once again thank you for your detailed answer!
I will give a proper look to sleep modes.
Always good to learn from you!