How to structure the GUI?

I assume you checked that the object reference you are using is not null? Or, perhaps whatever interrupt/clock mechanism you are using to call lv_tick_inc stops functioning?

Is there an API I can call to get the object reference? or do I just check the value of lv_obj_t *obj (in my case gauge1 or led1 etc)?

This behaviour is exhibited if I do the update via an lv_task or directly in my main loop, so I believe the tick timer is not the fault here as the object updates if I do everything in an ino file like so:

int g_val = 0;
lv_obj_t * gauge1;
void setup() {
    while (!Serial && millis() < 5000);
    Serial.begin(115200);
    pinMode(4, OUTPUT);
    tft.begin();
    tft.setBitDepth(16);
    tft.setRotation(1);
    //tft.setTearingEffect(true);
    tft.setFrameRate(78);
    tft.setAddrWindow(0, 0, screenWidth - 1, screenHeight - 1);
    

    if (!ts.begin(40)) { 
        Serial.println("Unable to start touchscreen.");
    } 
    else { 
        Serial.println("Touchscreen started ok."); 
    }
 
    digitalWrite(4, HIGH);
    
    lv_init();
    lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * LV_VER_RES_MAX/ 40 );
    //lv_log_register_print_cb(my_print); /* register print function for debugging */
    
   /*Initialize the display*/
    lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.buffer = &disp_buf;
    lv_disp_drv_register(&disp_drv);


    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);             /*Descriptor of a input device driver*/
    indev_drv.type = LV_INDEV_TYPE_POINTER;    /*Touch pad is a pointer-like device*/
    indev_drv.read_cb = my_touchpad_read;      /*Set your driver function*/
    lv_indev_drv_register(&indev_drv);         /*Finally register the driver*/
    
    Serial.println("tick.begin");
    tick.begin(lv_tick_handler, LVGL_TICK_PERIOD * 1000);  // Start ticker

    static lv_color_t needle_colors[3];
    needle_colors[0] = LV_COLOR_BLUE;
    needle_colors[1] = LV_COLOR_ORANGE;
    needle_colors[2] = LV_COLOR_PURPLE;

    /*Create a gauge*/
    gauge1 = lv_gauge_create(lv_scr_act(), NULL);
    lv_gauge_set_needle_count(gauge1, 1, needle_colors);
    lv_obj_set_size(gauge1, 200, 200);
    lv_obj_align(gauge1, NULL, LV_ALIGN_CENTER, 0, 0);

    /*Set the values*/
    lv_gauge_set_value(gauge1, 0, 0);
}


const int loopDelay1 = 5; // Make a request every 5ms
unsigned long timeNow1 = 0;

const int loopDelay2 = 10; // Make a request every 10ms
unsigned long timeNow2 = 0;
void loop() {
if (millis() > timeNow1 + loopDelay1)
    {
      timeNow1 = millis();
      lv_task_handler(); /* let the GUI do its work */
    }

if (millis() > timeNow2 + loopDelay2)
    {
      timeNow2 = millis();
      g_val++;
      lv_gauge_set_value(gauge1, 0, g_val);
      if (g_val >=100){
        g_val = 0;
        }
      
    }
}

Whereas in my actual app, I have all the LVGL object and UI rendering code in a separate .c file (its based off the lv printer demo) and I added a function in that .c file to update the gauge/labels and it is called from the main loop within the .ino file
So I suspect my object might be NULL as you suggested.

In this case the easiest solution is to use a global variable to store object references. Ensure you are not shadowing the variable (redeclaring it within a function) otherwise the global won’t get updated. I think Arduino IDE has a warning for that, but I don’t think they enable it by default.

1 Like

This was the case… I was sure I had removed the variable declaration of all the labels and gauge from my startup function.

All is working now, and I must say, even smoother than it was before! Very amazed at the performance bump after this fix

Thought I’d share a video of the first live data test with LVGL handling the UI
https://youtu.be/wKEKcuaV_A8
I am positively surprised at the smoothness and speed of the UI! Very stoked!

1 Like

Love it! The gauge animation when you open the page looks amazing!

1 Like

WoW! Good job man!