Skip to content

Commit

Permalink
Keyboard to Effect interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
doughsay committed Jul 5, 2020
1 parent 3b376bb commit 9a93177
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lib/rgb_matrix/effect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule RGBMatrix.Effect do
@callback new(leds :: list(LED.t()), config :: any) :: {render_in, any}
@callback render(state :: any, config :: any) ::
{list(RGBMatrix.any_color_model()), render_in, any}
@callback key_pressed(state :: any, config :: any, led :: LED.t()) :: {render_in, any}
@callback interact(state :: any, config :: any, led :: LED.t()) :: {render_in, any}

@type t :: %__MODULE__{
type: type,
Expand Down Expand Up @@ -78,11 +78,11 @@ defmodule RGBMatrix.Effect do
end

@doc """
Sends a key pressed event to an effect.
Sends an interaction event to an effect.
"""
@spec key_pressed(effect :: t, led :: LED.t()) :: {render_in, t}
def key_pressed(effect, led) do
{render_in, effect_state} = effect.type.key_pressed(effect.state, effect.config, led)
@spec interact(effect :: t, led :: LED.t()) :: {render_in, t}
def interact(effect, led) do
{render_in, effect_state} = effect.type.interact(effect.state, effect.config, led)
{render_in, %{effect | state: effect_state}}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/breathing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule RGBMatrix.Effect.Breathing do
end

@impl true
def key_pressed(state, _config, _led) do
def interact(state, _config, _led) do
{:ignore, state}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/cycle_all.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule RGBMatrix.Effect.CycleAll do
end

@impl true
def key_pressed(state, _config, _led) do
def interact(state, _config, _led) do
{:ignore, state}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/hue_wave.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ defmodule RGBMatrix.Effect.HueWave do
end

@impl true
def key_pressed(state, _config, _led) do
def interact(state, _config, _led) do
{:ignore, state}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/pinwheel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule RGBMatrix.Effect.Pinwheel do
end

@impl true
def key_pressed(state, _config, %LED{x: x, y: y}) do
def interact(state, _config, %LED{x: x, y: y}) do
{:ignore, %{state | center: %{x: x, y: y}}}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/random_keypresses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule RGBMatrix.Effect.RandomKeypresses do
end

@impl true
def key_pressed(state, _config, led) do
def interact(state, _config, led) do
{0, %{state | dirty: led.id}}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/random_solid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule RGBMatrix.Effect.RandomSolid do
end

@impl true
def key_pressed(state, _config, _led) do
def interact(state, _config, _led) do
{0, state}
end
end
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/solid_color.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule RGBMatrix.Effect.SolidColor do
end

@impl true
def key_pressed(state, _config, _led) do
def interact(state, _config, _led) do
{:ignore, state}
end
end
9 changes: 5 additions & 4 deletions lib/rgb_matrix/effect/solid_reactive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@ defmodule RGBMatrix.Effect.SolidReactive do
description: """
The speed at which the hue shifts back to base.
"""
field :speed, :integer, default: 4, min: 0, max: 32
field(:speed, :integer, default: 4, min: 0, max: 32)

@doc name: "Distance",
description: """
The distance that the hue shifts on key-press.
"""
field :distance, :integer, default: 180, min: 0, max: 360, step: 10
field(:distance, :integer, default: 180, min: 0, max: 360, step: 10)

@doc name: "Direction",
description: """
The direction (through the color wheel) that the hue shifts on key-press.
"""
field :direction, :option,
field(:direction, :option,
default: :random,
options: [
:random,
:negative,
:positive
]
)
end

defmodule State do
Expand Down Expand Up @@ -95,7 +96,7 @@ defmodule RGBMatrix.Effect.SolidReactive do
end

@impl true
def key_pressed(state, config, led) do
def interact(state, config, led) do
direction = direction_modifier(config.direction)

render_in =
Expand Down
2 changes: 1 addition & 1 deletion lib/rgb_matrix/effect/splash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule RGBMatrix.Effect.Splash do
end

@impl true
def key_pressed(state, _config, led) do
def interact(state, _config, led) do
# {:ignore, %{state | hits: Map.put(state.hits, led, state.tick)}}
{:ignore, %{state | hits: %{led => state.tick}}}
end
Expand Down
14 changes: 14 additions & 0 deletions lib/rgb_matrix/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ defmodule RGBMatrix.Engine do
GenServer.call(__MODULE__, {:unregister_paintable, paintable})
end

@spec interact(led :: LED.t()) :: :ok
def interact(led) do
GenServer.cast(__MODULE__, {:interact, led})
end

# Server

@impl GenServer
Expand Down Expand Up @@ -157,6 +162,15 @@ defmodule RGBMatrix.Engine do
{:noreply, state}
end

@impl GenServer
def handle_cast({:interact, led}, state) do
{render_in, effect} = Effect.interact(state.effect, led)
state = schedule_next_render(state, render_in)
state = %State{state | effect: effect}

{:noreply, %State{state | effect: effect}}
end

@impl GenServer
def handle_call({:register_paintable, paintable}, _from, state) do
state = add_paintable(paintable, state)
Expand Down
10 changes: 9 additions & 1 deletion lib/xebow/keyboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Xebow.Keyboard do
use GenServer

alias Circuits.GPIO
alias RGBMatrix.Effect
alias RGBMatrix.{Effect, Engine}

# maps the physical GPIO pins to key IDs
# TODO: re-number these keys so they map to the keyboard in X/Y natural order,
Expand Down Expand Up @@ -215,13 +215,21 @@ defmodule Xebow.Keyboard do
0 ->
Logger.debug("key pressed #{key_id}")
AFK.State.press_key(keyboard_state, key_id)
rgb_matrix_interact(key_id)

1 ->
Logger.debug("key released #{key_id}")
AFK.State.release_key(keyboard_state, key_id)
end
end

defp rgb_matrix_interact(key_id) do
case Layout.led_for_key(Xebow.layout(), key_id) do
nil -> :noop
led -> Engine.interact(led)
end
end

# Custom Key Functions

def flash(color) do
Expand Down

0 comments on commit 9a93177

Please sign in to comment.