有人能帮我做下面的代码吗?
我有一个自定义类,我想为ticker函数onTickerCallback()定义一个回调。
它在ESP8266上编译和运行,但不在ESP32上运行。
我看到ESP32 Ticker::once有一个不同的声明,但是我的c++知识并不能帮助我找到解决方案。
Test.h
class Test {
public:
void start();
void doSomething();
private:
void onTickerCallback();
};Test.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");
}main.cpp
#include <Arduino.h>
#include <Test.h>
Test t;
void setup() {
Serial.begin(115200);
t.start();
}
void loop() {
}在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*)>' 发布于 2020-04-02 07:19:37
两个Ticker.h文件的实现有点不同。在ESP8266上,一次性方法期望输入" callback_function_t“,其中callback_function_t定义为:
typedef std::function<void(void)> callback_function_t;在ESP32上,它希望输入"callback_t“,定义为:
typedef void (*callback_t)(void);在您的代码示例中,std::bind提供了预期的std::函数类型。对于ESP32 Ticker.h则不是这样,它需要一个函数指针。你有两个选择:
#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);
}#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");
}奖励:在考虑了一下之后,您可以使用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");
}https://stackoverflow.com/questions/60985496
复制相似问题