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(id2100) reads the Grove gesture detector and emits aGestureNotificationfor each recognised gesture (filtered here to the four cardinal directions);I2COutputControlProtocol(id2000) drives the Grove RGB LED matrix; the demo sends it aSetDisplayColorRequestcarrying the matrix'sDeviceHandleand 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
-
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
ConstructorsConstructorDescriptionBuilds the demo protocol with the sharedBabelDemoname and id; Babel uses the id to route events to this instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Application entry point (invoked byMainfor thecardinalGesturedemo).voidhandleDemoTimer(DemoTimer t, long time) Timer handler driven by the periodicDemoTimer.voidhandleGestureInputReply(pt.unl.fct.di.tardis.babel.iot.controlprotocols.replies.GestureInputReply rep, short protocolId) Reply handler for theGetReactiveGestureRequest.voidhandleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceReply, invoked once per registration (here twice).voidinit(Properties props) 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
-
Constructor Details
-
BabelCardinalGestureMatrixDemo
public BabelCardinalGestureMatrixDemo()Builds the demo protocol with the sharedBabelDemoname 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:
initin classpt.unl.fct.di.novasys.babel.core.GenericProtocol- Throws:
pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationExceptionIOException
-
handleRegisterIoTDeviceReply
public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Reply handler forRegisterIoTDeviceReply, invoked once per registration (here twice). The canonical pattern: checkRegisterIoTDeviceReply.isSuccessful()and on success keep theDeviceHandle; 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 theGetReactiveGestureRequest. 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 inhandleGestureNotification(pt.unl.fct.di.tardis.babel.iot.controlprotocols.notifications.GestureNotification, short). -
handleDemoTimer
Timer handler driven by the periodicDemoTimer. 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
Application entry point (invoked byMainfor thecardinalGesturedemo). Standard Babel bootstrap:- get the singleton
Babelinstance; - load configuration from
paradigmshift.config; - instantiate the control protocols (I²C output 2000 + I²C input 2100) and the demo protocol;
registerProtocoleach one;initthem in dependency order — control protocols first, then the demo, so the demo's first requests have a target;startBabel to run the event loops and begin delivering events.
- get the singleton
-