dawidchyrzynski / arduino-home-assistant

ArduinoHA allows to integrate an Arduino/ESP based device with Home Assistant using MQTT.
https://dawidchyrzynski.github.io/arduino-home-assistant/
GNU Affero General Public License v3.0
480 stars 116 forks source link

ESP32 Support for Method Pointers #143

Open ZanzyTHEbar opened 1 year ago

ZanzyTHEbar commented 1 year ago

Hello, i am using this library, and i am having to re-write the call backs to support std::function on the ESP devices.

In my project, i have created a HASSMQTT class for handling all of my HAMqtt logic. Normally, i would use a lambda or std::bind to a callback which has a std::function based type.

I would really like this functionality - but i can't in the default library as you only except function pointers, and not method pointers (on supported boards).

I would recommend doing something like:

#ifdef ESP32
#include <functional>
#define HASWITCH_CALLBACK(name) std::function<void(bool state, HASwitch* s)> name
#else
#ifdef ESP8266
#include <functional>
#define HASWITCH_CALLBACK(name) std::function<void(bool state, HASwitch* s)> name
#endif
#define HASWITCH_CALLBACK(name) void (*name)(bool, HASwitch*)
#endif

Now, of course the std::function is available for all 32bit boards, so adding support for all 32bit boards using a single preprocessor macro is preferred.