Class BabelArrowGestureMatrixDemo

java.lang.Object
pt.unl.fct.di.novasys.babel.core.GenericProtocol
pt.unl.fct.di.tardis.babel.iot.demos.BabelArrowGestureMatrixDemo
All Implemented Interfaces:
BabelDemo

public class BabelArrowGestureMatrixDemo extends pt.unl.fct.di.novasys.babel.core.GenericProtocol implements BabelDemo
Reactive Babel demo: draws an arrow on the Grove RGB LED matrix that matches the direction of the detected gesture (UP/DOWN/LEFT/RIGHT), then clears the matrix shortly afterwards if no new gesture arrives.

Like its sibling demos it illustrates reactive input driving output, wiring two IoT control protocols together:

  • I2CInputControlProtocol (id 2100) reads the Grove gesture detector and emits a GestureNotification for each recognised gesture (here filtered to the four cardinal directions);
  • I2COutputControlProtocol (id 2000) drives the Grove RGB LED matrix; the demo sends it a ShowDisplayRequest with a bitmap encoding the matching arrow, and a ClearDisplayRequest to blank it.

The added wrinkle versus BabelAnyGestureMatrixDemo is a self-cancelling "auto-clear" timer: each gesture stamps lastActionTimestamp and schedules a DemoTimer carrying that stamp; the timer only clears the display if it is still the latest action, so a fresh gesture before the timeout cancels the pending clear. The demo never touches Pi4J/GPIO directly — it only exchanges Babel requests/replies/notifications with the control protocols, following "Pattern 1: driving a Grove device" from the module README.md.

Run it with the command-line name arrowGesture (see Main.java):

java -jar babel-raspberry-iot-examples.jar arrowGesture

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class pt.unl.fct.di.novasys.babel.core.GenericProtocol

    pt.unl.fct.di.novasys.babel.core.GenericProtocol.ProtocolMetricsBabelMetrics
  • Field Summary

    Fields inherited from class pt.unl.fct.di.novasys.babel.core.GenericProtocol

    babel, babelSecurity

    Fields inherited from interface pt.unl.fct.di.tardis.babel.iot.demos.BabelDemo

    PROTO_ID, PROTO_NAME
  • Constructor Summary

    Constructors
    Constructor
    Description
    Builds the demo protocol with the shared BabelDemo name/id (Babel routes events by that id) and initialises the auto-clear timestamp to zero.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Application entry point (invoked by Main for the arrowGesture demo).
    void
    handleDemoTimer(DemoTimer t, long time)
    Timer handler for the auto-clear DemoTimer.
    void
    handleGestureInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.GestureInputReply rep, short protocolId)
    Reply handler for the GetReactiveGestureRequest.
    void
    handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
    Reply handler for RegisterIoTDeviceReply, invoked once per registration.
    void
    Babel lifecycle hook: register all handlers, then send the requests that will trigger them.

    Methods inherited from class pt.unl.fct.di.novasys.babel.core.GenericProtocol

    addSecret, addSecret, addSecret, addSecret, cancelTimer, closeConnection, closeConnection, closeConnection, closeConnection, closeConnection, closeConnection, createChannel, createSecureChannel, createSecureChannel, createSecureChannel, createSecureChannelWithAliases, createSecureChannelWithAliases, createSecureChannelWithIdentities, createSecureChannelWithIdentities, createSecureChannelWithProtoIdentities, enableGenericMetrics, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateIdentity, generateSecret, generateSecret, generateSecret, generateSecret, generateSecretFromPassword, generateSecretFromPassword, generateSecretFromPassword, generateSecretFromPassword, getChannelOrThrow, getDefaultChannel, getDefaultProtoIdentity, getDefaultProtoIdentityCrypt, getDefaultProtoSecret, getMillisSinceBabelStart, getOrGenerateDefaultProtoIdentity, getProtoId, getProtoName, hasProtocolThreadStarted, openConnection, openConnection, openConnection, openConnection, registerChannelEventHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageHandler, registerMessageSerializer, registerMetric, registerReplyHandler, registerRequestHandler, registerSharedChannel, registerTimerHandler, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendMessage, sendReply, sendRequest, setDefaultChannel, setDefaultProtoIdentity, setDefaultProtoIdentity, setDefaultProtoSecret, setupPeriodicTimer, setupTimer, startEventThread, subscribeNotification, triggerNotification, unsubscribeNotification

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BabelArrowGestureMatrixDemo

      public BabelArrowGestureMatrixDemo()
      Builds the demo protocol with the shared BabelDemo name/id (Babel routes events by that id) and initialises the auto-clear timestamp to zero.
  • Method Details

    • init

      public void init(Properties props) throws pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException, IOException
      Babel lifecycle hook: register all handlers, then send the requests that will trigger them. As always, handlers are registered before the requests so no reply or notification can arrive unhandled.
      Specified by:
      init in class pt.unl.fct.di.novasys.babel.core.GenericProtocol
      Throws:
      pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException
      IOException
    • handleRegisterIoTDeviceReply

      public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
      Reply handler for RegisterIoTDeviceReply, invoked once per registration. The canonical pattern: check RegisterIoTDeviceReply.isSuccessful() and on success keep the DeviceHandle. Because both devices share this handler, it branches on the reply's device type to store the right handle and act accordingly.
    • handleGestureInputReply

      public void handleGestureInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.GestureInputReply rep, short protocolId)
      Reply handler for the GetReactiveGestureRequest. It confirms the input protocol accepted the reactive subscription; on success the demo paints the matrix blue as a "ready" acknowledgement and arms a one-shot auto-clear timer stamped with the current time so the display blanks if nothing follows.
    • handleDemoTimer

      public void handleDemoTimer(DemoTimer t, long time)
      Timer handler for the auto-clear DemoTimer. It clears the matrix only if the timer's stamp still equals lastActionTimestamp — i.e. no newer gesture happened since this timer was armed. This "timestamp token" trick lets a fresh gesture implicitly cancel a stale pending clear without tracking timer ids.
    • execute

      public void execute() throws Exception
      Application entry point (invoked by Main for the arrowGesture demo). Standard Babel bootstrap:
      1. get the singleton Babel instance;
      2. load configuration from paradigmshift.config;
      3. instantiate the control protocols (I²C output 2000 + I²C input 2100) and the demo protocol;
      4. registerProtocol each one;
      5. init them in dependency order — control protocols first, then the demo, so the demo's first requests have a target;
      6. start Babel to run the event loops and begin delivering events.
      The demo accesses hardware only through these protocols — it never builds a Pi4J context or opens an I²C bus itself.
      Specified by:
      execute in interface BabelDemo
      Throws:
      Exception