Class BabelLcdDemo

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

public class BabelLcdDemo extends pt.unl.fct.di.novasys.babel.core.GenericProtocol implements BabelDemo
The reference demo for this project, and the one the README walks through end-to-end under "Anatomy of a demo". It drives a Grove LCD (an I²C device) through the I2COutputControlProtocol (protocol id 2000, the control protocol that owns LCD and LED-matrix output), cycling a new line of text onto the display every 1.5 seconds. Run it with:
java -jar babel-raspberry-iot-examples.jar Lcd

One class, two roles. As a GenericProtocol it lives on Babel's single-threaded event loop, holding the device handle and reacting to replies and timers; as a BabelDemo it knows how to bootstrap its own runtime in execute().

The pattern every Grove demo follows (see README "Pattern 1"):

  1. register a reply handler, then send a RegisterIoTDeviceRequest asking the control protocol to claim the device for us;
  2. when the RegisterIoTDeviceReply arrives, keep the DeviceHandle — that opaque token is our reference to the device from then on;
  3. drive the device by sending typed requests that carry the handle (here ShowTextRequest), often on a DemoTimer so it repeats.

The application never touches Pi4J, I²C, or GPIO directly. It only sends Babel requests; the I2COutputControlProtocol translates them into the actual hardware operations. That separation is the whole point of the layering and is what lets this code stay short and portable.

This demo is derived from NOVA FCT / TaRDIS work (see the README "Credits").

  • 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
    Sets the demo's Babel identity (shared BabelDemo.PROTO_NAME / BabelDemo.PROTO_ID).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    The bootstrap, invoked once by Main.
    void
    handleDemoTimer(DemoTimer t, long time)
    Periodic-timer handler: invoked by Babel every time the DemoTimer fires.
    void
    handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
    Reply handler for RegisterIoTDeviceReply: Babel delivers this once the control protocol has tried to register our LCD.
    void
    Babel calls init once this protocol is registered.

    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

  • Method Details

    • init

      public void init(Properties props) throws pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException, IOException
      Babel calls init once this protocol is registered. This is where the register-then-drive pattern starts.

      Order matters: we wire up our handlers before sending any request, so a reply can never arrive before the handler that consumes it is in place.

      Specified by:
      init in class pt.unl.fct.di.novasys.babel.core.GenericProtocol
      Throws:
      pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException
      IOException
    • handleDemoTimer

      public void handleDemoTimer(DemoTimer t, long time)
      Periodic-timer handler: invoked by Babel every time the DemoTimer fires. Each tick drives the LCD by sending a ShowTextRequest (carrying the lcdDevice handle and the next line of text) to the control protocol — the recurring "act repeatedly on a device" half of the pattern.
    • handleRegisterIoTDeviceReply

      public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
      Reply handler for RegisterIoTDeviceReply: Babel delivers this once the control protocol has tried to register our LCD. This is where we capture the DeviceHandle and only then begin driving the device.
    • execute

      public void execute() throws Exception
      The bootstrap, invoked once by Main. It builds the Babel runtime this demo needs and starts the event loop.

      The recipe is the same in every demo: get the Babel singleton, load config, instantiate only the control protocol(s) this demo uses (here just the I²C output protocol), register both that protocol and the demo, init each in dependency order (control protocol first so its request handlers exist before the demo sends to it), then start().

      Specified by:
      execute in interface BabelDemo
      Throws:
      Exception