Comment définir un Event_cb

Hello, I am new to the lvgl library. I’ve been on a project for 4 months. But I was sick in the meantime. Currently I am in the finishing phase of my project. Here is a snippet of the code for my project. In this code, I have two windows: fen_one and fen_two. My first question is the following: I would like to display the window fen_deux if I click on the button “START A MEASUREMENT” of the window fen_un. Can you help me on this?

/**

  • @file main

*/

/*********************

  •  INCLUDES
    

*********************/
#include <stdlib.h>
#include <unistd.h>

#include “lvgl/lvgl.h”
#include “lv_drivers/win_drv.h”
#include “lv_examples/src/lv_demo_widgets/lv_demo_widgets.h”

#include <windows.h>

/*********************

  •  DEFINES
    

*********************/

/**********************

  •  TYPEDEFS
    

**********************/

/**********************

  • STATIC PROTOTYPES
    **********************/

static void hal_init(void);
static int tick_thread(void *data);

/**********************

  • STATIC VARIABLES
    **********************/

/**********************

  •  MACROS
    

**********************/

/**********************

  • GLOBAL FUNCTIONS
    ********************/
    #if WIN32
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
    #else
    int main(int argc, char
    argv)
    #endif // WIN32
    {

static lv_obj_t * slider_label;

/*Initialize LittlevGL*/
lv_init();

/*Initialize the HAL for LittlevGL*/
hal_init();

/*Check the themes too*/
lv_disp_set_default(lv_windows_disp);



lv_obj_t * fen_un = lv_obj_create(lv_scr_act(), NULL);  //Création de la fenetre d'accueil
lv_obj_set_size(fen_un, 480, 320);              //Taille de l'écran


lv_obj_t * btn_1 = lv_btn_create(fen_un, NULL);         // Ajoute un bouton 1 à l'écran actuel
lv_obj_set_pos(btn_1, 50, 120);                         //Définit sa position
lv_obj_set_size(btn_1, 180, 30);                        //Définit sa taille
lv_obj_t * label_1 = lv_label_create(btn_1, NULL);      // Ajoute une étiquette au bouton
lv_label_set_text(label_1, "A PROPOS DU DISPOSITIF");   // Définit le texte de l'étiquette


lv_obj_t * btn_2 = lv_btn_create(fen_un, NULL);
lv_obj_set_pos(btn_2, 260, 120);
lv_obj_set_size(btn_2, 180, 30);
lv_obj_t * label_2 = lv_label_create(btn_2, NULL);
lv_label_set_text(label_2, "DEMARRER UNE MESURE");

lv_obj_t * fen_deux = lv_obj_create(NULL, NULL);
lv_obj_set_size(fen_deux, 480, 320);

lv_obj_t * btn_3 = lv_btn_create(fen_deux, NULL);
lv_obj_set_pos(btn_3, 150, 40);
lv_obj_set_size(btn_3, 180, 30);
lv_obj_t * label_3 = lv_label_create(btn_3, NULL);
lv_label_set_text(label_3, "PARAMETRES");


lv_obj_t * btn_5 = lv_btn_create(fen_deux, NULL);
lv_obj_set_pos(btn_5, 50, 100);
lv_obj_set_size(btn_5, 390, 30);
lv_obj_t * label_5 = lv_label_create(btn_5, NULL);
lv_label_set_text(label_5, "DEMARRER LE BALAYAGE");


    /* Create a slider in the center of the display */
lv_obj_t * slider = lv_slider_create(fen_deux, NULL);
lv_obj_set_width(slider, LV_DPI * 2);
lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0);
//lv_obj_set_event_cb(slider, slider_event_cb);
lv_slider_set_range(slider, 0, 100);

