Newbie Q - lv_task_once

Description

ESP32 / Arduino 1.8.9 working off the Example file ESP32_TFT_eSPI

What do you want to achieve?

Update the text of a label with lv_task_once

What have you tried so far?

Tried the documentation and all the examples I can find but i’m clearly missing something simple here as I slowly get my head around LittlevGL :|:upside_down_face:

Code to reproduce

When i want to update the label i’m running…

lv_task_once(my_task);

which is erroring with "cannot convert ‘void ()()’ to 'lv_task_t {aka _lv_task_t*}’ for argument ‘1’ to ‘void lv_task_once(lv_task_t*)’*

Then for the my_task function i have

void my_task()
{
  lv_label_set_text(label, "Updated Text!!!!");
  }

The original label is created with…

  /* Create simple label */
  lv_obj_t *label = lv_label_create(lv_scr_act(), NULL);
  lv_label_set_text(label, "Hello Arduino! (V6.0)");

2 things:

  • Why are you trying to use an lv_task here? It might be simpler to just call lv_label_set_text where you need to.
  • If you need to use an lv_task, read the documentation.

If you read the error you’re getting, it’s because lv_task_once expects an lv_task_t * as a parameter but you’re trying to pass it a function. Reading the task documentation will then show you that you can create a task with lv_task_create, but calling lv_task_once means that it will be deleted after it runs. So a task probably isn’t what you’re looking for.

1 Like

Thanks for your reply embeededt and thank you for your patience, i managed to get there!

For other flailing newbies with wobbly syntax- i suggest you check out https://github.com/littlevgl/lvgl/issues/492#issuecomment-440170076

@flossandmeditate to expand the solution,

Method1,

lv_obj_t * my_label;
int num = 0;

/* In setup */
my_label = lv_label_create(lv_scr_act(), NULL);

/* In loop: */
char buf[32];
sprintf(buf, "num: %d\n", num);
num++;
lv_label_set_text(my_label, buf);

Method 2,

lv_obj_t * my_label;
int num = 0;
char buf[32]; /* Must be persistent */

/* In setup */
my_label = lv_label_create(lv_scr_act(), NULL);
lv_label_set_static_text(my_label, buf);

/* In loop: */
sprintf(buf, "num: %d\n", num);
num++;
lv_label_set_static_text(my_label, NULL);

The difference is that with method 1, lv_label_set_text needs to reallocate memory each time you call it (from its perspective the string length may have changed) and with method 2, lv_label_set_text_static does not does not need to as it merely uses the static buffer you have allocated to the label when it was first called.

1 Like

I recommend turning on the related warnings for your compiler. Warnings seem annoying at first, but in many cases they actually save you a lot of debugging time by finding the issue for you.

2 Likes