将示例代码重构为 class 不会引发重载函数的实例

Refactoring sample code into class raises no instance of overloaded function

我是 CPP 的新手,并尝试将此示例代码 https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WPS/WPS.ino "refactor" 转换为名为 ApiClient` 的 class。然后我想能够做这样的事情:

apiClient = ApiClient(myUrl);
apiClient.sendValue(key, value);

wifi.onEvent(WiFiEvent); 函数调用外,所有内容均可编译。 当我将整个示例代码复制粘贴到我的 main.cpp 文件中时,该示例正在运行。当我使用 "refactored" 方法时,wifi.onEvent(WifiEvent) 正在抱怨。

确切的错误信息:

no instance of overloaded function "WiFiClass::onEvent" matches the argument list -- argument types are: (void (system_event_id_t event, system_event_info_t info)) -- object type is: WiFiClass

我知道 onEvent 函数通常有两个参数,但为什么这在示例代码中有效?我该如何解决?

这是我目前拥有的:

main.cpp



#include "WiFi.h"
#include <esp_wps.h>
#include <Hythe22.h>
#include <ApiClient.h>

#define DHTPIN 14
// ?accessKey=ist_NJu3tjPIBCYeJd6DGGBxzq14LvungHoK&bucketKey=B37GHBNK5HL3
#define API_URL "https://groker.init.st/api/events";

Hythe22 dht(DHTPIN);
ApiClient apiClient;
uint64_t chipid;
#define ESP_DEVICE_NAME String(chipid)


void setup()
{
  String apiUrl = "https://myApi.Endpoint.com/event";
  Serial.begin(115200);
  delay(100);
  Serial.println();
}

void loop()
{

  String temp = String(dht.temperature);
  Serial.println("TEMP:" + temp);
  apiClient.sendValue("temperature", temp);

  String hum = String(dht.humidity);
  Serial.println("HUM:" + hum);
  apiClient.sendValue("humidity", hum);
}

ApiClient.h

/*
===========================================================
*/

#ifndef WebClient_h
#define WebClient_h

#include <Arduino.h>
#include <WiFi.h>
#include "esp_wps.h"
#include <HTTPClient.h>

class ApiClient
{
  public:
    ApiClient(String apiUrl);
    void sendValue(String key, String value);
    void wpsInitConfig();
    void WiFiEvent(WiFiEvent_t event, system_event_info_t info);
    String wpspin2string(uint8_t a[]);
    String requestUrl;
    String _apiUrl;
    int chipid;

  private:
};

#endif

ApiClient.cpp


/*
===========================================================
*/

#include <Arduino.h>
#include <ApiClient.h>
#include <WiFi.h>
#include <esp_wps.h>
#include <HTTPClient.h>

int chipid;

#define ESP_WPS_MODE WPS_TYPE_PBC
#define ESP_MANUFACTURER "ESPRESSIF"
#define ESP_MODEL_NUMBER "ESP32"
#define ESP_MODEL_NAME "ESPRESSIF IOT"
#define ESP_DEVICE_NAME "ESP STATION"

String _apiUrl;
HTTPClient http;
String requestUrl;
WiFiClass wifi;

static esp_wps_config_t config;

ApiClient::ApiClient(String apiUrl)
{
    Serial.begin(115200);
    delay(10);

    Serial.println();

    wifi.onEvent(WiFiEvent);
    wifi.mode(WIFI_MODE_STA);

    Serial.println("Starting WPS");

    wpsInitConfig();
    esp_wifi_wps_enable(&config);
    esp_wifi_wps_start(0);
}

void sendValue(String key, String value)
{
    HTTPClient http;
    Serial.println("key:" + key);
    Serial.println("value:" + value);
    requestUrl = _apiUrl + "?" + key + "=" + value;
    // Serial.println(apiUrl);
    http.begin(requestUrl);

    int httpCode = http.GET();

    if (httpCode > 0)
    {
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);
        //file found at server --> on unsucessful connection code will be -1
        if (httpCode == HTTP_CODE_OK)
        {
            String payload = http.getString();
            Serial.println(payload);
        }
    }
    else
    {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
}

void ApiClient::wpsInitConfig()
{
    config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
    config.wps_type = ESP_WPS_MODE;
    strcpy(config.factory_info.manufacturer, ESP_MANUFACTURER);
    strcpy(config.factory_info.model_number, ESP_MODEL_NUMBER);
    strcpy(config.factory_info.model_name, ESP_MODEL_NAME);
    strcpy(config.factory_info.device_name, ESP_DEVICE_NAME);
}

String wpspin2string(uint8_t a[])
{
    char wps_pin[9];
    for (int i = 0; i < 8; i++)
    {
        wps_pin[i] = a[i];
    }
    wps_pin[8] = '[=14=]';
    return (String)wps_pin;
}

void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
  switch(event){
    case SYSTEM_EVENT_STA_START:
      Serial.println("Station Mode Started");
      break;
    case SYSTEM_EVENT_STA_GOT_IP:
      Serial.println("Connected to :" + String(WiFi.SSID()));
      Serial.print("Got IP: ");
      Serial.println(WiFi.localIP());
      break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
      Serial.println("Disconnected from station, attempting reconnection");
      WiFi.reconnect();
      break;
    case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
      Serial.println("WPS Successfull, stopping WPS and connecting to: " + String(WiFi.SSID()));
      esp_wifi_wps_disable();
      delay(10);
      WiFi.begin();
      break;
    case SYSTEM_EVENT_STA_WPS_ER_FAILED:
      Serial.println("WPS Failed, retrying");
      esp_wifi_wps_disable();
      esp_wifi_wps_enable(&config);
      esp_wifi_wps_start(0);
      break;
    case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
      Serial.println("WPS Timedout, retrying");
      esp_wifi_wps_disable();
      esp_wifi_wps_enable(&config);
      esp_wifi_wps_start(0);
      break;
    case SYSTEM_EVENT_STA_WPS_ER_PIN:
      Serial.println("WPS_PIN = " + wpspin2string(info.sta_er_pin.pin_code));
      break;
    default:
      break;
  }
}

提前致谢

我查看了 OP 问题中链接的 example

相关部分是

void setup(){
  // contents skipped
  WiFi.onEvent(WiFiEvent);
  // contents skipped
}

因此WiFiEvent是上面定义的自由函数:

void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
  switch(event){
    // some cases to handle various events
    default:
      break;
  }
}

OP 想将此事件处理程序重构到他的 class ApiClient:

class ApiClient
{
  public:
    ApiClient(String apiUrl);
    void sendValue(String key, String value);
    void wpsInitConfig();
    void WiFiEvent(WiFiEvent_t event, system_event_info_t info);
    String wpspin2string(uint8_t a[]);
    String requestUrl;
    String _apiUrl;
    int chipid;

  private:
};

本质区别是WiFiEvent()因此变成了成员函数,OP得到了报错

no instance of overloaded function "WiFiClass::onEvent" matches the argument list -- argument types are: (void (system_event_id_t event, system_event_info_t info)) -- object type is: WiFiClass

出于好奇,我在 github 项目中挖掘了一下,终于找到了 WiFiClass::onEvent() 的声明——它继承自 class WiFiGenericClass:

class WiFiGenericClass
{
  public:
    WiFiGenericClass();

    wifi_event_id_t onEvent(WiFiEventCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
    wifi_event_id_t onEvent(WiFiEventFuncCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
    wifi_event_id_t onEvent(WiFiEventSysCb cbEvent, system_event_id_t event = SYSTEM_EVENT_MAX);
    // a lot more - skipped
};

因此,实际上有3个onEvent()的声明有两个参数,因此每个的第2nd个参数都有一个默认参数。 (因此,示例中只有一个参数的调用 WiFi.onEvent(WiFiEvent); 是可以的。)

为了彻底解决这个问题,我查找了 WiFiEventCbWiFiEventFuncCbWiFiEventSysCb,并在 class WiFiGenericClass 上面的同一个头文件中找到了它们:

typedef void (*WiFiEventCb)(system_event_id_t event);
typedef std::function<void(system_event_id_t event, system_event_info_t info)> WiFiEventFuncCb;
typedef void (*WiFiEventSysCb)(system_event_t *event);

这就是三个 typedef 的意思:

  1. WiFiEventCb ... 指向一个(自由)函数的函数指针 returns void 并且有一个 system_event_id_t
  2. 类型的参数
  3. WiFiEventFuncCb ... std::function 任何东西的对象 returning void 并且有两个类型 system_event_id_tsystem_event_info_t 的参数
  4. WiFiEventSysCb ... 指向一个(自由)函数的函数指针 returns void 并且有一个 system_event_id_t*.[=115= 类型的参数]

显然,该示例使用了 2nd onEvent(),因为它是唯一一个具有两个参数的接受函数。

std::function 的支持非常好,因为它接受任何具有匹配签名的可调用对象:

  • 免费功能,
  • 函子,
  • lambda(实际上就是前者之一)。

因此,要使 ApiClient::WiFiEvent() 兼容,有两种选择:

  1. 声明ApiClient::WiFiEvent()为静态成员函数
  2. ApiClient::WiFiEvent() 与调用它的实例绑定。

第一个选项限制了 ApiClient::WiFiEvent() 的可用性,因为 static 成员函数在没有 resp 实例的情况下被调用。 class。缺点——成员函数中没有可用的实例(即禁止显式或隐式访问 this),这可能会或可能不会被接受。

第二个选项可以通过使用 lambda 作为适配器轻松实现。为此,必须更改 ApiClient::ApiClient() 中的事件处理程序注册:

//ERROR: wifi.onEvent(WiFiEvent);
//Instead:
wifi.onEvent(
  [this](WiFiEvent_t event, system_event_info_t info) {
    this->WiFiEvent(event, info);
  });

这有效地注册了一个具有接受签名的仿函数,该签名捕获 ApiClientthis,以便可以完成对成员函数实例的有效调用。 return 类型的 lambda 被隐式声明为 void 因为在 lambda 的主体中没有 return.

最后,我想提一下,在 lambda 表达式中捕获是一件必须小心完成的事情。如果 wifithis 长(即 ApiClient 的实例),那么它可能会在没有有效的 this 指针的情况下调用 ApiClient::WiFiEvent()

为了使其成为 "bullet-proof",ApiClient 的析构函数可以使用 onEvent() 编辑的 wifi_event_id_t 调用 removeEvent() . (为此应将其存储在 ApiClient 中。)