Mbed OS fails with lvgl library

Hi everyone,
I'll start by saying that I'm now starting to work with the LVGL library and Arduino display.
basically I would like to write simple text inside a grid but when I try Mbed OS breaks.
I'll leave you my code.

#include "Arduino_H7_Video.h"
#include "lvgl.h"
#include "Arduino_GigaDisplayTouch.h"

Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;

void setup() {
  Display.begin();
  TouchDetector.begin();

  //Display & Grid Setup
  lv_obj_t* screen = lv_obj_create(lv_scr_act());
  lv_obj_set_size(screen, Display.width(), Display.height());

  static lv_coord_t col_dsc[] = { 370, 370, LV_GRID_TEMPLATE_LAST };
  static lv_coord_t row_dsc[] = { 215, 215, 215, 215, LV_GRID_TEMPLATE_LAST };

  lv_obj_t* grid = lv_obj_create(lv_scr_act());
  lv_obj_set_grid_dsc_array(grid, col_dsc, row_dsc);
  lv_obj_set_size(grid, Display.width(), Display.height());

  //top left
  lv_obj_t* obj;
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row
  lv_label_set_text(obj,"a");

  //bottom left
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  //top right
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row

  //bottom right
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
}

void loop() {
  lv_timer_handler();
}

I would also like to delve deeper into the library, for example it is not clear to me how to press a button and change the state of a variable or print the value of an entire variable on the screen.
can anyone help me?

What exactly breaks?

There is also a lot of information here :https://docs.arduino.cc/tutorials/giga-display-shield/lvgl-guide

I don't know exactly, the program works until you get to

lv_label_set_text(obj,"a");

then the red LED starts to flash.

Ah Missed that line, instead of that use this:

lv_obj_t *label = lv_label_create(obj);
lv_label_set_text_fmt(label, "Hello");
lv_obj_center(label);

lv_label_set_text_fmt would only be used for labels.

Hi @nocentini,
The problem is that you're trying to write the text inside a grid. And you can't!
As @BobTheDog explained, firstly you need to create a label inside the grid:

lv_obj_t * label = lv_label_create(obj);

and then add the text:

lv_label_set_text(label,"a");

Refer to LVGL documentation about label to better understanding.

okay so now I think I understand...
I also ask you if my reasoning is correct:
if I wanted to write an int or float type variable inside a label I would have to do something like this:

  lv_obj_t* obj;
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row

  lv_obj_t *My_Variabile = lv_label_create(obj);
  lv_label_set_text_fmt(label,MyVariabile);
  lv_obj_center(My_Variabile);

and if by chance I wanted to have a button that makes me change the state of a variable:

#include "Arduino_H7_Video.h"
#include "lvgl.h"
#include "Arduino_GigaDisplayTouch.h"

Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;
bool state = false;


static void btn_event_cb(lv_event_t * e) {
  state= true;

}

void setup() {
  Display.begin();
  TouchDetector.begin();

  //Display & Grid Setup
  lv_obj_t* screen = lv_obj_create(lv_scr_act());
  lv_obj_set_size(screen, Display.width(), Display.height());

  static lv_coord_t col_dsc[] = { 370, 370, LV_GRID_TEMPLATE_LAST };
  static lv_coord_t row_dsc[] = { 215, 215, 215, 215, LV_GRID_TEMPLATE_LAST };

  lv_obj_t* grid = lv_obj_create(lv_scr_act());
  lv_obj_set_grid_dsc_array(grid, col_dsc, row_dsc);
  lv_obj_set_size(grid, Display.width(), Display.height());

  //top left
  lv_obj_t* obj;
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row
  lv_obj_t * btn = lv_btn_create(obj);
  lv_obj_set_size(btn, 100, 40);
  lv_obj_center(btn);
  lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_CLICKED, NULL);

  //bottom left
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  lv_obj_t *label = lv_label_create(obj);
  lv_label_set_text_fmt(label, "the state is:", state);
  lv_obj_center(label);
  
  //top right
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row

  //bottom right
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
}

