rocker

5d0f445 · gmorell · 2023-01-02 00:42

6 files +70 -1

Files changed

modified lib/lambent_ex/machine/steps/rainbow.ex
+1 −1
@@ -9,7 +9,7 @@ defmodule LambentEx.Machine.Steps.Rainbow do
9 9 # @behaviour LambentEx.Machine.Steps
10 10
11 11 @registry :lambent_steps
12 − @cls :chase
12 + @cls :rainbow
13 13
14 14 alias LambentEx.Machine.Steps.Rainbow.Cfg
15 15 alias LambentEx.Utils.Color
added lib/lambent_ex/machine/steps/rocker.ex
+69 −0
@@ -0,0 +1,69 @@
1 +defmodule LambentEx.Machine.Steps.Rocker.Cfg do
2 + # @enforce_keys [:h]
3 + defstruct [h: [0, 128], l: 1000, d: true, s: 255, v: 255, id: 0, status: 0, hues: []]
4 +end
5 +
6 +defmodule LambentEx.Machine.Steps.Rocker do
7 + @moduledoc false
8 + use GenServer
9 + # @behaviour LambentEx.Machine.Steps
10 +
11 + @registry :lambent_steps
12 + @cls :rocker
13 +
14 + alias LambentEx.Machine.Steps.Rocker.Cfg
15 + alias LambentEx.Utils.Color
16 +
17 + def generate_status_hsvs(hues, linger, true) do
18 + # dark
19 + hues |> Enum.map(fn h ->
20 + 0..255 |> Enum.map(fn x -> [h, 255, x] end) ++
21 + 0..linger |> Enum.map(fn x -> [h, 255, 255] end) ++
22 + 255..0 |> Enum.map(fn x -> [h, 255, x] end)
23 + end)
24 +
25 + end
26 +
27 + def generate_status_hsvs(hues, linger, false) do
28 + # light
29 + hues |> Enum.map(fn h ->
30 + 0..255 |> Enum.map(fn x -> [h, x, 255] end) ++
31 + 0..linger |> Enum.map(fn x -> [h, 255, 255] end) ++
32 + 255..0 |> Enum.map(fn x -> [h, x, 255] end)
33 + end)
34 + end
35 +
36 + defp n(opts) do
37 + "#{@cls}-#{opts[:name]}-#{opts[:id]}"
38 + end
39 +
40 + def start_link(opts) do
41 + GenServer.start_link(__MODULE__, opts, name: via_tuple(n(opts)))
42 + end
43 +
44 + def init(opts) do
45 + opts = opts |> Map.put(:status, opts.id)
46 + cfg = struct(%LambentEx.Machine.Steps.Rocker.Cfg{}, opts)
47 + cfg.hues = generate_status_hsvs(cfg.h, cfg.l, cfg.v)
48 + {:ok, cfg}
49 + end
50 +
51 + defp via_tuple(name) do
52 + {:via, Registry, {@registry, name}}
53 + end
54 +
55 + defp read(state) do
56 + Color.hsv2rgb(state.hues |> Enum.at(self.status))
57 + end
58 +
59 + def handle_cast(:step, state) do
60 + state =
61 + state
62 + |> Map.put(:status, (state.status + 1) |> Integer.mod(state.hues |> length))
63 + {:noreply, state}
64 + end
65 +
66 + def handle_call(:read, _from, state) do
67 + {:reply, read(state), state}
68 + end
69 +end

Parents: 1c47834