Class BabelEncoderMatrixDemo
- All Implemented Interfaces:
BabelDemo
This is the demo to read to learn the asynchronous notification
pattern. Unlike the LED and LCD demos (which only register an output device
and drive it on a timer), this one subscribes to an
EncoderNotification: the input control protocol pushes one to us every
time the knob turns, and we react by updating the matrix. There is no polling.
Devices & control protocols used.
- Grove LED matrix (
DeviceType.GROVE_LED_MATRIX) — output, driven throughI2COutputControlProtocol(protocol id 2000). - Grove rotary encoder (
DeviceType.GROVE_ENCODER) — reactive input, read throughDigitalInputControlProtocol(protocol id 2200), which emitsEncoderNotifications.
The teaching point. The app never touches Pi4J, GPIO or
I²C directly. It registers two devices, subscribes to encoder turns, and reacts
by sending a DisplayBarRequest; the control protocols do all the
hardware work.
To run: java -jar <jar> encoderMatrix (see
Main.java).
Configuration. The encoder's GPIO line is read from the
encoder.line property (default 5) in
paradigmshift.config; the matrix is an I²C device and needs no line.
Based on IoT-control demos originally developed at NOVA FCT for the TaRDIS project; provided and evolved independently by ParadigmShift.
-
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
FieldsModifier and TypeFieldDescriptionstatic final StringConfig key naming the GPIO line the rotary encoder is wired to.static final StringDefault GPIO line for the encoder.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 protocol identity shared by all demos (BabelDemo.PROTO_NAME/BabelDemo.PROTO_ID). -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute()Entry point for this demo (called fromMain).voidhandleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Shared reply handler for both device registrations.voidinit(Properties props) Wires this protocol's event handlers and registers both devices.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
-
Field Details
-
ENCODER_PORT
Config key naming the GPIO line the rotary encoder is wired to.- See Also:
-
ENCODER_PORT_DEFAULT
Default GPIO line for the encoder.- See Also:
-
-
Constructor Details
-
BabelEncoderMatrixDemo
public BabelEncoderMatrixDemo()Sets the protocol identity shared by all demos (BabelDemo.PROTO_NAME/BabelDemo.PROTO_ID). Babel handlers are wired later ininit(Properties).
-
-
Method Details
-
init
public void init(Properties props) throws pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationException, IOException Wires this protocol's event handlers and registers both devices.The ordering is the key lesson:
- Register the
RegisterIoTDeviceReplyhandler before sending any registration request, so the reply cannot arrive first. One reply handler serves both devices; it tells them apart byRegisterIoTDeviceReply.getDeviceType(). subscribeNotification(EncoderNotification...)— this is how the demo receives asynchronous encoder turns. Once the encoder is set up for reactive reporting, the input control protocol pushes anEncoderNotificationon every detent.- Send a
RegisterIoTDeviceRequestfor each device, each to its owning control protocol — the matrix toI2COutputControlProtocol(no GPIO line; it is I²C) and the encoder toDigitalInputControlProtocol(with its GPIO line).
handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply, short).- Specified by:
initin classpt.unl.fct.di.novasys.babel.core.GenericProtocol- Throws:
pt.unl.fct.di.novasys.babel.exceptions.HandlerRegistrationExceptionIOException
- Register the
-
handleRegisterIoTDeviceReply
public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId) Shared reply handler for both device registrations. Babel routes everyRegisterIoTDeviceReplyhere; we demux onRegisterIoTDeviceReply.getDeviceType()to learn which device it is for and stash the matchingDeviceHandle.When the encoder reply arrives we issue a
GetReactiveEncoderRequestwith aThreshold.none()threshold: that arms the encoder for reactive reporting, so every subsequent turn comes back as anEncoderNotification(handled inhandleEncoderNotification(pt.unl.fct.di.tardis.babel.iot.controlprotocols.notifications.EncoderNotification, short)). On any failure the demo bails out, and each branch sanity-checks the alias against what we asked for. -
execute
Entry point for this demo (called fromMain). Bootstraps Babel: grab theBabelsingleton, loadparadigmshift.config, instantiate the two control protocols this demo needs (I2COutputControlProtocolfor the matrix,DigitalInputControlProtocolfor the encoder), register them plus this demo, theninitall three in dependency order (control protocols first so their handlers exist before the demo sends to them) and start the event loop.
-