java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.ButtonComponent
- All Implemented Interfaces:
DigitalEventProvider<ButtonComponent.ButtonState>
,EventProvider<DigitalEventListener<ButtonComponent.ButtonState>,
,ButtonComponent.ButtonState> SimpleEventProvider<ButtonComponent.ButtonState>
public class ButtonComponent
extends Component
implements DigitalEventProvider<ButtonComponent.ButtonState>
Implementation of the CrowPi buttons using GPIO with Pi4J
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
All available states reported by the button component. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final long
Default debounce time in microsecondsprotected final com.pi4j.io.gpio.digital.DigitalInput
Pi4J digital input instance used by this component -
Constructor Summary
ConstructorDescriptionButtonComponent
(com.pi4j.context.Context pi4j, int address, boolean inverted, long debounce) Creates a new button component with custom GPIO address and debounce time.ButtonComponent
(com.pi4j.context.Context pi4j, Button button) Creates a new button component using the default setup for the given CrowPi button. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Analyzes the given value passed by an event and triggers 0-n simple events based on it.com.pi4j.io.gpio.digital.DigitalInput
Returns the Pi4JDigitalInput
associated with this component.getState()
Returns the current state of the touch sensorboolean
isDown()
Checks if button is currently pressedboolean
isUp()
Checks if button is currently depressed (= NOT pressed)mapDigitalState
(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalState
to a well-knownButtonComponent.ButtonState
void
onDown
(SimpleEventHandler handler) Sets or disables the handler for the onDown event.void
onUp
(SimpleEventHandler handler) Sets or disables the handler for the onUp event.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEvent
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.pi4j.crowpi.components.events.DigitalEventProvider
addListener, removeListener
-
Field Details
-
DEFAULT_DEBOUNCE
protected static final long DEFAULT_DEBOUNCEDefault debounce time in microseconds- See Also:
-
digitalInput
protected final com.pi4j.io.gpio.digital.DigitalInput digitalInputPi4J digital input instance used by this component
-
-
Constructor Details
-
ButtonComponent
Creates a new button component using the default setup for the given CrowPi button.- Parameters:
pi4j
- Pi4J contextbutton
- Desired CrowPi button
-
ButtonComponent
public ButtonComponent(com.pi4j.context.Context pi4j, int address, boolean inverted, long debounce) Creates a new button component with custom GPIO address and debounce time.- Parameters:
pi4j
- Pi4J contextaddress
- GPIO address of buttoninverted
- Specify if button state is inverteddebounce
- Debounce time in microseconds
-
-
Method Details
-
getState
Returns the current state of the touch sensor- Returns:
- Current button state
-
isDown
public boolean isDown()Checks if button is currently pressed- Returns:
- True if button is pressed
-
isUp
public boolean isUp()Checks if button is currently depressed (= NOT pressed)- Returns:
- True if button is depressed
-
mapDigitalState
public ButtonComponent.ButtonState mapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalState
to a well-knownButtonComponent.ButtonState
- Specified by:
mapDigitalState
in interfaceDigitalEventProvider<ButtonComponent.ButtonState>
- Parameters:
digitalState
- Pi4J digital state to map- Returns:
- Mapped touch state
-
dispatchSimpleEvents
Analyzes the given value passed by an event and triggers 0-n simple events based on it. This method allows mapping various value/state changes to simple events. Must be attached usingDigitalEventProvider.addListener(EventHandler)
.- Specified by:
dispatchSimpleEvents
in interfaceSimpleEventProvider<ButtonComponent.ButtonState>
- Parameters:
state
- Event value
-
onDown
Sets or disables the handler for the onDown event. This event gets triggered whenever the button is pressed. Only a single event handler can be registered at once.- Parameters:
handler
- Event handler to call or null to disable
-
onUp
Sets or disables the handler for the onUp event. This event gets triggered whenever the button is no longer pressed. Only a single event handler can be registered at once.- Parameters:
handler
- Event handler to call or null to disable
-
getDigitalInput
public com.pi4j.io.gpio.digital.DigitalInput getDigitalInput()Returns the Pi4JDigitalInput
associated with this component.- Specified by:
getDigitalInput
in interfaceDigitalEventProvider<ButtonComponent.ButtonState>
- Returns:
- Pi4J
DigitalInput
instance
-