Aircoookie / Espalexa

Alexa voice control for ESP8266/ESP32 (including brightness and color!)
MIT License
545 stars 137 forks source link

Not a real issue: unique callback function #211

Open Socket2000 opened 1 year ago

Socket2000 commented 1 year ago

One annoying thing is having to define different callback functions for each device. If we could also pass the name of the device in the function,we can have only one function and it will be up to the programmer to search for the device to be controlled based on the past name. Each device can direct to the same routine.

for example only for brightness devices:

In espalexadevice.h> typedef std :: function <void (uint8_t b, String dName)> BrightnessCallbackFunction;

in espalexadecive.cpp> void EspalexaDevice :: doCallback () { if (_callback! = nullptr) {_callback (_val, _deviceName); return;}

Aircoookie commented 1 year ago

Hey, this should already be possible. Just pass the same callback function when initializing each device, e.g.

espalexa.addDevice("Alpha", alphaChanged, EspalexaDeviceType::onoff);
espalexa.addDevice("Beta", alphaChanged, EspalexaDeviceType::dimmable, 127);

Callback functions have EspalexaDevice* as a parameter, that pointer refers to the device that caused the callback. You can then identify the device via d->getName() or d->getId()

Socket2000 commented 1 year ago

Yup!