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
Nested ClassesModifier and TypeClassDescriptionstatic enumAll available states reported by the tilt sensor component. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final longDefault debounce time in microsecondsprotected static final intDefault BCM pin of tilt sensor for CrowPiprotected static final intDefault threshold for considering consecutive state changes as shaking. -
Constructor Summary
ConstructorsConstructorDescriptionTiltSensorComponent(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.DigitalInputConfigbuildDigitalInputConfig(com.pi4j.context.Context pi4j, int address, long debounce) Builds a new DigitalInput instance for the tilt sensor.voidAnalyzes 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 tilt sensor.booleanChecks if the tilt sensor is currently tilted leftbooleanChecks if the tilt sensor is currently tilted rightmapDigitalState(com.pi4j.io.gpio.digital.DigitalState digitalState) Maps aDigitalStateto a well-knownTiltSensorComponent.TiltStatevoidonShake(int threshold, SimpleEventHandler handler) Sets or disables the handler for the onShake event.voidonShake(SimpleEventHandler handler) Sets or disables the handler for the onShake event.voidonTiltLeft(SimpleEventHandler handler) Sets or disables the handler for the onTiltLeft event.voidonTiltRight(SimpleEventHandler handler) Sets or disables the handler for the onTiltRight 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_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 aDigitalStateto a well-knownTiltSensorComponent.TiltState- Specified by:
mapDigitalStatein 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:
dispatchSimpleEventsin 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_THRESHOLDas 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 Pi4JDigitalInputassociated with this component.- Specified by:
getDigitalInputin interfaceDigitalEventProvider<TiltSensorComponent.TiltState>- Returns:
- Pi4J
DigitalInputinstance
-
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
-