void loop() {
  lv_timer_handler();
}
1 Like

This will only set the label once, at that moment in time.

When state changes it will not be updated, so you need to do it again in btn_event_cb

so define lv_obj_t *label; where your state variable is.

Change the existing lv_obj_t *label = lv_label_create(obj); to label = lv_label_create(obj);

and have :

static void btn_event_cb(lv_event_t * e) {
  state= true;
  lv_label_set_text_fmt(label, "the state is:", state);
}

my goal was to create this button that when pressed made me go from 0 to 1 the variable and then wrote it to me from another part of the grid.
are you telling me that when I press the button only the area of the grid relating to the correct button updates?

I don't have the Arduino at hand at the moment and I can't do the various tests. Forgive my ignorance

Not quite.

I am trying to say that lv_label_set_text_fmt will only update the text once.

If you want to update the text again, you have to call lv_label_set_text_fmt again.

okay then I think I understand.
How stupid was I not to understand this, it's quite banal.
so what happens in the setup and the labels with the values that I impose are only used to give a startup value, then in the callback function I have to tell it which label to update and how.
similar thing if I wanted to update an integer that varies within the loop.
when the value is updated I write the command

lv_label_set_text_fmt(label,"MyVariabile is: %d", MyVariabile);

and I won.

I imagine something like that

#include "Arduino_H7_Video.h"
#include "lvgl.h"
#include "Arduino_GigaDisplayTouch.h"

Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;
int var = 0;
bool state = false;
lv_obj_t *label_counter; 
lv_obj_t *label_button; 

static void btn_event_cb(lv_event_t* e) {
   state=!state;
    lv_label_set_text_fmt(label_button, "Value: %d", state);
  
}

void setup() {
  Display.begin();
  TouchDetector.begin();

  
  lv_obj_t* screen = lv_obj_create(lv_scr_act());
  lv_obj_set_size(screen, Display.width(), Display.height());

  static lv_coord_t col_dsc[] = { 370, 370, LV_GRID_TEMPLATE_LAST };
  static lv_coord_t row_dsc[] = { 215, 215, 215, 215, LV_GRID_TEMPLATE_LAST };

  lv_obj_t* grid = lv_obj_create(lv_scr_act());
  lv_obj_set_grid_dsc_array(grid, col_dsc, row_dsc);
  lv_obj_set_size(grid, Display.width(), Display.height());

 
  lv_obj_t* obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row
  lv_obj_t * btn = lv_btn_create(obj);
  lv_obj_set_size(btn, 100, 40);
  lv_obj_center(btn);
  lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_CLICKED, NULL);

  // Inizializza l'etichetta per visualizzare il valore iniziale della variabile
  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  label_counter = lv_label_create(obj); // Inizializza l'etichetta
  lv_label_set_text_fmt(label_counter, "Value: %d", var); // Imposta il testo iniziale dell'etichetta
 

  obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  label_button = lv_label_create(obj);
  lv_label_set_text_fmt(label_button, "Value: %d", state);
}

void loop() {

  var++;


  lv_label_set_text_fmt(label_counter, "Value: %d", var);
  delay(1000); 
  lv_task_handler(); 
}

so I have a button, which when I press it makes me change the state of a variable from true to false and then during the loop it prints the value of a variable, obviously all in different grid cells

Perfect :slight_smile:

I'm trying to change the font of the text, since the default one is too small, but it gives me errors...
I use:

lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);

but:

