首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >arduino (esp32 8266/esp32 32)代码机回调类成员函数

arduino (esp32 8266/esp32 32)代码机回调类成员函数
EN

Stack Overflow用户
提问于 2020-04-02 06:34:11
回答 1查看 3K关注 0票数 3

有人能帮我做下面的代码吗?

我有一个自定义类,我想为ticker函数onTickerCallback()定义一个回调。

它在ESP8266上编译和运行,但不在ESP32上运行。

我看到ESP32 Ticker::once有一个不同的声明,但是我的c++知识并不能帮助我找到解决方案。

Test.h

代码语言:javascript
复制
class Test {
  public:
    void start();
    void doSomething();
  private:
    void onTickerCallback();
};

Test.cpp

代码语言:javascript
复制
#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

代码语言:javascript
复制
#include <Arduino.h>
#include <Test.h>

Test t;

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

void loop() {
}

在ESP32上,我得到以下错误:

代码语言:javascript
复制
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*)>'  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-02 07:19:37

两个Ticker.h文件的实现有点不同。在ESP8266上,一次性方法期望输入" callback_function_t“,其中callback_function_t定义为:

代码语言:javascript
复制
typedef std::function<void(void)> callback_function_t;

在ESP32上,它希望输入"callback_t“,定义为:

代码语言:javascript
复制
typedef void (*callback_t)(void);

在您的代码示例中,std::bind提供了预期的std::函数类型。对于ESP32 Ticker.h则不是这样,它需要一个函数指针。你有两个选择:

  1. 不是测试类的onTickerCallback函数部分,而是为回调创建一个空闲函数。(请注意,只有当回调不需要成为Test类的一部分时,这才是可以接受的)。

代码语言:javascript
复制
#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);
}

  1. 创建一个接受测试实例的免费函数,并使用它调用函数。(请注意,这还需要onTickerCallback公开,我认为没有真正的方法)。

代码语言:javascript
复制
#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之前有一个+符号,以便它作为函数指针工作)。这看起来应该是:

代码语言:javascript
复制
#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");
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60985496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档