-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Calback functions (or Rotary object) should have a void* argument #48
Comments
Hey, Cheers |
Hi. I forked the project to make that happen in my code. I would be more than happy to share it once I know it works, and even more happy if you could find some value. Please note that I never contributed actively to open source projects so far, so I have a vague idea on how this sharing can be done. In the end, if possible, I’d prefer get this in your project than have a separate fork. This seems more productive for the community. Happy to help. —Christophe Sent from my iPadOn Jan 8, 2024, at 11:56 AM, Lennart Hennigs ***@***.***> wrote:
Hey,
thank you for your comment. I will take a look at your suggestion.
Cheers
l.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
@homedatadev I looked at your fork - but your changes don't seem to have made it into your fork of the repo. Can you push your changes back to your fork, so we can see what you've done? I agree that the ability to pass a (void *) context object would be useful and more in line with C design patterns. Is it just something like this (purposefully incomplete here...): /* in the public member declaration... */
using CallbackFunction = void (*)(ESPRotary&, void*);
void setChangedHandler(CallbackFunction f, void *context);
void setRightRotationHandler(CallbackFunction f, void *context);
void setLeftRotationHandler(CallbackFunction f, void *context); And we have to retain the context: void setChangedHandler(CallbackFunction f, void* context) {
change_cb = f;
change_cb_context = context;
} Then in the implementation of /* just for example... */
void ESPRotary::_callCallback(CallbackFunction callback, void* context) {
if (callback != NULL) callback(*this, context);
} And changes to you the callback itself: typedef struct {
uint16_t foo;
uint8_t bar;
} example_context_t;
void rotate_callback(ESPRotary& r, void* context) {
// Cast the context pointer back to the appropriate type
example_context_t *data = static_cast<example_context_t *>(context);
ESP_LOGI("my_module", "foo from context = %d", data→foo);
}
// Setting the callback with the context
example_context_t ctx;
ctx.foo = 65535;
ctx.bar = 127;
ro.setChangedHandler(rotate_callback, &ctx); etc. Happy to fork it too and send a PR, too. |
Most callback functions take a void* argument to provide some custom context. When you cannot do some lambda/closure functions like in Python, this is the way to work around it.
In my case, it would allow me to pass the pointer of the object that owns the Rotary object (and other things)
The text was updated successfully, but these errors were encountered: