Passing in additional parameters to Event Handler functions

Chucky101
Posts: 2
Joined: Sun Jun 19, 2022 12:48 am

Passing in additional parameters to Event Handler functions

Postby Chucky101 » Sun Jun 19, 2022 12:57 am

I am trying to get access to a class member variable to expose whether the class is connected to Wifi or not.

I am currently registering the handler as shown below with the assumption that "this" will get passed into the handler in place of "event_data".
  1.  
  2. esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, WifiEventHandler, this);
  3.  
When when this code runs it crashed and causes a LoadProhibited error.
  1.  
  2. void WifiModule::WifiEventHandler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
  3. {
  4.     auto ThisWifiModuleReference = (WifiModule*)(event_data);
  5.  
  6.     std::cout << ThisWifiModuleReference->m_sSSID << std::endl;
  7.  
  8.     switch (event_id)
  9.     {
  10.     case WIFI_EVENT_STA_START:
  11.         printf("WiFi connecting ... \n");
  12.         //ThisWifiModuleReference->m_bWifiConnected = false;
  13.         break;
  14.     case WIFI_EVENT_STA_CONNECTED:
  15.         printf("WiFi connected ... \n");
  16.         //ThisWifiModuleReference->m_bWifiConnected = false;
  17.         break;
  18.     case WIFI_EVENT_STA_DISCONNECTED:
  19.         printf("WiFi lost connection ... \n");
  20.         //ThisWifiModuleReference->m_bWifiConnected = false;
  21.         break;
  22.     case IP_EVENT_STA_GOT_IP:
  23.         printf("WiFi got IP ... \n\n");
  24.         //ThisWifiModuleReference->m_bWifiConnected = true;
  25.         break;
  26.     default:
  27.         break;
  28.     }
  29. }
  30.  
Still quite new to embedded programming so I would really appreciate an explanation as to why this would happen/how to fix it.
Last edited by Chucky101 on Wed Nov 09, 2022 10:17 pm, edited 1 time in total.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: Passing in additional parameters to Even Handler functions

Postby boarchuz » Tue Jun 21, 2022 4:30 pm

Close, but 'event_handler_arg' is your "this" pointer in this case, not 'event_data'.

Who is online

Users browsing this forum: No registered users and 104 guests