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

Simon! #73

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion lib/rgb_matrix/animation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ defmodule RGBMatrix.Animation do
| __MODULE__.SolidColor
| __MODULE__.Breathing
| __MODULE__.SolidReactive
| __MODULE__.Simon

@doc """
Returns a list of the available types of animations.
Expand All @@ -52,7 +53,8 @@ defmodule RGBMatrix.Animation do
__MODULE__.RandomKeypresses,
__MODULE__.SolidColor,
__MODULE__.Breathing,
__MODULE__.SolidReactive
__MODULE__.SolidReactive,
__MODULE__.Simon
]
end

Expand Down
134 changes: 134 additions & 0 deletions lib/rgb_matrix/animation/simon.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
defmodule RGBMatrix.Animation.Simon do
@moduledoc """
An interactive "Simon" game.
"""

alias Chameleon.HSV
alias RGBMatrix.Animation

use Animation

defmodule Config do
@moduledoc false
use RGBMatrix.Animation.Config
end

defmodule State do
@moduledoc false
defstruct [:leds, :simon_sequence, :state]
end

@black Chameleon.RGB.new(0, 0, 0)
@red Chameleon.RGB.new(255, 0, 0)

@impl true
def new(leds, _config) do
state =
%State{leds: leds}
doughsay marked this conversation as resolved.
Show resolved Hide resolved
|> init_sequence()

{0, state}
end

doughsay marked this conversation as resolved.
Show resolved Hide resolved
@impl true
def render(%{state: {:playing_sequence, []}} = state, _config) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it an %Animation{} that's passed to render?

Suggested change
def render(%{state: {:playing_sequence, []}} = state, _config) do
def render(%{state: {:playing_sequence, []}} = animation, _config) do

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is also the source of some confusion from your previous code review of the refactor (this is why the callbacks use any for state, not Animation.t(). I wanted to avoid passing in the actual animation struct in the refactor to keep things simpler (i.e. the animation implementer knows exactly what they're getting because they defined it themselves.)

This is open for discussion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I might have contributed to that confusion here, sorry! There's actually a field called state inside the State struct. So starting from the animation struct it actually looks like animation.state.state 😅

colors = state.leds |> Enum.map(fn led -> {led.id, @black} end)

state = %{state | state: {:expecting_input, state.simon_sequence}}

{:never, colors, state}
end

@impl true
def render(%{state: {:playing_sequence, [{led, color} | rest]}} = state, _config) do
colors =
Enum.map(state.leds, fn
^led -> {led.id, color}
other_led -> {other_led.id, @black}
end)

state = %{state | state: {:playing_sequence, rest}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might have loose type checking unless it's %State{}.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this means?


{1_000, colors, state}
doughsay marked this conversation as resolved.
Show resolved Hide resolved
end

@impl true
def render(%{state: {:feedback, [{led, color} | rest]}} = state, _config) do
colors =
Enum.map(state.leds, fn
^led -> {led.id, color}
other_led -> {other_led.id, @black}
end)

state =
case rest do
[] -> extend_sequence(state)
rest -> %{state | state: {:expecting_input, rest}}
end

{500, colors, state}
doughsay marked this conversation as resolved.
Show resolved Hide resolved
end

@impl true
def render(%{state: :lost} = state, _config) do
colors = state.leds |> Enum.map(fn led -> {led.id, @red} end)

state =
state
|> init_sequence()

{2_000, colors, state}
end

@impl true
def render(state, _config) do
colors = state.leds |> Enum.map(fn led -> {led.id, @black} end)

{:never, colors, state}
end

@impl true
def interact(
%{state: {:expecting_input, [{led, _color} | _rest] = sequence}} = state,
_config,
led
) do
state = %{state | state: {:feedback, sequence}}

{0, state}
end

@impl true
def interact(%{state: {:expecting_input, [_x | _rest]}} = state, _config, _y) do
state = %{state | state: :lost}

{0, state}
end

@impl true
def interact(state, _config, _led) do
{:ignore, state}
end

defp random_color do
HSV.new((:rand.uniform() * 360) |> trunc(), 100, 100)
end

defp init_sequence(state) do
doughsay marked this conversation as resolved.
Show resolved Hide resolved
led = Enum.random(state.leds)
color = random_color()
%State{state | simon_sequence: [{led, color}], state: {:playing_sequence, [{led, color}]}}
doughsay marked this conversation as resolved.
Show resolved Hide resolved
end

defp extend_sequence(state) do
led = Enum.random(state.leds)
color = random_color()
doughsay marked this conversation as resolved.
Show resolved Hide resolved
sequence = state.simon_sequence ++ [{led, color}]

%{
state
| simon_sequence: sequence,
state: {:playing_sequence, sequence}
doughsay marked this conversation as resolved.
Show resolved Hide resolved
}
end
end