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

时间:2018-10-31 20:08:49

标签: c++ c++11 arduino

我正在用C ++为ESP8266编写一个小程序,遇到了麻烦。 我创建了一个Led类表单处理LED。想法是该类应处理眨眼函数。为此,我使用了一个名为Ticker的库。

Ticker中的一个函数,attach_ms需要回调,而我无法使它与非静态成员函数一起工作。

这是我的头文件:

#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

这是我的代码文件:

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

}

此行给出了编译错误,我不知道如何解决。尝试遵循我在互联网上找到的所有“建议”。

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

1 个答案:

答案 0 :(得分:2)

根据this version of 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);
}

在第一种情况下,您可以将lambda与std::function一起使用,根本不需要std::bind()

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

在第二种情况下,callback接受一个用户定义的参数,该参数传递给Ticker::attach_ms(),因此您可以将this作为该参数传递(如上所示) ,正是std::function的{​​{1}}版本在内部所做的操作):

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); 不允许使用大于4个字节的回调参数,这意味着在为指针为8个字节的64位进行编译时,这两种方法都不起作用!恕我直言,这似乎是内部Ticker::attach_ms()方法实现中的错误,该方法将回调参数作为Ticker::_attach_ms()而不是uint32_t

uintptr_t