简体   繁体   English

如何在C++中使用非静态成员函数作为回调

[英]How to use non static member functions as callback in C++

I am writing a small program for ESP8266 in C++ and run into trouble.我正在用 C++ 为 ESP8266 编写一个小程序并遇到了麻烦。 I've created a Led class form handling leds.我创建了一个处理 LED 的 Led 类表单。 The idea is that the class should handle a blink function.这个想法是该类应该处理一个闪烁功能。 For this I use a library called Ticker.为此,我使用了一个名为 Ticker 的库。

A function in Ticker, attach_ms requires a callback and I cant get that to work with a non static member functions. Ticker 中的一个函数 attach_ms 需要一个回调,我无法让它与非静态成员函数一起使用。

This is my header file:这是我的头文件:

#ifndef led_h
#define led_h

#include <Arduino.h> 
#include <Ticker.h>
#include "debugutils.h"

#define tickLength 100


enum class LedState {
        OFF,
        ON,
        SLOW_BLINK,
        FAST_BLINK
};


class Led {
public:

    Led(Ticker *tick, uint8_t ledPin, int slowBlinkTime, int fastBlinkTime);

    void on();
    void off();
    void slowBlink( );
    void fastBlink( );

private:
    uint8_t pin;
    int counter;
    int slowNoBlinkTicks;
    int fastNoBlinkTicks;
    LedState state;
    void ledOn();
    void ledOff();
    void ledInvert();
    void clean();
    void blink(int par);
    void tickerCallbackLed();
};
#endif

This is my code file:这是我的代码文件:

#include "led.h"


void Led::ledOn() {
    digitalWrite(pin, HIGH);
}

void Led::ledOff() {
     digitalWrite(pin, LOW);
}

void Led::ledInvert() {
    digitalWrite(pin, !digitalRead(pin));
}

void Led::clean() {
    counter = 0;
}

void Led::blink(int par) {
    if (counter > par) {
        ledInvert();
        counter = 0;
    }
    else {
        counter++;
    }
}

void Led::tickerCallbackLed() {

    switch (state) {
        case LedState::OFF : break;
        case LedState::ON : break;
        case LedState::SLOW_BLINK : blink(slowNoBlinkTicks); break;
        case LedState::FAST_BLINK : blink (fastNoBlinkTicks);  break;
        default : break;
    };


};

void Led::on() {
    ledOn();
    state = LedState::ON;
};


void Led::off(){
    ledOff();
    state = LedState::OFF;
};

void Led::slowBlink(){
    clean();
    ledInvert();
    state = LedState::SLOW_BLINK;
};

void Led::fastBlink(){
    clean();
    ledInvert();
    state = LedState::FAST_BLINK;
};


Led::Led(Ticker *tick, uint8_t ledPin, int slowBlinkTime, int fastBlinkTime) {

    tick->attach_ms(tickLength, std::bind(&Led::tickerCallbackLed,this));

    slowNoBlinkTicks = slowBlinkTime/tickLength;
    fastNoBlinkTicks = fastBlinkTime/tickLength;

    pinMode(ledPin,OUTPUT);

    digitalWrite(ledPin,LOW);

    pin = ledPin;
    state = LedState::OFF;
    counter = 0;

}

This line gives a compile error and I dont know how to fix it.这一行给出了一个编译错误,我不知道如何修复它。 Have tried to follow all "advice" I have found on internet.尝试遵循我在互联网上找到的所有“建议”。

 tick->attach_ms(tickLength, std::bind(&Led::tickerCallbackLed,this));

According to this version of Ticker.h , Ticker::attach_ms() is overloaded to accept either a std::function<void(void)> or a void (*)(TArg) as the callback:根据此版本的Ticker.hTicker::attach_ms()被重载以接受std::function<void(void)>void (*)(TArg)作为回调:

typedef void (*callback_with_arg_t)(void*);
typedef std::function<void(void)> callback_function_t;

void attach_ms(uint32_t milliseconds, callback_function_t callback)
{
        _callback_function = callback;
        attach_ms(milliseconds, _static_callback, (void*)this);
}

template<typename TArg>
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
        static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
        uint32_t arg32 = (uint32_t)arg;
        _attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}

In the first case, you can use a lambda with std::function , you don't need std::bind() at all:在第一种情况下,您可以将 lambda 与std::function ,您根本不需要std::bind()

tick->attach_ms(tickLength, [this](){ this->tickerCallbackLed(); });

In the second case, the callback takes a user-defined argument that is passed to Ticker::attach_ms() , so you can pass this as that argument (which, as you can see above, is exactly what the std::function version of Ticker::attach_ms() does internally):在第二种情况下, callback采用传递给Ticker::attach_ms()的用户定义参数,因此您可以将this作为该参数传递(正如您在上面看到的,这正是std::function版本Ticker::attach_ms()在内部执行):

class Led {
...
private:
    ...
    static void staticTickerCallbackLed(Led *pThis);
    void tickerCallbackLed();
    ...
};

void Led::staticTickerCallbackLed(Led *pThis)
{
    pThis->TickerCallbackLed();
}

...

tick->attach_ms(tickLength, &Led::staticTickerCallbackLed, this);

Do note, though, that Ticker::attach_ms() does not allow a callback argument that is > 4 bytes in size, which means either approach will not work when compiling for 64-bit, where pointers are 8 bytes!但是请注意, Ticker::attach_ms()不允许回调参数大于 4 个字节,这意味着在编译 64 位时,这两种方法都不起作用,其中指针为 8 个字节! IMHO, that seems like a bug in the implementation of the internal Ticker::_attach_ms() method, which takes in a callback argument as uint32_t instead of as uintptr_t :恕我直言,这似乎是内部Ticker::_attach_ms()方法实现中的一个错误,该方法将回调参数作为uint32_t而不是uintptr_t

void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM