Class TiltSensorComponent

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
  • Field Details

    • DEFAULT_PIN

      protected static final int DEFAULT_PIN
      Default BCM pin of tilt sensor for CrowPi
      See Also:
    • DEFAULT_DEBOUNCE

      protected static final long DEFAULT_DEBOUNCE
      Default debounce time in microseconds
      See Also:
    • DEFAULT_SHAKE_THRESHOLD

      protected static final int DEFAULT_SHAKE_THRESHOLD
      Default 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 context
      address - GPIO address of tilt sensor
      debounce - Debounce time in microseconds
  • Method Details

    • getState

      public TiltSensorComponent.TiltState 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 a DigitalState to a well-known TiltSensorComponent.TiltState
      Specified by:
      mapDigitalState in interface DigitalEventProvider<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

      public void dispatchSimpleEvents(TiltSensorComponent.TiltState value)
      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 using DigitalEventProvider.addListener(EventHandler).
      Specified by:
      dispatchSimpleEvents in interface SimpleEventProvider<TiltSensorComponent.TiltState>
      Parameters:
      value - Event value
    • onTiltLeft

      public void onTiltLeft(SimpleEventHandler handler)
      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

      public void onTiltRight(SimpleEventHandler handler)
      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

      public void onShake(SimpleEventHandler handler)
      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 the DEFAULT_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

      public void onShake(int threshold, SimpleEventHandler handler)
      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 Pi4J DigitalInput associated with this component.
      Specified by:
      getDigitalInput in interface DigitalEventProvider<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 context
      address - GPIO address of tilt sensor
      debounce - Debounce time in microseconds
      Returns:
      DigitalInput configuration