evert-arias / EasyButton

Arduino library for debouncing momentary contact switches, detect press, release, long press and sequences with event definitions and callbacks.
https://easybtn.earias.me
MIT License
447 stars 62 forks source link

Enhancement: instance pointer in callbacks #73

Closed fastbike closed 1 year ago

fastbike commented 1 year ago

Devices often contain multiple buttons, the one I'm dealing with has 6. The easiest way to model these is in an array.

In a device with multiple buttons the code can get pretty unwieldy if you need to attach 3 callbacks to each of the 6 buttons, that's 18 functions to write, maintain and understand.

Is there a simple way to detect which button has fired the callback e.g. like "this" is javascript, c#, "self" in Delphi Pascal etc ? void onPressed(button this) { Serial.println("Button x has been pressed!"); }

fastbike commented 1 year ago

I've made some changes in my fork of this library - it allows the callback to access the button and take action based on which button fired the callback, see https://github.com/fastbike/EasyButton/commit/041232204007687e44eda7c1d0f22969541cc50e