Class BabelLcdDemo
- All Implemented Interfaces:
BabelDemo
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"):
- register a reply handler, then send a
RegisterIoTDeviceRequestasking the control protocol to claim the device for us; - when the
RegisterIoTDeviceReplyarrives, keep theDeviceHandle— that opaque token is our reference to the device from then on; - drive the device by sending typed requests that carry the handle
(here
ShowTextRequest), often on aDemoTimerso 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, babelSecurityFields inherited from interface pt.unl.fct.di.tardis.babel.iot.demos.BabelDemo
PROTO_ID, PROTO_NAME -
Constructor Summary
ConstructorsConstructorDescriptionSets the demo's Babel identity (sharedBabelDemo.PROTO_NAME/BabelDemo.PROTO_ID). -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()The bootstrap, invoked once byMain.voidhandleDemoTimer(DemoTimer t, long time) Periodic-timer handler: invoked by Babel every time theDemoTimerfires.voidhandleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceReply: Babel delivers this once the control protocol has tried to register our LCD.voidinit(Properties props) Babel callsinitonce 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
-
Constructor Details
-
BabelLcdDemo
public BabelLcdDemo()Sets the demo's Babel identity (sharedBabelDemo.PROTO_NAME/BabelDemo.PROTO_ID). Note that no device work happens here — the device is requested later ininit(Properties), once Babel is wired up and able to deliver the reply.
-
-
Method Details
-
init
public void init(Properties props) throws pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException, IOException Babel callsinitonce 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:
initin classpt.unl.fct.di.novasys.babel.core.GenericProtocol- Throws:
pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationExceptionIOException
-
handleDemoTimer
Periodic-timer handler: invoked by Babel every time theDemoTimerfires. Each tick drives the LCD by sending aShowTextRequest(carrying thelcdDevicehandle 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 forRegisterIoTDeviceReply: Babel delivers this once the control protocol has tried to register our LCD. This is where we capture theDeviceHandleand only then begin driving the device. -
execute
The bootstrap, invoked once byMain. It builds the Babel runtime this demo needs and starts the event loop.The recipe is the same in every demo: get the
Babelsingleton, 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,initeach in dependency order (control protocol first so its request handlers exist before the demo sends to it), thenstart().
-