SMFSW / SeqButton

Handling filtered button pushes with callbacks for Arduino

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Button or pin in Callback

eried opened this issue · comments

Hi,

Is there is any easy way to get the button or pin number in the callback? (to know which one triggered it)

commented

Changes made

Thanks a lot! I tried several libraries (about 12, using "button" or "debounce" keyword) but yours is the only one that works and it is simple enough.

It would be possible to add the milliseconds that the key was pressed on the release event? or as a field in the button itself? I do not really care about "hold" or "double click" detection like other libraries try to implement, but the milliseconds would be nice for filtering actions.

If you think adding the ms is wasteful, can you suggest a clean way that does not require a hack of the library (to keep my code portable using your library)

commented

Thanks for the nice words!

I may add this feature in the library too, but i'm not sure it's really useful in the library itself.
I think what you're trying to achieve is like this example: OffTimerButton.ino
It's not a proper way to handle this but demonstrates the possibility.

The best would be to register a callback function when releasing the button that updates a global variable (defined outside any function in your sketch) with milliseconds().
Then, in the loop, you can check for the time to trigger an action (and using also a boolean that you update when action is performed and set to False in the callback to be able to trigger the action again, without always triggering the action when time has elapsed).

I see, but each button should have an individual value (press -> release: it took XX ms) so maybe I can exemplify it with another pull request (I am not sure on how to make this use 0 ram if user does not call the getHeldMs() function. This is not for a timer like in the example but more for detecting long presses.

void callbackRelease()
{
    Serial.print("Button was held for: "):
    Serial.print(btn.getHeldMs()): // basically does millis()-millis_when_btn_was_last_pressed()
    Serial.println(" ms."):
}

(Maybe instead of returning the pin, the callback should return the btn instance)

commented

I just added another example here on github (not delivered by the Arduino library manager yet).
Still you can use the Arduino library manager to get the latest library source code, no change made, I just added the example.

I think this is what you try to achieve: TimingExample.ino

Oh yes, thanks, I might end doing that to keep everything 100% compatible.

Anyway, few things to think:

  1. Do you think it too much overhead if I add the timing to the button instance itself? (any opinion on that?)

Serial.println(btn.getHeldFor()):

  1. What about returning the Button instance itself in the callback, vs the pin? (the button already has a pin).
void callbackRelease(SeqButton btn)
{
    Serial.println(btn.getPin()): 
}
commented

You should try the latest commit (also available in the arduino library manager).
I hope it's not bugged or having regressions on some functionalities (I tested it for most cases, but I don't exclude there could be some issues)