Class BabelChainableLedsRGBDemo

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

public class BabelChainableLedsRGBDemo extends pt.unl.fct.di.novasys.babel.core.GenericProtocol implements BabelDemo
Demo: a strip of Grove chainable RGB LEDs whose colours animate as a travelling rainbow. On every tick a fresh colour is computed for the head of the strip (LED 0) and the previous colours are shifted one position down the chain, so the colour appears to flow along the strip.

Colours are expressed in the RGB model here: three bytes (red, green, blue) per LED. The HSB sibling of this demo (BabelChainableLedsHSBDemo) animates the identical strip using the hue/saturation/brightness model instead — comparing the two side by side is the point of having both.

Devices & control protocols used. One Grove chainable RGB LED strip (DeviceType.GROVE_CHAINABLE_RGB), driven through the DigitalOutputControlProtocol (protocol id 2300). The strip lives on a single GPIO data line; the control protocol does all the bit-banging.

The teaching point. This application protocol never touches Pi4J, GPIO or the LED wire format directly. It only sends Babel requests (RegisterIoTDeviceRequest, SetMultipleChainableLEDColorRGBRequest) to the control protocol, which performs the actual GPIO work. The app reasons purely in terms of "device handles" and "set these colours"; the hardware lives behind that boundary.

To run: java -jar <jar> ledsRGB (see Main.java).

Configuration. The strip length is read from the rgb.led.count property (via DigitalOutputControlProtocol.RGB_LED_COUNT, default 1) and the GPIO data line from led.line (default 24) — both in paradigmshift.config. Line 24 is chosen so the strip coexists with a seated LoRa HAT; see the project README for why line 26 must be avoided.

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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Config key naming the GPIO data line the LED strip is wired to.
    static final String
    Default GPIO line (BCM 24) — coexists with a seated LoRa HAT.
    static final String
    Human-readable name we register the strip under, then verify in the reply.

    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 protocol identity shared by all demos (BabelDemo.PROTO_NAME / BabelDemo.PROTO_ID) and seeds the colour RNG.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Entry point for this demo (called from Main).
    void
    handleDemoTimer(DemoTimer t, long time)
    Periodic-timer handler: advances the animation one step.
    void
    handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
    Reply handler for the device registration.
    void
    Wires this protocol's event handlers and kicks off device registration.

    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
  • Field Details

    • LED_PORT

      public static final String LED_PORT
      Config key naming the GPIO data line the LED strip is wired to.
      See Also:
    • LED_PORT_DEFAULT

      public static final String LED_PORT_DEFAULT
      Default GPIO line (BCM 24) — coexists with a seated LoRa HAT.
      See Also:
    • ledAlias

      public static final String ledAlias
      Human-readable name we register the strip under, then verify in the reply.
      See Also:
  • Constructor Details

    • BabelChainableLedsRGBDemo

      public BabelChainableLedsRGBDemo()
      Sets the protocol identity shared by all demos (BabelDemo.PROTO_NAME / BabelDemo.PROTO_ID) and seeds the colour RNG. No Babel handlers are wired here — that happens in init(Properties), which Babel calls once the protocol is registered.
  • 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 kicks off device registration.

      The order matters: we register the reply (and timer) handlers before sending the request, so the RegisterIoTDeviceReply cannot arrive before its handler is in place. We then ask the DigitalOutputControlProtocol to register a chainable RGB strip on deviceLine; the actual handle comes back asynchronously in handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply, short). Finally we allocate the all-black colour buffer.

      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: advances the animation one step. Each tick shifts every LED's colour one slot toward the tail of the strip, then computes a new RGB colour for the head (LED 0) by sampling a sine wave at three 120° phase offsets (the classic rainbow trick). It then pushes the whole strip out via updateLedsColors().

      Babel calls this on its event loop because the timer was armed with setupPeriodicTimer once the device was ready.

    • handleRegisterIoTDeviceReply

      public void handleRegisterIoTDeviceReply(pt.unl.fct.di.tardis.babel.iot.api.replies.RegisterIoTDeviceReply rep, short protocolId)
      Reply handler for the device registration. Babel routes the RegisterIoTDeviceReply here once the control protocol has probed and claimed the hardware.

      The pattern: check RegisterIoTDeviceReply.isSuccessful(); on failure, bail out (the hardware is missing or busy). On success, keep the DeviceHandle — it is our only reference to the strip from now on — paint an initial frame, and arm the periodic timer that drives the animation. The alias check is a defensive sanity check that the reply we got is for the device we asked for.

    • execute

      public void execute() throws Exception
      Entry point for this demo (called from Main). Bootstraps the Babel runtime: grab the Babel singleton, load paradigmshift.config, instantiate the one control protocol this demo needs (DigitalOutputControlProtocol), register both it and this demo, then init them in dependency order (control protocol first so its request handlers exist before we send to it) and start the event loop.
      Specified by:
      execute in interface BabelDemo
      Throws:
      Exception