Description
Hi @kisvegabor and @embeddedt
i am facing a small issue with “lv_obj_set_user_data”
i have created custom list using area.
when i click on each area i want to print particular data regarding that area.
the data passing to “lv_obj_set_user_data” is printing correctly when 1st click but on 2nd click data not printing.
for every area created with forloop printing correct data for first click. on 2nd click printing only small value.
i am attaching my sample code below.
i need your support to solve this issue.
Please help me with this issue. i don’t know where i did mistake.
Thanks in advance.
What MCU/Processor/Board and compiler are you using?
ARM
What LVGL version are you using?
v7
What do you want to achieve?
What have you tried so far?
Code to reproduce
Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.
The code block(s) should be formatted like:
void room_devices(char *arr_device_name[],char *arr_device_id[],char *arr_device_type[],char *arr_device_client[])
{
static lv_style_t lv_style_title;
lv_style_init(&lv_style_title);
lv_style_set_text_font(&lv_style_title, LV_STATE_DEFAULT, &lv_font_montserrat_24);
lv_style_set_text_color(&lv_style_title, LV_STATE_DEFAULT, LV_COLOR_WHITE);
static lv_style_t lv_style_text;
lv_style_init(&lv_style_text);
lv_style_set_text_font(&lv_style_text, LV_STATE_DEFAULT, &lv_font_montserrat_30);
lv_style_set_text_color(&lv_style_text, LV_STATE_DEFAULT, LV_COLOR_GRAY);
/*Line style*/
static lv_style_t style_line;
lv_style_init(&style_line);
lv_style_set_line_width(&style_line, LV_STATE_DEFAULT, 2);
lv_style_set_line_color(&style_line, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_style_set_line_rounded(&style_line, LV_STATE_DEFAULT, true);
//page style
static lv_style_t style_page;
lv_style_init(&style_page);
lv_style_set_bg_color(&style_page,LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_style_set_border_width(&style_page,LV_STATE_DEFAULT,0);
lv_style_set_text_color(&style_page,LV_STATE_DEFAULT,LV_COLOR_WHITE);
lv_style_set_text_font(&style_page, LV_STATE_DEFAULT, &lv_font_montserrat_24);
lv_style_set_line_width(&style_page,LV_STATE_DEFAULT,30);
//style_box
static lv_style_t style_box;
lv_style_init(&style_box);
lv_style_set_bg_color(&style_box,LV_STATE_DEFAULT,LV_COLOR_BLACK);
lv_style_set_pad_top(&style_box,LV_STATE_DEFAULT,30);
lv_style_set_pad_bottom(&style_box,LV_STATE_DEFAULT,30);
lv_style_set_pad_left(&style_box,LV_STATE_DEFAULT,30);
lv_style_set_pad_right(&style_box,LV_STATE_DEFAULT,30);
lv_style_set_radius(&style_box, LV_STATE_DEFAULT, 10);
lv_style_set_text_font(&style_box, LV_STATE_DEFAULT, &lv_font_montserrat_20);
lv_style_set_text_color(&style_box, LV_STATE_DEFAULT, LV_COLOR_WHITE);
/*create a page title*/
lv_obj_t * page_title;
page_title=lv_label_create(lv_scr_act(),NULL);
lv_label_set_text(page_title,"Devices");
lv_obj_add_style(page_title,LV_OBJ_PART_MAIN, &lv_style_title);
lv_obj_align(page_title,NULL,LV_ALIGN_IN_TOP_MID,0,20);
//style button//
lv_style_init(&style_button);
lv_style_set_bg_color(&style_button,LV_STATE_DEFAULT, LV_COLOR_CYAN);
lv_style_set_bg_opa(&style_button,LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_style_set_radius(&style_button,LV_STATE_DEFAULT,30);
//line
static lv_point_t line_points[] = {{0,0},{480,0}};
//line object//
lv_obj_t * line1;
line1 = lv_line_create(lv_scr_act(), NULL);
lv_line_set_points(line1, line_points, 2);
lv_obj_add_style(line1,LV_LINE_PART_MAIN, &style_line);
lv_obj_align(line1, page_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
//page
lv_obj_t * page = lv_page_create(lv_scr_act(), NULL);
//lv_obj_set_pos(page, 10, 10);
lv_obj_set_size(page, 480, 750);
lv_obj_add_style(page, LV_PAGE_PART_BG, &style_page);
lv_page_set_scrlbar_mode(page, LV_SCRLBAR_MODE_OFF );
lv_page_set_scrollable_fit2(page, LV_FIT_TIGHT, LV_FIT_TIGHT);
lv_obj_align(page, line1, LV_ALIGN_IN_TOP_MID, 0, 10);
for(int i=0;i<30;i++){
if(arr_device_name[i]!=NULL && arr_device_id[i]!=NULL && arr_device_type[i]!=NULL && arr_device_client[i]!=NULL){
char name[50];
char id[50];
char type[50];
char deviceclient[50];
strcpy(name,arr_device_name[i]);
strcpy(id,arr_device_id[i]);
strcpy(type,arr_device_type[i]);
strcpy(deviceclient,arr_device_client[i]);
char data[256];
snprintf(data,256,"%s-%s-%s-%s",name,id,type,deviceclient);
//creating array for passing string
char* str_device = malloc(strlen(data)+1);
strcpy(str_device, data);
char *array_device[] = {};
char * ( *ptr )[] = &array_device;
( *ptr )[i] = str_device;
// add an area
lv_obj_t * area = lv_obj_create(page, NULL);
lv_obj_set_user_data(area,array_device[i]);
lv_obj_set_size(area, 450, 150);
lv_obj_add_style(area, LV_OBJ_PART_MAIN, &style_box);
lv_obj_align(area, NULL, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
// adding stuff to area
//devicename
devicename = lv_label_create(area, NULL);
lv_label_set_recolor(devicename, true);
lv_label_set_text(devicename,arr_device_name[i]);
lv_obj_align(devicename,NULL,LV_ALIGN_IN_TOP_LEFT,10,10);
//devicetype
lv_obj_t * devicetype = lv_label_create(area, NULL);
lv_label_set_recolor(devicetype, true);
lv_label_set_text(devicetype,arr_device_type[i]);
lv_obj_align(devicetype,NULL,LV_ALIGN_IN_TOP_MID,0,10);
//client
lv_obj_t * client = lv_label_create(area, NULL);
lv_label_set_recolor(client, true);
lv_label_set_text(client,arr_device_client[i]);
lv_obj_align(client,NULL,LV_ALIGN_IN_TOP_RIGHT,-10,10);
//extra widget
if(strcmp(arr_device_type[i], "dimmer")==0){
//label
slider_label = lv_label_create(area, NULL);
lv_label_set_text(slider_label, "Level:58%");
lv_obj_set_auto_realign(slider_label, true);
lv_obj_add_style(slider_label,LV_OBJ_PART_MAIN, &lv_style_text);
lv_obj_align(slider_label, NULL, LV_ALIGN_CENTER, 0, 10);
}else if(strcmp(arr_device_type[i],"Switch")==0){
//label
switch_label = lv_label_create(area, NULL);
lv_label_set_text(switch_label, "State:Off");
lv_obj_set_auto_realign(switch_label, true);
lv_obj_add_style(switch_label,LV_OBJ_PART_MAIN, &lv_style_text);
lv_obj_align(switch_label, NULL, LV_ALIGN_CENTER, 0, 10);
}else if(strcmp(arr_device_type[i], "Thermostat")==0){
lv_obj_t * widget = lv_label_create(area, NULL);
lv_label_set_text(widget, "Temperature:20°C");
lv_label_set_recolor(widget, true);
lv_obj_add_style(widget,LV_OBJ_PART_MAIN, &lv_style_text);
lv_obj_align(widget,NULL,LV_ALIGN_CENTER,0,10);
}else if(strcmp(arr_device_type[i],"Smart plug")==0){
//label
smart_plug_label = lv_label_create(area, NULL);
lv_label_set_text(smart_plug_label, "State:Off");
lv_obj_set_auto_realign(smart_plug_label, true);
lv_obj_add_style(smart_plug_label,LV_OBJ_PART_MAIN, &lv_style_text);
lv_obj_align(smart_plug_label,NULL,LV_ALIGN_CENTER,0,10);
}
lv_obj_set_click(area, true);
lv_obj_set_drag_parent(area, true);
lv_obj_set_event_cb(area, page_event_handler);
}
}
}
static void page_event_handler(lv_obj_t * area, lv_event_t event)
{
if(event == LV_EVENT_CLICKED) {
char * user_data=lv_obj_get_user_data(area);
printf("user data from area:%s\n",user_data);
char * devicename_split = strtok(user_data, "-");
char * deviceid_split = strtok(NULL, "-");
char * devicetype_split = strtok(NULL, "-");
char * deviceclient_split = strtok(NULL, "-");
printf( "---str_split1:%s\n", devicename_split );
printf( "---str_split2:%s\n", deviceid_split );
printf( "---str_split3:%s\n", devicetype_split );
printf( "---str_split4:%s\n", deviceclient_split );
}
}
output
On 1st Click
user data from area:Living room-9-Switch-Philips
---str_split1:Living room
---str_split2:9
---str_split3:Switch
---str_split4:Philips
On 2nd Click(Got only Living Room)
user data from area:Living room
---str_split1:Living room
---str_split2:(null)
---str_split3:(null)
---str_split4:(null)
Screenshot and/or video
If possible, add screenshots and/or videos about the current state.