From 9d9a1656934a6a444d6f32e197844e541e46942e Mon Sep 17 00:00:00 2001 From: Martin Mueller Date: Sat, 25 Jun 2022 10:25:05 -0400 Subject: [PATCH] Added an example using the button shortcut --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e438b7..9e5799e 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ The heart of ESPUI is [ESPAsyncWebserver](https://github.com/me-no-dev/ESPAsyncW

This section will explain in detail how the Library is to be used from the Arduino code side. In the arduino `setup()` routine the interface can be customised by adding UI Elements. This is done by calling the corresponding library methods on the Library object `ESPUI`. Eg: `ESPUI.button("button", &myCallback);` creates a button in the interface that calls the `myCallback(Control *sender, int eventname)` function when changed. All buttons and items call their callback whenever there is a state change from them. This means the button will call the callback when it is pressed and also again when it is released. To separate different events, an integer number with the event name is passed to the callback function that can be handled in a `switch(){}case{}` statement.

-Alternativly you may use the extended callback funtion which provides three parameters to the callback function `myCallback(Control *sender, int eventname, void * UserParameter)`. The `UserParameter` is provided as part of the `ESPUI.addControl` and allows the user to define contextual information that is to be presented to the callback function in an unmodified form. Defining an extended callback is only supported via the `ESPUI.addContol` method. +Alternativly you may use the extended callback funtion which provides three parameters to the callback function `myCallback(Control *sender, int eventname, void * UserParameter)`. The `UserParameter` is provided as part of the `ESPUI.addControl` method set and allows the user to define contextual information that is to be presented to the callback function in an unmodified form.

The below example creates a button and defines a lambda function to implicitly create an `ExtendedCallback` which then invokes a more specialized button callback handler. The example uses the `UserParameter` to hold the `this` pointer to an object instance, providing a mechanism for sending the event to a specific object without the need for a switch / map / lookup translation of the Sender Id to an object reference. ``` @@ -170,6 +170,18 @@ void YourClassName::setup() } }, this); // <-Third parameter for the extended callback + + // or + ButtonElementId = ESPUI.button( + " Button Face Text ", + [](Control *sender, int eventname, void* param) + { + if(param) + { + reinterpret_cast(param)->myButtonCallback(sender, eventname); + } + }, + this); // <-Third parameter for the extended callback } ``` ```