Skip to content

Commit

Permalink
provide current frame to live view mount
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Van Volkinburg committed Jul 29, 2020
1 parent df40f71 commit 13ef765
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 60 deletions.
8 changes: 2 additions & 6 deletions lib/rgb_matrix/animation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ defmodule RGBMatrix.Animation do
animation_config = config_module.new()
animation_state = animation_type.new(leds, animation_config)

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

animation
end

@doc """
Expand Down Expand Up @@ -114,8 +112,6 @@ defmodule RGBMatrix.Animation do

config = config_module.update(config, params)

animation = %{animation | config: config}
:ok = Xebow.update_animation_config(animation)
animation
%{animation | config: config}
end
end
10 changes: 5 additions & 5 deletions lib/rgb_matrix/animation/random_keypresses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ defmodule RGBMatrix.Animation.RandomKeypresses do
def new(leds, _config) do
led_ids = Enum.map(leds, & &1.id)

%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
13 changes: 7 additions & 6 deletions lib/rgb_matrix/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ defmodule RGBMatrix.Engine do
GenServer.cast(__MODULE__, {:set_animation, animation_type})
end

@typep frame :: %{LED.t() => RGBMatrix.any_color_model()}

@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 Down Expand Up @@ -185,7 +187,6 @@ defmodule RGBMatrix.Engine do

@impl GenServer
def handle_cast({:set_animation, animation}, state) do
# state = set_animation(state, animation_type)
state =
%State{state | animation: animation}
|> schedule_next_render(0)
Expand All @@ -208,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 @@ -225,7 +226,7 @@ defmodule RGBMatrix.Engine do
%State{state | animation: animation}
|> inform_configurables()

{:reply, :ok, state}
{:reply, Xebow.update_animation_config(animation), state}
end

@impl GenServer
Expand Down
21 changes: 11 additions & 10 deletions lib/xebow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Xebow do
"""

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

@leds [
LED.new(:l001, 0, 0),
Expand Down Expand Up @@ -47,14 +48,14 @@ defmodule Xebow do

@spec start_link([]) :: GenServer.on_start()
def start_link([]) do
GenServer.start_link(__MODULE__, RGBMatrix.Animation.types(), name: __MODULE__)
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() :: RGBMatrix.Animation.Config.t()
@spec get_animation_config() :: {Animation.Config.t(), keyword(Animation.Config.t())}
def get_animation_config do
GenServer.call(__MODULE__, :get_animation_config)
end
Expand All @@ -78,7 +79,7 @@ defmodule Xebow do
@doc """
Updates the animation configuration for the current animation
"""
@spec update_animation_config(RGBMatrix.Animation.type()) :: :ok | :error
@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
Expand All @@ -92,7 +93,7 @@ defmodule Xebow do
|> Enum.map(&initialize_animation/1)

[current | _] = active_animations
RGBMatrix.Engine.set_animation(current)
Engine.set_animation(current)
state = {active_animations, []}

{:ok, state}
Expand All @@ -101,7 +102,7 @@ defmodule Xebow do
@impl GenServer
def handle_call(:get_animation_config, _caller, state) do
{[current | _rest], _previous} = state
{:reply, RGBMatrix.Animation.get_config(current), state}
{:reply, Animation.get_config(current), state}
end

@impl GenServer
Expand All @@ -115,11 +116,11 @@ defmodule Xebow do
case state do
{[current | []], previous} ->
remaining_next = Enum.reverse([current | previous])
RGBMatrix.Engine.set_animation(hd(remaining_next))
Engine.set_animation(hd(remaining_next))
{:noreply, {remaining_next, []}}

{[current | remaining_next], previous} ->
RGBMatrix.Engine.set_animation(hd(remaining_next))
Engine.set_animation(hd(remaining_next))
{:noreply, {remaining_next, [current | previous]}}
end
end
Expand All @@ -129,16 +130,16 @@ defmodule Xebow do
case state do
{remaining_next, []} ->
[next | remaining_previous] = Enum.reverse(remaining_next)
RGBMatrix.Engine.set_animation(next)
Engine.set_animation(next)
{:noreply, {[next], remaining_previous}}

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

defp initialize_animation(animation_type) do
RGBMatrix.Animation.new(animation_type, @leds)
Animation.new(animation_type, @leds)
end
end
2 changes: 1 addition & 1 deletion lib/xebow/keyboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ defmodule Xebow.Keyboard do
state = %{
pins: pins,
keyboard_state: keyboard_state,
hid: hid,
hid: hid
}

{:ok, state}
Expand Down
2 changes: 1 addition & 1 deletion lib/xebow/leds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Xebow.LEDs do
defp register_with_engine!(spidev) do
pid = self()

{:ok, paint_fn} =
{:ok, paint_fn, _frame} =
Engine.register_paintable(fn frame ->
if Process.alive?(pid) do
paint(spidev, frame)
Expand Down
40 changes: 9 additions & 31 deletions lib/xebow_web/live/matrix_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ defmodule XebowWeb.MatrixLive do
initial_assigns = [
leds: make_view_leds(@black_frame),
config: config,
config_schema: config_schema,
config_schema: config_schema
]

initial_assigns =
if connected?(socket) do
{paint_fn, config_fn} = register_with_engine!()
{paint_fn, config_fn, frame} = register_with_engine!()

Keyword.merge(initial_assigns, paint_fn: paint_fn, config_fn: config_fn)
Keyword.merge(initial_assigns,
paint_fn: paint_fn,
config_fn: config_fn,
leds: make_view_leds(frame)
)
else
initial_assigns
end
Expand Down Expand Up @@ -76,38 +80,12 @@ defmodule XebowWeb.MatrixLive do
def handle_event("next_animation", %{}, socket) do
Xebow.next_animation()
{:noreply, socket}
# next_index = socket.assigns.current_animation_index + 1

# next_index =
# case next_index < Enum.count(socket.assigns.animation_types) do
# true -> next_index
# _ -> 0
# end

# animation_type = Enum.at(socket.assigns.animation_types, next_index)

# RGBMatrix.Engine.set_animation(animation_type)

# {:noreply, assign(socket, current_animation_index: next_index)}
end

@impl Phoenix.LiveView
def handle_event("previous_animation", %{}, socket) do
Xebow.previous_animation()
{:noreply, socket}
# previous_index = socket.assigns.current_animation_index - 1

# previous_index =
# case previous_index < 0 do
# true -> Enum.count(socket.assigns.animation_types) - 1
# _ -> previous_index
# end

# animation_type = Enum.at(socket.assigns.animation_types, previous_index)

# RGBMatrix.Engine.set_animation(animation_type)

# {:noreply, assign(socket, current_animation_index: previous_index)}
end

@impl Phoenix.LiveView
Expand All @@ -119,7 +97,7 @@ defmodule XebowWeb.MatrixLive do
defp register_with_engine! do
pid = self()

{:ok, paint_fn} =
{:ok, paint_fn, frame} =
Engine.register_paintable(fn frame ->
if Process.alive?(pid) do
send(pid, {:render, frame})
Expand All @@ -139,7 +117,7 @@ defmodule XebowWeb.MatrixLive do
end
end)

{paint_fn, config_fn}
{paint_fn, config_fn, frame}
end

defp make_view_leds(frame) do
Expand Down

0 comments on commit 13ef765

Please sign in to comment.