java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.TiltSensorComponent
- All Implemented Interfaces:
DigitalEventProvider<TiltSensorComponent.TiltState>
,EventProvider<DigitalEventListener<TiltSensorComponent.TiltState>,
,TiltSensorComponent.TiltState> SimpleEventProvider<TiltSensorComponent.TiltState>
public class TiltSensorComponent
extends Component
implements DigitalEventProvider<TiltSensorComponent.TiltState>
Implementation of the CrowPi tilt sensor using GPIO with Pi4J
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
All available states reported by the tilt sensor component. -
Field Summary
Modifier and TypeFieldDescriptionprotected static final long
Default debounce time in microsecondsprotected static final int
Default BCM pin of tilt sensor for CrowPiprotected static final int
Default threshold for considering consecutive state changes as shaking. -
Constructor Summary
ConstructorDescriptionTiltSensorComponent
(com.pi4j.context.Context pi4j) Creates a new tilt sensor component using the default setup.TiltSensorComponent
(com.pi4j.context.Context pi4j, int address, long debounce) Creates a new tilt sensor component with custom GPIO address and debounce time. -
Method Summary
Modifier and TypeMethodDescriptionprotected com.pi4j.io.gpio.digital.DigitalInputConfig
buildDigitalInputConfig
(com.pi4j.context.Context pi4j, int address, long debounce) Builds a new DigitalInput instance for the tilt sensor.void
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 tilt sensor.boolean
Checks if the tilt sensor is currently tilted leftboolean
Checks if the tilt sensor is currently tilted rightmapDigitalState
(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalState
to a well-knownTiltSensorComponent.TiltState
void
onShake
(int threshold, SimpleEventHandler handler) Sets or disables the handler for the onShake event.void
onShake
(SimpleEventHandler handler) Sets or disables the handler for the onShake event.void
onTiltLeft
(SimpleEventHandler handler) Sets or disables the handler for the onTiltLeft event.void
onTiltRight
(SimpleEventHandler handler) Sets or disables the handler for the onTiltRight 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_PIN
protected static final int DEFAULT_PINDefault BCM pin of tilt sensor for CrowPi- See Also:
-
DEFAULT_DEBOUNCE
protected static final long DEFAULT_DEBOUNCEDefault debounce time in microseconds- See Also:
-
DEFAULT_SHAKE_THRESHOLD
protected static final int DEFAULT_SHAKE_THRESHOLDDefault threshold for considering consecutive state changes as shaking.- See Also:
-
-
Constructor Details
-
TiltSensorComponent
public TiltSensorComponent(com.pi4j.context.Context pi4j) Creates a new tilt sensor component using the default setup.- Parameters:
pi4j
- Pi4J context
-
TiltSensorComponent
public TiltSensorComponent(com.pi4j.context.Context pi4j, int address, long debounce) Creates a new tilt sensor component with custom GPIO address and debounce time.- Parameters:
pi4j
- Pi4J contextaddress
- GPIO address of tilt sensordebounce
- Debounce time in microseconds
-
-
Method Details
-
getState
Returns the current state of the tilt sensor.- Returns:
- Current tilt sensor state
-
mapDigitalState
public TiltSensorComponent.TiltState mapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalState
to a well-knownTiltSensorComponent.TiltState
- Specified by:
mapDigitalState
in interfaceDigitalEventProvider<TiltSensorComponent.TiltState>
- Parameters:
digitalState
- Pi4J digital state to map- Returns:
- Mapped tilt state
-
hasLeftTilt
public boolean hasLeftTilt()Checks if the tilt sensor is currently tilted left- Returns:
- True if tilted left
-
hasRightTilt
public boolean hasRightTilt()Checks if the tilt sensor is currently tilted right- Returns:
- True if tilted right
-
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<TiltSensorComponent.TiltState>
- Parameters:
value
- Event value
-
onTiltLeft
Sets or disables the handler for the onTiltLeft event. This event gets triggered whenever the sensor is tilted left. Only a single event handler can be registered at once.- Parameters:
handler
- Event handler to call or null to disable
-
onTiltRight
Sets or disables the handler for the onTiltRight event. This event gets triggered whenever the sensor is tilted right. Only a single event handler can be registered at once.- Parameters:
handler
- Event handler to call or null to disable
-
onShake
Sets or disables the handler for the onShake event. This event gets triggered whenever the sensor alternates between left/right in a short time. Using this method will use theDEFAULT_SHAKE_THRESHOLD
as the desired shake threshold. Only a single event handler can be registered at once.- Parameters:
handler
- Event handler to call or null to disable
-
onShake
Sets or disables the handler for the onShake event. This event gets triggered whenever the sensor alternates between left/right in a short time. Only a single event handler can be registered at once.- Parameters:
threshold
- Threshold when to consider consecutive state changes as shaking. As an example, passing 3 would mean that repeatedly alternating between left/right/left (or vice-versa) would be considered as shaking.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<TiltSensorComponent.TiltState>
- Returns:
- Pi4J
DigitalInput
instance
-
buildDigitalInputConfig
protected com.pi4j.io.gpio.digital.DigitalInputConfig buildDigitalInputConfig(com.pi4j.context.Context pi4j, int address, long debounce) Builds a new DigitalInput instance for the tilt sensor.- Parameters:
pi4j
- Pi4J contextaddress
- GPIO address of tilt sensordebounce
- Debounce time in microseconds- Returns:
- DigitalInput configuration
-