Fledex.Animation.Coordinator (fledex v0.6.0)

View Source

The coordinator is a module that is responsible for running the coordinator function.

This module is rather simple:

  • It listens to events on the Fledex.Utils.PubSub.channel_state() channel
  • Passes the events to the function (:func) with which it was configured (or changed through the config/3 function)
  • And keeps track of some settings, that the function can use in it's processing

See config_t/0 for more details on how to implement the function.

Summary

Types

The configuration of a coordinator.

Functions

Returns a specification to start this module under a supervisor.

Change the config of the given coordinator

Start a new coordinator for the given led strip with the specified config

Stop the coordinator

Types

config_t()

@type config_t() :: %{
  type: :coordinator,
  options: keyword(),
  func: (broadcast_state :: any(), context :: map(), state :: keyword() ->
           new_state :: keyword())
}

The configuration of a coordinator.

The most important part is the :func which is a function that takes 3 parameters

  • broadcast_state: event (usually an atom),
  • context: descriptor to describe who has sent ou the event. It usually contains the strip_name and/or the animation_name and/or information about the effect.
  • state: contains the state of the coordinator. The first time the function is called the :options are passed in as state. The Coordinator should return a new_state.

You probably would impelment it something like this:

func: fn
  {:stop_start, %{strip_name: :john, animation_name: :doe}, state} ->
    # ... do something with the animations or effects
    # ... update the state if necessary
    state
  _ ->
    # do nothing
    state
end

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

config(strip_name, coordinator_name, config)

@spec config(atom(), atom(), config_t()) :: :ok

Change the config of the given coordinator

start_link(strip_name, animation_name, config)

@spec start_link(
  strip_name :: atom(),
  coordinator_name :: atom(),
  config :: config_t()
) ::
  GenServer.on_start()

Start a new coordinator for the given led strip with the specified config

stop(strip_name, coordinator_name)

@spec stop(atom(), atom()) :: :ok

Stop the coordinator