WARNING: library MKRIMU claims to run on samd architecture(s) and may be incompatible with your current board which runs on mbed_giga architecture(s).
/private/var/folders/7s/v5ly7vts1pvfqgtmw4q_zv240000gn/T/.arduinoIDE-unsaved2023926-23217-itolry.ickdk/sketch_oct26b/sketch_oct26b.ino: In function 'void setup()':
/private/var/folders/7s/v5ly7vts1pvfqgtmw4q_zv240000gn/T/.arduinoIDE-unsaved2023926-23217-itolry.ickdk/sketch_oct26b/sketch_oct26b.ino:149:80: error: 'lv_font_montserrat_28' was not declared in this scope
   lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);
                                                                                ^~~~~~~~~~~~~~~~~~~~~
/private/var/folders/7s/v5ly7vts1pvfqgtmw4q_zv240000gn/T/.arduinoIDE-unsaved2023926-23217-itolry.ickdk/sketch_oct26b/sketch_oct26b.ino:149:80: note: suggested alternative: 'lv_font_montserrat_14'
   lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);
                                                                                ^~~~~~~~~~~~~~~~~~~~~
                                                                                lv_font_montserrat_14
/private/var/folders/7s/v5ly7vts1pvfqgtmw4q_zv240000gn/T/.arduinoIDE-unsaved2023926-23217-itolry.ickdk/sketch_oct26b/sketch_oct26b.ino:149:3: error: 'lv_obj_set_style_local_text_font' was not declared in this scope
   lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/private/var/folders/7s/v5ly7vts1pvfqgtmw4q_zv240000gn/T/.arduinoIDE-unsaved2023926-23217-itolry.ickdk/sketch_oct26b/sketch_oct26b.ino:149:3: note: suggested alternative: 'lv_obj_set_style_text_font'
   lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lv_obj_set_style_text_font

exit status 1

Compilation error: 'lv_font_montserrat_28' was not declared in this scope

Open Arduino/hardware/mbed/libraries/Arduino_H7_Video/src/lv_conf.h file and go to line 366:

...
#define LV_FONT_MONTSERRAT_26 0
#define LV_FONT_MONTSERRAT_28 1 // Set to 1
#define LV_FONT_MONTSERRAT_30 0
...

Pay attention that adding font increase the memory size of the sketch.

WARNING: library MKRIMU claims to run on samd architecture(s) and may be incompatible with your current board which runs on mbed_giga architecture(s).
/Users/andreanocentini/Desktop/arduino mega/programma_vecchio_lvgl/programma_vecchio_lvgl.ino: In function 'void setup()':
/Users/andreanocentini/Desktop/arduino mega/programma_vecchio_lvgl/programma_vecchio_lvgl.ino:152:1: error: 'lv_obj_set_style_local_text_font' was not declared in this scope
 lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/andreanocentini/Desktop/arduino mega/programma_vecchio_lvgl/programma_vecchio_lvgl.ino:152:1: note: suggested alternative: 'lv_obj_set_style_text_font'
 lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28);
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 lv_obj_set_style_text_font
/Users/andreanocentini/Desktop/arduino mega/programma_vecchio_lvgl/programma_vecchio_lvgl.ino:191:50: warning: format '%d' expects argument of type 'int', but argument 3 has type 'double' [-Wformat=]
   lv_label_set_text_fmt(label_AWA, "AWA: %d", AWA);
                                                  ^
/Users/andreanocentini/Desktop/arduino mega/programma_vecchio_lvgl/programma_vecchio_lvgl.ino:198:58: warning: format '%d' expects argument of type 'int', but argument 3 has type 'double' [-Wformat=]
   lv_label_set_text_fmt(label_LAT, "LAT: %d", latitudine1);
                                                          ^

exit status 1

Compilation error: 'lv_obj_set_style_local_text_font' was not declared in this scope

the code I use is:


lv_obj_t *label_station; 
lv_obj_t *label_time; 
lv_obj_t *label_battery;

lv_obj_t *label_TWA;
lv_obj_t *label_SOG;
lv_obj_t *label_Heading;

lv_obj_t *label_TWS;
lv_obj_t *label_GPSheading;
lv_obj_t *label_Pitch;

