Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not a real issue: unique callback function #211

Open
Socket2000 opened this issue Nov 27, 2022 · 2 comments
Open

Not a real issue: unique callback function #211

Socket2000 opened this issue Nov 27, 2022 · 2 comments

Comments

@Socket2000
Copy link

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
Copy link
Owner

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
Copy link
Author

Yup!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants