-
Notifications
You must be signed in to change notification settings - Fork 10
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
refactor animation cycling #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,8 +73,25 @@ defmodule Xebow.Keys do | |
|
||
# Client | ||
|
||
def start_link([], opts \\ []) do | ||
GenServer.start_link(__MODULE__, [], opts) | ||
@spec start_link(any()) :: GenServer.on_start() | ||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
@doc """ | ||
Cycle to the next animation | ||
""" | ||
@spec next_animation() :: :ok | ||
def next_animation do | ||
GenServer.cast(__MODULE__, :next_animation) | ||
end | ||
|
||
@doc """ | ||
Cycle to the previous animation | ||
""" | ||
@spec previous_animation() :: :ok | ||
def previous_animation do | ||
GenServer.cast(__MODULE__, :previous_animation) | ||
end | ||
|
||
# Server | ||
|
@@ -102,20 +119,65 @@ defmodule Xebow.Keys do | |
poll_timer_ms = 15 | ||
:timer.send_interval(poll_timer_ms, self(), :update_pin_values) | ||
|
||
animations = | ||
Animation.types() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ (informational, no action required) It would be nice to eventually move the runtime animation types into a file that handles application logic ( Related: #49 (comment) |
||
|> Enum.map(&Animation.new(type: &1)) | ||
|
||
{:ok, | ||
%{ | ||
pins: pins, | ||
keyboard_state: keyboard_state, | ||
hid: hid | ||
hid: hid, | ||
animations: animations, | ||
current_animation_index: 0 | ||
Comment on lines
+131
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️ (informational, no action required) Keeping this data in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my possibly related documentation update calling out the fact that |
||
}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast(:next_animation, state) do | ||
next_index = state.current_animation_index + 1 | ||
|
||
next_index = | ||
case next_index < Enum.count(state.animations) do | ||
true -> next_index | ||
_ -> 0 | ||
end | ||
|
||
animation = Enum.at(state.animations, next_index) | ||
|
||
Xebow.Engine.play_animation(animation) | ||
|
||
state = %{state | current_animation_index: next_index} | ||
|
||
{:noreply, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast(:previous_animation, state) do | ||
previous_index = state.current_animation_index - 1 | ||
|
||
previous_index = | ||
case previous_index < 0 do | ||
true -> Enum.count(state.animations) - 1 | ||
_ -> previous_index | ||
end | ||
|
||
animation = Enum.at(state.animations, previous_index) | ||
|
||
Xebow.Engine.play_animation(animation) | ||
|
||
state = %{state | current_animation_index: previous_index} | ||
|
||
{:noreply, state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_info({:hid_report, hid_report}, state) do | ||
IO.binwrite(state.hid, hid_report) | ||
{:noreply, state} | ||
end | ||
|
||
@impl GenServer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought you only needed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're right. I'm fine either way. With @vanvoljg's approach it's harder to forget to add it back in if one is removed during refactoring. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it mostly made it clearer that it's a callback. I can omit in the future them if they make things too cluttered. |
||
def handle_info(:update_pin_values, state) do | ||
new_pins = | ||
Enum.map(state.pins, fn {pin_number, pin_ref, old_value} -> | ||
|
@@ -158,14 +220,6 @@ defmodule Xebow.Keys do | |
Xebow.Engine.play_animation(animation, async: false) | ||
end | ||
|
||
def next_animation do | ||
Xebow.Engine.next_animation() | ||
end | ||
|
||
def previous_animation do | ||
Xebow.Engine.previous_animation() | ||
end | ||
|
||
def start_wifi_wizard do | ||
case VintageNetWizard.run_wizard() do | ||
:ok -> flash("green") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nickdichev can you please make sure I didn't break any of your logic?