public class Button extends DigitalInputDevice
Provides push button related utility methods.
From the ButtonTest example:
try (Button button = new Button(inputPin, GpioPullUpDown.PULL_UP)) {
button.addListener(event -> Logger.debug("valueChanged({})", event));
Logger.debug("Waiting for 10s - *** Press the button connected to input pin " + inputPin + " ***");
SleepUtil.sleepSeconds(10);
}
Controlling an LED with a button ButtonControlledLed:
try (Button button = new Button(buttonPin, GpioPullUpDown.PULL_UP); LED led = new LED(ledPin)) {
button.whenPressed(led::on);
button.whenReleased(led::off);
Logger.info("Waiting for 10s - *** Press the button connected to pin {} ***", Integer.valueOf(buttonPin));
SleepUtil.sleepSeconds(10);
}
device
activeHigh
gpio
Constructor and Description |
---|
Button(GpioDeviceFactoryInterface deviceFactory,
int gpio,
GpioPullUpDown pud) |
Button(int gpio)
Pull up / down configuration defaults to NONE.
|
Button(int gpio,
GpioPullUpDown pud) |
Modifier and Type | Method and Description |
---|---|
boolean |
isPressed()
Get the current state.
|
boolean |
isReleased()
Get the current state.
|
void |
whenPressed(Action action)
Action to perform when the button is pressed.
|
void |
whenReleased(Action action)
Action to perform when the button is released.
|
close, getPullUpDown, getTrigger, getValue, isActive, removeListener, setListener
disableDeviceListener, enableDeviceListener, isActiveHigh, valueChanged, whenActivated, whenDeactivated
addListener, removeAllListeners, removeListener
getGpio
public Button(int gpio) throws RuntimeIOException
gpio
- GPIO to which the button is connected.RuntimeIOException
- If an I/O error occurred.public Button(int gpio, GpioPullUpDown pud) throws RuntimeIOException
gpio
- GPIO to which the button is connected.pud
- Pull up / down configuration (NONE, PULL_UP, PULL_DOWN).RuntimeIOException
- If an I/O error occurred.public Button(GpioDeviceFactoryInterface deviceFactory, int gpio, GpioPullUpDown pud) throws RuntimeIOException
deviceFactory
- Device factory to use to contruct the device.gpio
- GPIO for the button.pud
- Pull up / down configuration (NONE, PULL_UP, PULL_DOWN).RuntimeIOException
- If an I/O error occurred.public boolean isPressed()
public boolean isReleased()
public void whenPressed(Action action)
action
- Action function to invoke.public void whenReleased(Action action)
action
- Action function to invoke.Copyright © 2016–2017 mattjlewis. All rights reserved.