lv_obj_t *label_AWA;
lv_obj_t *label_LAT;
lv_obj_t *label_ROLL;

void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;  //Wait for user to open terminal

  Display.begin();
  TouchDetector.begin();

  lv_obj_t* screen = lv_obj_create(lv_scr_act());
  lv_obj_set_size(screen, Display.width(), Display.height());

  static lv_coord_t col_dsc[] = { 263, 263, 263, LV_GRID_TEMPLATE_LAST };
  static lv_coord_t row_dsc[] = { 25, 150, 150, 150, 150, LV_GRID_TEMPLATE_LAST };

  lv_obj_t* grid = lv_obj_create(lv_scr_act());
  lv_obj_set_grid_dsc_array(grid, col_dsc, row_dsc);
  lv_obj_set_size(grid, Display.width(), Display.height());
///////////////////////////////////////////////////////////////////////////////////
lv_obj_t* obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row
  
  label_station = lv_label_create(obj);
  lv_obj_align(label_station, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_station, "Stazione %s", Station ? "connessa" : "disconnessa");
///////////////////////////////////////////////////////////////////////////////////
 obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row

  label_time = lv_label_create(obj);
  lv_obj_align(label_time, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_time, "%d:%d:%d", ore, minuti, secondi );
///////////////////////////////////////////////////////////////////////////////////
   obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 2, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 0, 1);      //row
  label_battery = lv_label_create(obj);
  lv_obj_align(label_battery, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_battery, "bateria staz.: %d", battery);
///////////////////////////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  label_TWA = lv_label_create(obj);
  lv_obj_align(label_TWA, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_TWA, "TWA: %d", TWA);
  ///////////////////////////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  label_SOG= lv_label_create(obj);

  lv_obj_set_style_local_text_font(label_SOG, LV_PART_MAIN, LV_STATE_DEFAULT, &lv_font_montserrat_28); //problem!!!!!!

  lv_obj_align(label_SOG, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_SOG, "SOG: %.2f", velocita);
///////////////////////////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 2, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 1, 1);      //row
  label_Heading = lv_label_create(obj);
  lv_obj_align(label_Heading, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_Heading, "MHEAD: %.2f", heading);

////////////////////////////////////////////////////////////
       obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 2, 1);      //row
  label_TWS = lv_label_create(obj);
  lv_obj_align(label_TWS, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_TWS, "TWS: %d", TWS);
////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 2, 1);      //row
  label_GPSheading= lv_label_create(obj);
  lv_obj_align(label_GPSheading, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_GPSheading, "GHEAD: %d", rottaGps1);
////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 2, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 2, 1);      //row
  label_Pitch = lv_label_create(obj);
  lv_obj_align(label_Pitch, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_Pitch, "Pitch: %d", pitch1);

////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 0, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 3, 1);      //row
  label_AWA = lv_label_create(obj);
  lv_obj_align(label_AWA, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_AWA, "AWA: %d", AWA);
////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 1, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 3, 1);      //row
  label_LAT = lv_label_create(obj);
  lv_obj_align(label_LAT, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_LAT, "LAT: %d", latitudine1);
////////////////////////////////////////////////////////////
     obj = lv_obj_create(grid);
  lv_obj_set_grid_cell(obj, LV_GRID_ALIGN_STRETCH, 2, 1,  //column
                       LV_GRID_ALIGN_STRETCH, 3, 1);      //row
  label_ROLL = lv_label_create(obj);
  lv_obj_align(label_ROLL, LV_ALIGN_CENTER, 0, 0);
  lv_label_set_text_fmt(label_ROLL, "Pitch: %d", roll1);

This library is beautiful, you do beautiful and very useful things.
but let's say that basic things like writing text and changing a font are not well documented. I'm going crazy about it!

You're using a function related to an old version of LVGL library.
Arduino_H7_Video library is based on LVGL v8.3.5.
Please refer to the official LVGL documentation that is full of example and well documented.