c++ - 将示例代码重构到类中不会引发重载函数的实例

标签 c++ arduino esp32

我是 CPP 的新手并尝试“重构”此示例代码 https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WPS/WPS.ino进入一个名为 的类 ApiClient`。然后我想能够做这样的事情:

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

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

确切的错误信息:

没有重载函数“WiFiClass::onEvent”的实例匹配参数列表——参数类型是:(void (system_event_id_t event, system_event_info_t info))——对象类型是: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] = '\0';
    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;
  }
}

提前致谢

最佳答案

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

相关部分是

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
};

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

为了彻底解决这个问题,我查找了WiFiEventCb。 , WiFiEventFuncCb , 和 WiFiEventSysCb并在 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 ... 指向返回 void 的(自由)函数的函数指针并且有一个类型为 system_event_id_t 的参数
  2. WiFiEventFuncCb ...一个std::function任何返回的对象void并且有两个类型的参数 system_event_id_tsystem_event_info_t
  3. WiFiEventSysCb ... 指向返回 void 的(自由)函数的函数指针并且有一个类型为 system_event_id_t* 的参数.

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

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

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

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

  1. 声明ApiClient::WiFiEvent()作为静态成员函数
  2. 绑定(bind)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);
  });

这有效地注册了一个具有捕获 this 的已接受签名的仿函数的 ApiClient这样就可以完成对带有实例的成员函数的有效调用。 lambda 的返回类型隐式声明为 void因为没有 return在 lambda 的主体中。

最后,我想提一下,在 lambda 中进行捕获是一件必须谨慎完成的事情。如果wifi超过生命周期this (即 ApiClient 的实例)然后它可以调用 ApiClient::WiFiEvent()没有有效的 this -指针。

为了使其“防弹”,ApiClient 的析构函数可以调用removeEvent()使用 wifi_event_id_t onEvent() 返回. (为此应将其存储在 ApiClient 中。)

关于c++ - 将示例代码重构到类中不会引发重载函数的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54744985/

相关文章:

由单独源文件中的全局变量引起的 C++ 段错误

c++ - 为什么我重载的 C++ 构造函数没有被调用?

c - 移动机器人设置步数(Arduino)

arduino - 使用attachInterrupt时没有匹配函数错误

arduino - 在 ESP32 sketch 中隐藏 ssid 密码的选项

c++ - 聚合初始化,clang 要我破坏我的代码?

c++ - 获取花在进程上的 CPU 时间与花在进程问题上的世界时间 C++

embedded - 第一次使用嵌入式系统程序员使用哪个微 Controller 重要吗?

c - 如何使用 esp_bt_gap_read_rssi_delta 函数从 ESP32 获取蓝牙经典 RSSI?

visual-studio-code - 从平台IO上传代码到esp32