Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move animation management out of Engine #95

Merged
merged 8 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/rgb_matrix/animation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule RGBMatrix.Animation do
}
defstruct [:type, :config, :state]

@callback new(leds :: [LED.t()], config :: Config.t()) :: {render_in, animation_state}
@callback new(leds :: [LED.t()], config :: Config.t()) :: animation_state
@callback render(state :: animation_state, config :: Config.t()) ::
{render_in, [RGBMatrix.any_color_model()], animation_state}
@callback interact(state :: animation_state, config :: Config.t(), led :: LED.t()) ::
Expand Down Expand Up @@ -59,19 +59,17 @@ defmodule RGBMatrix.Animation do
@doc """
Returns an animation's initial state.
"""
@spec new(animation_type :: type, leds :: [LED.t()]) :: {render_in, t}
@spec new(animation_type :: type, leds :: [LED.t()]) :: t
def new(animation_type, leds) do
config_module = Module.concat([animation_type, "Config"])
animation_config = config_module.new()
{render_in, animation_state} = animation_type.new(leds, animation_config)
animation_state = animation_type.new(leds, animation_config)

animation = %__MODULE__{
%__MODULE__{
type: animation_type,
config: animation_config,
state: animation_state
}

{render_in, animation}
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/breathing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule RGBMatrix.Animation.Breathing do
def new(leds, _config) do
color = HSV.new(40, 100, 100)
led_ids = Enum.map(leds, & &1.id)
{0, %State{color: color, tick: 0, speed: 100, led_ids: led_ids}}
%State{color: color, tick: 0, speed: 100, led_ids: led_ids}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/cycle_all.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule RGBMatrix.Animation.CycleAll do
@impl true
def new(leds, _config) do
led_ids = Enum.map(leds, & &1.id)
{0, %State{tick: 0, speed: 100, led_ids: led_ids}}
%State{tick: 0, speed: 100, led_ids: led_ids}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/hue_wave.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule RGBMatrix.Animation.HueWave do
@impl true
def new(leds, config) do
steps = 360 / config.width
{0, %State{tick: 0, leds: leds, steps: steps}}
%State{tick: 0, leds: leds, steps: steps}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/pinwheel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule RGBMatrix.Animation.Pinwheel do

@impl true
def new(leds, _config) do
{0, %State{tick: 0, speed: 100, leds: leds, center: determine_center(leds)}}
%State{tick: 0, speed: 100, leds: leds, center: determine_center(leds)}
end

defp determine_center(leds) do
Expand Down
11 changes: 5 additions & 6 deletions lib/rgb_matrix/animation/random_keypresses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ defmodule RGBMatrix.Animation.RandomKeypresses do
def new(leds, _config) do
led_ids = Enum.map(leds, & &1.id)

{0,
%State{
led_ids: led_ids,
# NOTE: as to not conflict with possible led ID of `:all`
dirty: {:all}
}}
%State{
led_ids: led_ids,
# NOTE: as to not conflict with possible led ID of `:all`
dirty: {:all}
}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/random_solid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule RGBMatrix.Animation.RandomSolid do

@impl true
def new(leds, _config) do
{0, %State{led_ids: Enum.map(leds, & &1.id)}}
%State{led_ids: Enum.map(leds, & &1.id)}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/solid_color.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule RGBMatrix.Animation.SolidColor do
@impl true
def new(leds, _config) do
color = HSV.new(120, 100, 100)
{0, %State{color: color, led_ids: Enum.map(leds, & &1.id)}}
%State{color: color, led_ids: Enum.map(leds, & &1.id)}
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/animation/solid_reactive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule RGBMatrix.Animation.SolidReactive do
@impl true
def new(leds, _config) do
color = HSV.new(190, 100, 100)
{0, %State{first_render: true, paused: false, tick: 0, color: color, leds: leds, hits: %{}}}
%State{first_render: true, paused: false, tick: 0, color: color, leds: leds, hits: %{}}
end

@impl true
Expand Down
63 changes: 23 additions & 40 deletions lib/rgb_matrix/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ defmodule RGBMatrix.Engine do
- `initial_animation` - The Animation type to initialize and play when the
engine starts.
"""
@spec start_link({leds :: [LED.t()], initial_animation_type :: Animation.type()}) ::
@spec start_link(leds :: [LED.t()]) ::
GenServer.on_start()
vanvoljg marked this conversation as resolved.
Show resolved Hide resolved
def start_link({leds, initial_animation_type}) do
GenServer.start_link(__MODULE__, {leds, initial_animation_type}, name: __MODULE__)
def start_link(leds) do
GenServer.start_link(__MODULE__, leds, name: __MODULE__)
end

@doc """
Expand All @@ -41,15 +41,17 @@ defmodule RGBMatrix.Engine do
GenServer.cast(__MODULE__, {:set_animation, animation_type})
end

@typep frame :: %{LED.t() => RGBMatrix.any_color_model()}
vanvoljg marked this conversation as resolved.
Show resolved Hide resolved

@doc """
Register a paint function for the engine to send frames to.

This function is idempotent.
"""
@spec register_paintable(paint_fn :: function) :: {:ok, function}
@spec register_paintable(paint_fn :: function) :: {:ok, function, frame}
def register_paintable(paint_fn) do
:ok = GenServer.call(__MODULE__, {:register_paintable, paint_fn})
{:ok, paint_fn}
{:ok, frame} = GenServer.call(__MODULE__, {:register_paintable, paint_fn})
{:ok, paint_fn, frame}
end

@doc """
Expand All @@ -71,14 +73,6 @@ defmodule RGBMatrix.Engine do
GenServer.cast(__MODULE__, {:interact, led})
end

@doc """
Retrieves the current animation's configuration and configuration schema.
"""
@spec get_animation_config() :: {config :: any, config_schema :: any}
def get_animation_config do
GenServer.call(__MODULE__, :get_animation_config)
end

@doc """
Updates the current animation's configuration.
"""
Expand Down Expand Up @@ -113,18 +107,16 @@ defmodule RGBMatrix.Engine do
# Server

@impl GenServer
def init({leds, initial_animation_type}) do
def init(leds) do
black = Chameleon.HSV.new(0, 0, 0)
frame = Map.new(leds, &{&1.id, black})

state =
%State{
leds: leds,
last_frame: frame,
paintables: MapSet.new(),
configurables: MapSet.new()
}
|> init_and_set_animation(initial_animation_type)
state = %State{
leds: leds,
last_frame: frame,
paintables: MapSet.new(),
configurables: MapSet.new()
}

{:ok, state}
end
Expand All @@ -139,14 +131,6 @@ defmodule RGBMatrix.Engine do
%State{state | paintables: paintables}
end

defp init_and_set_animation(state, animation_type) do
{render_in, animation} = Animation.new(animation_type, state.leds)

%State{state | animation: animation}
|> schedule_next_render(render_in)
|> inform_configurables()
end

defp schedule_next_render(state, :ignore) do
state
end
Expand Down Expand Up @@ -202,8 +186,12 @@ defmodule RGBMatrix.Engine do
end

@impl GenServer
def handle_cast({:set_animation, animation_type}, state) do
state = init_and_set_animation(state, animation_type)
def handle_cast({:set_animation, animation}, state) do
state =
%State{state | animation: animation}
|> schedule_next_render(0)
|> inform_configurables()

{:noreply, state}
end

Expand All @@ -221,7 +209,7 @@ defmodule RGBMatrix.Engine do
@impl GenServer
def handle_call({:register_paintable, paint_fn}, _from, state) do
state = add_paintable(paint_fn, state)
{:reply, :ok, state}
{:reply, {:ok, state.last_frame}, state}
end

@impl GenServer
Expand All @@ -230,11 +218,6 @@ defmodule RGBMatrix.Engine do
{:reply, :ok, state}
end

@impl GenServer
def handle_call(:get_animation_config, _from, state) do
{:reply, Animation.get_config(state.animation), state}
end

@impl GenServer
def handle_call({:update_animation_config, params}, _from, state) do
animation = Animation.update_config(state.animation, params)
Expand All @@ -243,7 +226,7 @@ defmodule RGBMatrix.Engine do
%State{state | animation: animation}
|> inform_configurables()

{:reply, :ok, state}
{:reply, Xebow.update_animation_config(animation), state}
vanvoljg marked this conversation as resolved.
Show resolved Hide resolved
end

@impl GenServer
Expand Down
107 changes: 106 additions & 1 deletion lib/xebow.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
defmodule Xebow do
@moduledoc false
@moduledoc """
Xebow is an Elixir-based firmware for keyboards. Currently, it is working on the Raspberry Pi0
Keybow kit.
"""

alias Layout.{Key, LED}
alias RGBMatrix.{Animation, Engine}

@leds [
LED.new(:l001, 0, 0),
Expand Down Expand Up @@ -37,4 +41,105 @@ defmodule Xebow do

@spec layout() :: Layout.t()
def layout, do: @layout

use GenServer

# Client Implementations:

@spec start_link([]) :: GenServer.on_start()
def start_link([]) do
GenServer.start_link(__MODULE__, Animation.types(), name: __MODULE__)
end

@doc """
Gets the current animation configuration. This retrievs current values, which
allows for changes to be made with `update_animation_config/2`
"""
@spec get_animation_config() :: {Animation.Config.t(), keyword(Animation.Config.t())}
def get_animation_config do
GenServer.call(__MODULE__, :get_animation_config)
end

@doc """
Switches to the next active animation
"""
@spec next_animation() :: :ok
def next_animation do
GenServer.cast(__MODULE__, :next_animation)
end

@doc """
Switches to the previous active animation
"""
@spec previous_animation() :: :ok
def previous_animation do
GenServer.cast(__MODULE__, :previous_animation)
end

@doc """
Updates the animation configuration for the current animation
"""
@spec update_animation_config(Animation.t()) :: :ok | :error
def update_animation_config(animation_with_config) do
GenServer.call(__MODULE__, {:update_animation_config, animation_with_config})
end

# Server Implementations:

@impl GenServer
def init(types) do
active_animations =
types
|> Enum.map(&initialize_animation/1)

[current | _] = active_animations
Engine.set_animation(current)
state = {active_animations, []}
vanvoljg marked this conversation as resolved.
Show resolved Hide resolved

{:ok, state}
end

@impl GenServer
def handle_call(:get_animation_config, _caller, state) do
{[current | _rest], _previous} = state
{:reply, Animation.get_config(current), state}
end

@impl GenServer
def handle_call({:update_animation_config, animation_with_config}, _caller, state) do
vanvoljg marked this conversation as resolved.
Show resolved Hide resolved
{[_current | rest], previous} = state
{:reply, :ok, {[animation_with_config | rest], previous}}
end

@impl GenServer
def handle_cast(:next_animation, state) do
case state do
{[current | []], previous} ->
remaining_next = Enum.reverse([current | previous])
Engine.set_animation(hd(remaining_next))
{:noreply, {remaining_next, []}}

{[current | remaining_next], previous} ->
Engine.set_animation(hd(remaining_next))
{:noreply, {remaining_next, [current | previous]}}
end
end

@impl GenServer
def handle_cast(:previous_animation, state) do
case state do
{remaining_next, []} ->
[next | remaining_previous] = Enum.reverse(remaining_next)
Engine.set_animation(next)
{:noreply, {[next], remaining_previous}}

{remaining_next, [next | remaining_previous]} ->
Engine.set_animation(next)
{:noreply, {[next | remaining_next], remaining_previous}}
end
end

defp initialize_animation(animation_type) do
Animation.new(animation_type, @leds)
end
end
5 changes: 3 additions & 2 deletions lib/xebow/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Xebow.Application do
use Application

@leds Xebow.layout() |> Layout.leds()
@animation_type RGBMatrix.Animation.types() |> List.first()

def start(_type, _args) do
# See https://hexdocs.pm/elixir/Supervisor.html
Expand All @@ -18,7 +17,9 @@ defmodule Xebow.Application do
# Children for all targets
# Starts a worker by calling: Xebow.Worker.start_link(arg)
# {Xebow.Worker, arg},
{RGBMatrix.Engine, {@leds, @animation_type}},
# Engine must be started before Xebow
{RGBMatrix.Engine, @leds},
Xebow,
# Phoenix:
XebowWeb.Telemetry,
{Phoenix.PubSub, name: Xebow.PubSub},
Expand Down
Loading