Open PheebeUK opened 5 months ago
Hi Pheebe,
How are you? :) Thanks for your input! Could you show me a code example on how you would like to use this functionality in the 'front end'?
Kind regards, Johannes
What about creating a CtrlGroup class, passing a reference of the group to each button that belongs to the group? This setup allows one release handler to be triggered by any button in the group, and the ID of the button is accessible within the handler. And in the sketch you could do something like this:
void onGroupPress(Groupable* object) {
Serial.print("Pressed: ");
Serial.println(object->identifier);
}
void onGroupRelease(Groupable* object) {
Serial.print("Released: ");
Serial.println(object->identifier);
}
CtrlGroup buttonGroup(onGroupPress, onGroupRelease);
CtrlBtn button1(1, 15);
CtrlBtn button2(2, 15);
void setup() {
Serial.begin(9600);
button1.setIdentifier("something");
button2.setIdentifier("else");
button1.setGroup(&buttonGroup);
button2.setGroup(&buttonGroup);
}
void loop() {
buttonGroup.process();
}
Hi @PheebeUK
I thought your idea for adding shared event handlers was a really good one, as it offers a nice alternate approach to handling inputs. So I went ahead and developed this feature for the 1.5.0 release of the library. You can find it here
Here's the example on how to implement it for buttons (it can also be used for rotary encoders & potentiometers):
#include <CtrlGroup.h>
#include <CtrlBtn.h>
CtrlGroup buttonGroup;
CtrlBtn button1(1, 15);
CtrlBtn button2(2, 15);
void onPress(Groupable& button) {
bool canPrint = button.getBoolean("canPrint");
int id = button.getInteger("id");
String name = button.getString("name");
if (canPrint) {
Serial.print("Pressed button with id: ");
Serial.print(id);
Serial.print(" / Name: ");
Serial.println(name);
}
}
void setup() {
Serial.begin(9600);
buttonGroup.setOnPress(onPress);
button1.setGroup(&buttonGroup);
button2.setGroup(&buttonGroup);
button1.setInteger("id", 0);
button1.setString("name", "Button 1");
button1.setBoolean("canPrint", true);
button2.setInteger("id", 1);
button2.setString("name", "Button 2");
button2.setBoolean("canPrint", true);
}
void loop() {
buttonGroup.process();
}
P.S. Propagation of new releases to the Arduino IDE usually takes a few hours. For PlatformIO it takes about 24 hours to show up.
All the examples (with more explanation) can be found here
I hope this help! Let me know what you think :)
Issue I have a matrix of 16 buttons, and I want to be able to use a single event handler to handle all events of a single type (say onRelease) from a matrix of buttons. However the framework itself does not provide any way to determine which object raised the event. Extending this idea out, it seems that it should be possible to have a single event handler to handle all event types raised by a class, so a single handler could handle all event types as well as be able to identify which object raised the event.
Potential Solutions This is just a bit of brainstorming