Lv_event_get_user_data - pass and receive a string from "lv_obj_add_event_cb"

Hi guys

Parameters:
LVGL 8.3.10
Arduino IDE 2.2.1 Linux
ESP32

I’m trying to pass a string information from “lv_obj_add_event_cb” and to receive the same value inside “lv_event_get_user_data” but it’s bringing me an invalid character value.
image

First function:

char * wifiName;
wifiName = "abc";
Serial.print("I'll send: ");
Serial.println(wifiName);
network = lv_list_add_btn(list1, LV_SYMBOL_WIFI, wifiNameA.c_str());
lv_obj_add_event_cb(network, list_event_handler, LV_EVENT_CLICKED, &wifiName);

image

Second function:

static void list_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);

   char * userdata = (char*)lv_event_get_user_data(e);
   
   // char * vai = (char*)lv_obj_get_user_data(e);
    Serial.print("I recieved: ");
    Serial.println((char*)lv_event_get_user_data(e));
    Serial.println((char*)userdata);

    if(code == LV_EVENT_CLICKED) {
        lv_example_textarea_2();
    }
}

image

What I’m doing wrong here?

Thank so much

Best Regards

Sho code where and how you declare wifiName

HI @mmar22

I updated the first post…

Thank so much

is complete bad in C maybe in C++ ok assign cont string, but as local cant be used in cb func as user data. Create it global and for safe

char wifiname[10];

Hi @Marian_M
Thank so much… Almost solved. I can pass the information, but only works for last item of list.
Can you please help me to understand my fault?


static void list_event_handler(lv_event_t * e)
{
  lv_event_code_t code = lv_event_get_code(e);
  lv_obj_t * obj = lv_event_get_target(e);

  const char *userdata = (char*)lv_event_get_user_data(e);

  Serial.print("I recieved: ");
  Serial.println(userdata);

  if(code == LV_EVENT_CLICKED) {
      lv_example_textarea_2();
  }
}

void scan_wifi(){

  lv_obj_t * list1;
  list1 = lv_list_create(tab3);
  lv_obj_set_size(list1, 180, 220);
  lv_obj_center(list1);

  String wifiNameAndSignal = "";
  char *wifiName[0];

  int n = WiFi.scanNetworks();
  Serial.println("scan done");

  if (n == 0) {
      Serial.println("no networks found");
      lv_list_add_text(list1, "No Networks found :(");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    lv_list_add_text(list1, "Networks found:");

    for (int i = 0; i < n; ++i) {

      *wifiName = {};
      wifiNameAndSignal = WiFi.SSID(i) + " (" + WiFi.RSSI(i) + " db)";

      String wifiNameOnly = WiFi.SSID(i);
      *wifiName = (char*)wifiNameOnly.c_str();
      Serial.print("I'll send: ");
      Serial.println(*wifiName);

      network = lv_list_add_btn(list1, LV_SYMBOL_WIFI, wifiNameAndSignal.c_str());
      lv_obj_add_event_cb(network, list_event_handler, LV_EVENT_CLICKED, *wifiName);

      String LabelItem = (String)(i+1) + ": " + WiFi.SSID(i) + " (" + WiFi.RSSI(i) + "db)";
             LabelItem += (WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*";
      Serial.println(LabelItem);
      Serial.println("-------------------------------------------------------------------------------");
      delay(10);
    }
  }
  Serial.println("");
}

The Serial Monitor info:

scan done
11 Networks found
I'll send: NET0001
1: NET0001 (-52db)*
-------------------------------------------------------------------------------
I'll send: NET0002
2: NET0002 (-56db)*
-------------------------------------------------------------------------------
I'll send: NET0003
3: NET0003 (-57db)*
-------------------------------------------------------------------------------
I'll send: NET0004
4: NET0004 (-59db)*
-------------------------------------------------------------------------------
I'll send: NET0005
5: NET0005 (-61db)*
-------------------------------------------------------------------------------
I'll send: NET0006
6: NET0006 (-75db)*
-------------------------------------------------------------------------------
I'll send: NET0007
7: NET0007 (-76db)*
-------------------------------------------------------------------------------
I'll send: NET0008
8: NET0008 (-83db)*
-------------------------------------------------------------------------------
I'll send: NET0009
9: NET0009 (-84db)*
-------------------------------------------------------------------------------
I'll send: NET00010
10: NET00010 (-84db)*
-------------------------------------------------------------------------------
I'll send: DIRECT-8B-HP ENVY 7640 series
11: DIRECT-8B-HP ENVY 7640 series (-87db)*
-------------------------------------------------------------------------------

I recieved: ����������?$9�?\��?
I recieved: ����������?$9�?\��?
I recieved: DIRECT-8B-HP ENVY 7640 series
I recieved: ����������?$9�?\��?

Thank so much

Best Regards

Still you mix chaos of C++ and C. But primary mistake is you create one wifiname pointer and many buttons with callbacks to one name, then result is last one name for all clicks.

THank so much

I 'll try to fix the languages here.

Best Regards

and you still place this inside func = local variable , that stop exist on func end. Then you cant use it in cb userdata.