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
Nested ClassesModifier and TypeClassDescriptionstatic enumAll available states reported by the button component. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final longDefault debounce time in microsecondsprotected final com.pi4j.io.gpio.digital.DigitalInputPi4J digital input instance used by this component -
Constructor Summary
ConstructorsConstructorDescriptionButtonComponent(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 TypeMethodDescriptionvoidAnalyzes the given value passed by an event and triggers 0-n simple events based on it.com.pi4j.io.gpio.digital.DigitalInputReturns the Pi4JDigitalInputassociated with this component.getState()Returns the current state of the touch sensorbooleanisDown()Checks if button is currently pressedbooleanisUp()Checks if button is currently depressed (= NOT pressed)mapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalStateto a well-knownButtonComponent.ButtonStatevoidonDown(SimpleEventHandler handler) Sets or disables the handler for the onDown event.voidonUp(SimpleEventHandler handler) Sets or disables the handler for the onUp event.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEventMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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 aDigitalStateto a well-knownButtonComponent.ButtonState- Specified by:
mapDigitalStatein 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:
dispatchSimpleEventsin 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 Pi4JDigitalInputassociated with this component.- Specified by:
getDigitalInputin interfaceDigitalEventProvider<ButtonComponent.ButtonState>- Returns:
- Pi4J
DigitalInputinstance
-