Measuring time between press and release of a button?

42 views
Skip to first unread message

GeertVc

unread,
Feb 6, 2017, 11:16:01 AM2/6/17
to Pi4J
Hi,

Using the MCP23017 classes of Pi4J, is it possible to measure the time between pressing and releasing the button?

I would like to assign other functionality to a button when it is pressed for, say, 1 second compared to functionality which is called when a 'normal' button press is executed.

So:
- short (or normal) press: calling function A
- long press: calling function B

There's already functionality built in for debouncing, so I was wondering if something similar exists for short/long keypresses where one could define the time of a long keypress.

If that doesn't exist yet and there are no immediate plans to come up with an implementation, I might have a look myself to see how this could be done...

Robert Savage

unread,
Feb 8, 2017, 9:58:18 AM2/8/17
to Pi4J
Hi GertVc,

Nothing like that exists currently at the GPIO layer.
However, there is a "Button" interface in the pi4j-device project and a "GpioButtonComponent" implementation class that supports GPIO events and emits button PRESS and RELEASE events and additionally includes a button HOLD event.

Example:

Button button = new GpioButtonComponent(myInputPin);

button.addListener(1000, new ButtonHoldListener() {
@Override
public void onButtonHold(ButtonEvent event) {
// DO WORK HERE!
}
});

You might take a look at that and see if it meets your needs.  You may need to extend or modify the impl to handle duration ranges for short vs long hold times for multiple hold events.

Thanks, Robert
Reply all
Reply to author
Forward
0 new messages