Class BabelCardinalGestureMatrixDemo

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

public class BabelCardinalGestureMatrixDemo extends pt.unl.fct.di.novasys.babel.core.GenericProtocol implements BabelDemo
Reactive Babel demo: colours the whole Grove RGB LED matrix according to the cardinal gesture detected (UP/DOWN/LEFT/RIGHT each map to a colour).

It is the simplest of the three gesture demos and a clean example of reactive input driving output, wiring two IoT control protocols:

  • I2CInputControlProtocol (id 2100) reads the Grove gesture detector and emits a GestureNotification for each recognised gesture (filtered here to the four cardinal directions);
  • I2COutputControlProtocol (id 2000) drives the Grove RGB LED matrix; the demo sends it a SetDisplayColorRequest carrying the matrix's DeviceHandle and the colour for the detected direction.

The demo is a GenericProtocol that never touches Pi4J, I²C or any GPIO line directly — it only exchanges Babel requests/replies/notifications with the control protocols, which own the hardware. This is "Pattern 1: driving a Grove device" from the module README.md, with a notification subscription added for the reactive input.

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

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

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 and id; Babel uses the id to route events to this instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Application entry point (invoked by Main for the cardinalGesture demo).
    void
    handleDemoTimer(DemoTimer t, long time)
    Timer handler driven by the periodic 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 (here twice).
    void
    Babel lifecycle hook: register all handlers, then send the requests that 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

    • BabelCardinalGestureMatrixDemo

      public BabelCardinalGestureMatrixDemo()
      Builds the demo protocol with the shared BabelDemo name and id; Babel uses the id to route events to this instance.
  • 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 trigger them. Handlers are registered before the requests so no reply or notification can arrive before there is a handler to receive it.
      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 (here twice). The canonical pattern: check RegisterIoTDeviceReply.isSuccessful() and on success keep the DeviceHandle; branch on the reply's device type since both registrations share this one handler.
    • 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 visible "ready" acknowledgement. The per-gesture colouring happens later in handleGestureNotification(pt.unl.fct.di.tardis.babel.iot.controlprotocols.notifications.GestureNotification, short).
    • handleDemoTimer

      public void handleDemoTimer(DemoTimer t, long time)
      Timer handler driven by the periodic DemoTimer. Each tick repaints the matrix green, giving a steady "idle" colour in between gesture-triggered colours. Illustrates a Babel protocol mixing timer-driven and event-driven work in one single-threaded handler model.
    • execute

      public void execute() throws Exception
      Application entry point (invoked by Main for the cardinalGesture 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 reaches hardware only through these protocols — never via Pi4J/GPIO directly.
      Specified by:
      execute in interface BabelDemo
      Throws:
      Exception