LennartHennigs / Button2

Arduino/ESP button library that provides callback functions to track single, double, triple and long clicks. It also takes care of debouncing.
MIT License
487 stars 81 forks source link

Use std::function to allow C++ 11 lambda functions #29

Closed jacobdekeizer closed 3 years ago

jacobdekeizer commented 3 years ago

Hi,

Thank you for making this library. I needed to access the this context from a callback in my project, so that I could update a counter in my class. Having the CallbackFunction use std::function, makes it possible to use the C++11 lambda functions.

My example:

void DevicesScreenActivity::begin()
{
  this->_inputManager->left.setReleasedHandler([&](Button2 &b) {
    Serial.println("test");

    this->selected++;
  });
}
LennartHennigs commented 3 years ago

Hey, thanks for pointing this out and adding this. Will merge it!

ItsFlo commented 3 years ago

This breaks compiling for Arduino for me. Compiling for ESP32 and ESP8266 both worked. I also tried a clean install of the Arduino IDE, only downloaded this library and only added #include "Button2.h" to an empty sketch and it didn´t compile:

fatal error: functional: No such file or directory

I tried to compile for both Uno and Nano.

@jacobdekeizer are you using an Arduino for your project or something else?

jacobdekeizer commented 3 years ago

Hi, I am using an esp32. I didn't know the arduino uno does not support the c++ standard library. I found this example so we can target both esp32/esp8266 and arduino: https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkConsole.h#L31.

LennartHennigs commented 3 years ago

Hey, I add pre-processor directives to detect ESP8266 & EPS32s and pushed a new released. The code now compiles on Arduino and these two platforms. I will keep it now as it is and maybe revisit it later to find a better solution.

Thanks @ItsFlo for finding the error and thanks @jacobdekeizer for suggesting a fix. Cheers