bxparks / AceButton

An adjustable, compact, event-driven button library for Arduino that debounces and dispatches events to a user-defined event handler.
MIT License
385 stars 37 forks source link

invalid use of non-static member function 'void Interface::handleEvent(ace_button::AceButton*, uint8_t, uint8_t)' #122

Closed Sercurio closed 7 months ago

Sercurio commented 7 months ago

Hello,

I try to instantiate a ButtonConfig in a member class function but I get the following error.

It is possible to do this pattern ?

class Test {
    private:
        AceButton button1 = AceButton(1);
        void configure();
        void handleEvent(AceButton *, uint8_t, uint8_t);
};
#include <Test.h>

void Test::configure(){
     pinMode(1, INPUT_PULLUP);
     ButtonConfig *buttonConfig = ButtonConfig::getSystemButtonConfig();
     buttonConfig->setEventHandler(handleEvent);
     buttonConfig->setFeature(ButtonConfig::kFeatureClick);
     buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
     buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
}

void Test::handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState){
     switch (button->getPin())
    {
    case 1:
        Serial.println("Hourrray");
        break;
    }
}

Thank you

bxparks commented 7 months ago

Try declaring your Test::handleEvent() as static. The AceButton::setEventHandler() expects a pointer to a plain function, not a pointer to a member function.

Sercurio commented 7 months ago

Yes I tried, but the problem is that I use member variable in my eventHandler function like that:

class Test {
    private:
        AceButton button1 = AceButton(1);
        int counter = 0;
        void configure();
        void handleEvent(AceButton *, uint8_t, uint8_t);
};
#include <Test.h>

void Test::configure(){
     pinMode(1, INPUT_PULLUP);
     ButtonConfig *buttonConfig = ButtonConfig::getSystemButtonConfig();
     buttonConfig->setEventHandler(handleEvent);
     buttonConfig->setFeature(ButtonConfig::kFeatureClick);
     buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
     buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
     buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
}

void Test::handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState){
     switch (button->getPin())
    {
    case 1:
        counter+=1;
        break;
    }
}