Package com.diozero.devices
Class Button
- All Implemented Interfaces:
DigitalInputDeviceInterface
,DeviceEventConsumer<DigitalInputEvent>
,Closeable
,AutoCloseable
,Consumer<DigitalInputEvent>
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("Event: {}", 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);
}
-
Field Summary
Fields inherited from class com.diozero.api.DigitalInputDevice
device
Fields inherited from class com.diozero.api.AbstractDigitalInputDevice
activeHigh
Fields inherited from class com.diozero.api.GpioDevice
gpio
-
Constructor Summary
Constructors Constructor Description Button(int gpio)
Pull up / down configuration defaults to NONE.Button(int gpio, GpioPullUpDown pud)
Button(GpioDeviceFactoryInterface deviceFactory, int gpio, GpioPullUpDown pud)
-
Method Summary
Modifier and Type Method 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.Methods inherited from class com.diozero.api.DigitalInputDevice
close, getPullUpDown, getTrigger, getValue, isActive, removeListener, setListener
Methods inherited from class com.diozero.api.AbstractDigitalInputDevice
accept, disableDeviceListener, enableDeviceListener, isActiveHigh, whenActivated, whenDeactivated
Methods inherited from class com.diozero.api.GpioInputDevice
addListener, removeAllListeners, removeListener
Methods inherited from class com.diozero.api.GpioDevice
getGpio
-
Constructor Details
-
Button
Pull up / down configuration defaults to NONE.- Parameters:
gpio
- GPIO to which the button is connected.- Throws:
RuntimeIOException
- If an I/O error occurred.
-
Button
- Parameters:
gpio
- GPIO to which the button is connected.pud
- Pull up / down configuration (NONE, PULL_UP, PULL_DOWN).- Throws:
RuntimeIOException
- If an I/O error occurred.
-
Button
public Button(GpioDeviceFactoryInterface deviceFactory, int gpio, GpioPullUpDown pud) throws RuntimeIOException- Parameters:
deviceFactory
- Device factory to use to contruct the device.gpio
- GPIO for the button.pud
- Pull up / down configuration (NONE, PULL_UP, PULL_DOWN).- Throws:
RuntimeIOException
- If an I/O error occurred.
-
-
Method Details
-
isPressed
public boolean isPressed()Get the current state.- Returns:
- Return true if the button is currently pressed.
-
isReleased
public boolean isReleased()Get the current state.- Returns:
- Return true if the button is currently released.
-
whenPressed
Action to perform when the button is pressed.- Parameters:
action
- Action function to invoke.
-
whenReleased
Action to perform when the button is released.- Parameters:
action
- Action function to invoke.
-