arduino (esp8266/esp32) 股票行情回调类成员函数

Dim*_*ris 3 c++ arduino

有人可以帮我解决以下代码吗?
我有一个自定义类,我想为该ticker函数定义一个回调onTickerCallback()
它可以在 ESP8266 上编译并运行,但不能在 ESP32 上编译和运行。
我看到 ESP32Ticker::once有不同的声明,但我的 C++ 知识无法帮助我找到解决方案。

测试.h

class Test {
  public:
    void start();
    void doSomething();
  private:
    void onTickerCallback();
};
Run Code Online (Sandbox Code Playgroud)

测试.cpp

#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void Test::start(){
  ticker.once(5, std::bind(&Test::onTickerCallback, this) );
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}
Run Code Online (Sandbox Code Playgroud)

主程序

#include <Arduino.h>
#include <Test.h>

Test t;

void setup() {
  Serial.begin(115200);
  t.start();
}

void loop() {
}
Run Code Online (Sandbox Code Playgroud)

在 ESP32 上我收到以下错误:

error: no matching function for call to 'Ticker::once(int, std::_Bind_helper<false, void (Test::*)(), Test*>::type)'  
note: candidate: void Ticker::once(float, Ticker::callback_t)
   void once(float seconds, callback_t callback)  
note:   no known conversion for argument 2 from 'std::_Bind_helper<false, void (Test::*)(), Test*>::type {aka std::_Bind<std::_Mem_fn<void (Test::*)()>(Test*)>}' to 'Ticker::callback_t {aka 
void (*)()}'  
note: candidate: template<class TArg> void Ticker::once(float, void (*)(TArg), TArg)
   void once(float seconds, void (*callback)(TArg), TArg arg)  
note:   mismatched types 'void (*)(TArg)' and 'std::_Bind<std::_Mem_fn<void (Test::*)()>(Test*)>'  
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 7

两个 Ticker.h 文件的实现有点不同。在 ESP8266 上,once 方法需要类型“callback_function_t”,其中callback_function_t 定义为:

typedef std::function<void(void)> callback_function_t;
Run Code Online (Sandbox Code Playgroud)

在 ESP32 上,它需要类型“callback_t”,其定义为:

typedef void (*callback_t)(void);
Run Code Online (Sandbox Code Playgroud)

在您的代码示例中, std::bind 提供了预期的 std::function 类型。ESP32 Ticker.h 的情况并非如此,它需要一个函数指针。您有两个选择:

  1. 无需将 onTickerCallback 函数作为 Test 类的一部分,只需为回调创建一个自由函数即可。(请注意,只有当回调不需要成为 Test 类的一部分时,这才是可接受的)。
#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void callbackFunction() {
  Serial.println("Hello");
}

void Test::start(){
  ticker.once(5, callbackFunction);
}
Run Code Online (Sandbox Code Playgroud)
  1. 创建一个带有 test 实例的自由函数,并使用它来调用您的函数。(请注意,这还要求 onTickerCallback 是公开的,我能想到没有真正的解决方法)。
#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void callbackFunc(Test* testInstance) {
  testInstance->onTickerCallback();
}

void Test::start(){
  ticker.once(5, callbackFunc, this);
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}
Run Code Online (Sandbox Code Playgroud)

额外奖励:想一想之后,您可以使用 lambda 而不是创建函数(请注意,您需要在 lambda 之前有一个 + 号才能使其充当函数指针)。这看起来像:

#include <Arduino.h>
#include <Test.h>
#include <functional>

// for ESP8266: https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/src/Ticker.h
// for ESP32:   https://github.com/espressif/arduino-esp32/blob/master/libraries/Ticker/src/Ticker.h
#include <Ticker.h>

Ticker ticker;

void Test::start(){
  ticker.once(5, +[](Test* testInstance) { testInstance->onTickerCallback(); }, this);
}

void Test::onTickerCallback() {
  doSomething();
}

void Test::doSomething() {
  Serial.println("Hello");
}
Run Code Online (Sandbox Code Playgroud)