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(id2100) reads the Grove gesture detector and emits aGestureNotificationfor each recognised gesture (here filtered to the four cardinal directions);I2COutputControlProtocol(id2000) drives the Grove RGB LED matrix; the demo sends it aShowDisplayRequestwith a bitmap encoding the matching arrow, and aClearDisplayRequestto 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
-
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/id (Babel routes events by that id) and initialises the auto-clear timestamp to zero. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Application entry point (invoked byMainfor thearrowGesturedemo).voidhandleDemoTimer(DemoTimer t, long time) Timer handler for the auto-clearDemoTimer.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.voidinit(Properties props) 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
-
Constructor Details
-
BabelArrowGestureMatrixDemo
public BabelArrowGestureMatrixDemo()Builds the demo protocol with the sharedBabelDemoname/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:
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. The canonical pattern: checkRegisterIoTDeviceReply.isSuccessful()and on success keep theDeviceHandle. 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 theGetReactiveGestureRequest. 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
Timer handler for the auto-clearDemoTimer. It clears the matrix only if the timer's stamp still equalslastActionTimestamp— 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
Application entry point (invoked byMainfor thearrowGesturedemo). 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
-