/* Create a label below the slider */
slider_label = lv_label_create(fen_deux, NULL);
lv_label_set_text(slider_label, "PROGRESSION DE LA MESURE");
lv_obj_set_auto_realign(slider_label, true);
lv_obj_align(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

lv_obj_t * btn_4 = lv_btn_create(fen_deux, NULL);
lv_obj_set_pos(btn_4, 50, 200);
lv_obj_set_size(btn_4, 180, 30);
lv_obj_t * label_4 = lv_label_create(btn_4, NULL);
lv_label_set_text(label_4, "GRANDEURS PHYSIQUES");


lv_obj_t * btn_7 = lv_btn_create(fen_deux, NULL);
lv_obj_set_pos(btn_7, 260, 200);
lv_obj_set_size(btn_7, 180, 30);
lv_obj_t * label_7 = lv_label_create(btn_7, NULL);
lv_label_set_text(label_7, "VOIR LES RESULTATS");

lv_obj_t * btn_6 = lv_btn_create(fen_deux, NULL);
lv_obj_set_pos(btn_6, 15, 260);
lv_obj_set_size(btn_6, 440, 30);
lv_obj_t * label_6 = lv_label_create(btn_6, NULL);
lv_label_set_text(label_6, "RETOUR");

lv_obj_t * fen_trois = lv_obj_create(NULL, NULL);
lv_obj_set_size(fen_trois, 480, 320);

lv_obj_t * tab = lv_tabview_create (fen_trois, NULL );
lv_obj_set_size (tab, LV_HOR_RES, LV_VER_RES);

lv_obj_t *  tab1= lv_tabview_add_tab (tab, " BALAYAGE " );



/*REMPLISSAGE DU TAB BALAYAGE*/


    /* Create the text area */

lv_obj_t * label1 = lv_label_create(tab1, NULL);
//lv_label_set_long_mode(label1, LV_LABEL_LONG_BREAK);
lv_obj_set_pos(label1, 15, 15);
lv_label_set_recolor(label1, true);
lv_label_set_align(label1, LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label1, "#008000 DEMARRAGE#");
lv_obj_set_width(label1, 150);

lv_obj_t * label2 = lv_label_create(tab1, NULL);
//lv_label_set_long_mode(label1, LV_LABEL_LONG_BREAK);
lv_obj_set_pos(label2, 15, 55);
lv_label_set_recolor(label2, true);
lv_label_set_align(label1, LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label2, "#008000 DELTA#");
lv_obj_set_width(label2, 150);



lv_obj_t * btn_13 = lv_btn_create(tab1, NULL);
lv_obj_set_pos(btn_13, 10, 220);
lv_obj_set_size(btn_13, 440, 20);
lv_obj_t * label_13 = lv_label_create(btn_13, NULL);
lv_label_set_text(label_13, "RETOUR");

lv_obj_t * ta_1 = lv_textarea_create(tab1, NULL);
lv_obj_set_pos(ta_1, 170, 10);
lv_obj_set_size(ta_1, 70, 1);
lv_textarea_set_accepted_chars(ta_1, "0123456789:");
lv_textarea_set_max_length(ta_1, 5);
lv_textarea_set_one_line(ta_1, true);
lv_textarea_set_text(ta_1, "");


lv_obj_t * ta_2 = lv_textarea_create(tab1, NULL);
lv_obj_set_pos(ta_2, 170, 50);
lv_obj_set_size(ta_2, 70, 1);
lv_textarea_set_accepted_chars(ta_2, "0123456789:");
lv_textarea_set_max_length(ta_2, 5);
lv_textarea_set_one_line(ta_2, true);
lv_textarea_set_text(ta_2, "");



lv_obj_t * fen_4 = lv_obj_create(tab1, NULL);
lv_obj_set_size(fen_4, 180, 190);
lv_obj_set_pos(fen_4,270, 10);


lv_obj_t * label6 = lv_label_create(fen_4, NULL);
lv_label_set_long_mode(label6, LV_LABEL_LONG_BREAK);
lv_obj_set_pos(label6, 10, 15);
lv_label_set_recolor(label6, true);
lv_label_set_align(label6, LV_LABEL_ALIGN_CENTER);
lv_label_set_text(label6, "#008000 DESCRIPTION DES#\n#008000     PARAMETRES##");
lv_obj_set_width(label6, 150);

lv_obj_t * btn_8 = lv_btn_create(fen_4, NULL);
lv_obj_set_pos(btn_8, 10, 50);
lv_obj_set_size(btn_8, 160, 15);
lv_obj_t * label_8 = lv_label_create(btn_8, NULL);
lv_label_set_text(label_8, "DEMARRAGE");

lv_obj_t * btn_9 = lv_btn_create(fen_4, NULL);
lv_obj_set_pos(btn_9, 10, 80);
lv_obj_set_size(btn_9, 160, 15);
lv_obj_t * label_9 = lv_label_create(btn_9, NULL);
lv_label_set_text(label_9, "DELTA");

#if WIN32
while(!lv_win_exit_flag) {
#else
while(1) {
#endif // WIN32
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
lv_task_handler();
usleep(1000); /Just to let the system breath/
}
return 0;
}

/**********************

  • STATIC FUNCTIONS
    **********************/

/**

  • Initialize the Hardware Abstraction Layer (HAL) for the Littlev graphics library
    /
    static void hal_init(void)
    {
    #if !WIN32
    /
    Add a display

    • Use the ‘monitor’ driver which creates window on PC’s monitor to simulate a display*/
      monitor_init();
      lv_disp_drv_t disp_drv;
      lv_disp_drv_init(&disp_drv); /Basic initialization/
      disp_drv.disp_flush = monitor_flush;
      disp_drv.disp_fill = monitor_fill;
      disp_drv.disp_map = monitor_map;
      lv_disp_drv_register(&disp_drv);

    /* Add the mouse (or touchpad) as input device

    • Use the ‘mouse’ driver which reads the PC’s mouse*/
      mouse_init();
      lv_indev_drv_t indev_drv;
      lv_indev_drv_init(&indev_drv); /Basic initialization/
      indev_drv.type = LV_INDEV_TYPE_POINTER;
      indev_drv.read = mouse_read; /This function will be called periodically (by the library) to get the mouse position and state/
      lv_indev_drv_register(&indev_drv);

    /* Tick init.

    • You have to call ‘lv_tick_handler()’ in every milliseconds
    • Create an SDL thread to do this*/
      SDL_CreateThread(tick_thread, “tick”, NULL);
      #else
      /* This sets up some timers to handle everything. /
      windrv_init();
      #endif
      }
      #if !WIN32
      /
      *
  • A task to measure the elapsed time for LittlevGL

  • @param data unused

  • @return never return
    */
    static int tick_thread(void *data)
    {
    while(1) {
    lv_tick_inc(1);
    SDL_Delay(1); /Sleep for 1 millisecond/
    }

    return 0;
    }
    #endif

You can show/hide objects using lv_obj_set_hidden. By doing this you can make the window appear when your event handler is called.

If you are looking for an example of how to use events, take a look at this documentation page.

Bonjour pouvez vous être plus précis, je suis un novice en programmation

First, create the window like you are creating the rest of your objects. Then, call lv_obj_set_hidden(window, false).

Take a look at the example I provided. That shows how to hook up an event callback to a button. In that event callback, you could then show the window using lv_obj_set_hidden(window, true).

Bonjour, de quel exemple parlez vous ?

https://docs.lvgl.io/latest/en/html/overview/event.html#events

Hello, I tried what you suggested to me. I manage to display the second window but the objects in it are not displayed. In other words, when I click on the button, I get an empty window. I would like to know how to make the objects that are in the window appear.

Voici l’exemple que j’ai essayé

int main(int argc, char** argv)
{
/Initialize LittlevGL/
lv_init();

/*Initialize the HAL for LittlevGL*/
hal_init();

/*Check the themes too*/
lv_disp_set_default(lv_windows_disp);


lv_obj_t * fen = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_size(fen, 480, 320);

lv_obj_t * btn = lv_btn_create(fen, NULL);
lv_obj_set_pos(btn, 50, 130);
lv_obj_set_size(btn, 390, 30);
lv_obj_t * label = lv_label_create(btn, NULL);
lv_label_set_text(label, “DEMARRER LE BALAYAGE”);
lv_obj_set_event_cb(btn, event_handler); /Assign an event callback/

}
return 0;
}

static void event_handler(lv_obj_t *obj, lv_event_t event)
{

if(event == LV_EVENT_CLICKED){
lv_obj_t * fen_trois = lv_win_create(NULL, NULL);
lv_obj_set_size(fen_trois, 480, 320);

lv_obj_t * btn_13 = lv_btn_create(fen_trois, NULL);
lv_obj_set_pos(btn_13, 10, 220);
lv_obj_set_size(btn_13, 440, 20);
lv_obj_t * label_13 = lv_label_create(btn_13, NULL);
lv_label_set_text(label_13, “RETOUR”);

lv_group_t * g = lv_group_create();
lv_group_add_obj(g, fen_trois);
lv_group_add_obj(g, btn_13);

lv_obj_set_hidden(fen_trois, true);
lv_scr_load ( fen_trois );

}

}