From 13ef76530719611ecced0db4bb033e6a72bc9a35 Mon Sep 17 00:00:00 2001 From: Jesse Van Volkinburg Date: Wed, 29 Jul 2020 00:29:19 -0700 Subject: [PATCH] provide current frame to live view mount --- lib/rgb_matrix/animation.ex | 8 +--- lib/rgb_matrix/animation/random_keypresses.ex | 10 ++--- lib/rgb_matrix/engine.ex | 13 +++--- lib/xebow.ex | 21 +++++----- lib/xebow/keyboard.ex | 2 +- lib/xebow/leds.ex | 2 +- lib/xebow_web/live/matrix_live.ex | 40 +++++-------------- 7 files changed, 36 insertions(+), 60 deletions(-) diff --git a/lib/rgb_matrix/animation.ex b/lib/rgb_matrix/animation.ex index 4418793..2245619 100644 --- a/lib/rgb_matrix/animation.ex +++ b/lib/rgb_matrix/animation.ex @@ -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 """ @@ -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 diff --git a/lib/rgb_matrix/animation/random_keypresses.ex b/lib/rgb_matrix/animation/random_keypresses.ex index 33679b6..5823b84 100644 --- a/lib/rgb_matrix/animation/random_keypresses.ex +++ b/lib/rgb_matrix/animation/random_keypresses.ex @@ -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 diff --git a/lib/rgb_matrix/engine.ex b/lib/rgb_matrix/engine.ex index 2c76f28..5684c05 100644 --- a/lib/rgb_matrix/engine.ex +++ b/lib/rgb_matrix/engine.ex @@ -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 """ @@ -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) @@ -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 @@ -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 diff --git a/lib/xebow.ex b/lib/xebow.ex index 16750b5..6e514a3 100644 --- a/lib/xebow.ex +++ b/lib/xebow.ex @@ -5,6 +5,7 @@ defmodule Xebow do """ alias Layout.{Key, LED} + alias RGBMatrix.{Animation, Engine} @leds [ LED.new(:l001, 0, 0), @@ -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 @@ -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 @@ -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} @@ -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 @@ -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 @@ -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 diff --git a/lib/xebow/keyboard.ex b/lib/xebow/keyboard.ex index 05adf29..fa93aa1 100644 --- a/lib/xebow/keyboard.ex +++ b/lib/xebow/keyboard.ex @@ -140,7 +140,7 @@ defmodule Xebow.Keyboard do state = %{ pins: pins, keyboard_state: keyboard_state, - hid: hid, + hid: hid } {:ok, state} diff --git a/lib/xebow/leds.ex b/lib/xebow/leds.ex index 79de259..1c39abc 100644 --- a/lib/xebow/leds.ex +++ b/lib/xebow/leds.ex @@ -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) diff --git a/lib/xebow_web/live/matrix_live.ex b/lib/xebow_web/live/matrix_live.ex index 2ebe9ca..d299c12 100644 --- a/lib/xebow_web/live/matrix_live.ex +++ b/lib/xebow_web/live/matrix_live.ex @@ -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 @@ -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 @@ -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}) @@ -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