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

Multiple onPressedFor #60

Open FraOre opened 2 years ago

FraOre commented 2 years ago

Hi, I would like to do something like this with the same button:

button.onPressedFor(1000, showIP); button.onPressedFor(3000, restartDevice);

so execute two different callback based on the pressing duration. It doesn't work for me. Just the last function is called. Any idea?

Thank you so much, Francesco

TylerTotally commented 2 years ago

I am trying to do the same thing, and having the same problems.

evert-arias commented 2 years ago

Hi @FraOre and @TylerTotally, I will take a look at this.

JFClaeys commented 2 years ago

Basically, it is the last defined OnPressedFor that is stamped in the EasyButtonBase class's members. WIhich is to say that all the previous calls to OnPressedFor are overwritten by the last one. So in the initial message, the 3000 callback it the winning one, but if they were declared 3000 THEN 1000, only the 1000 one will be used (and triggered)

So I believe that an array (or a chained list of events) should be set in place in order to hold the different OnPressedFor callbacks. Of course, _checkPressedTime should then loop that structure until found proper callback. I'll see how to setup something decently behaving :)

fastbike commented 1 year ago

This would be a useful addition to the library