colorspace and UDP sender work

44c31cc · gmorell · 2022-11-28 18:08

6 files +61 -1

Files changed

modified lib/lambent_ex/scan/8266-7777.ex
+34 −1
@@ -3,6 +3,8 @@ defmodule LambentEx.Scan.ESP8266x7777 do
3 3 use GenServer
4 4 require Logger
5 5
6 + alias LambentEx.Utils.Color, as: C
7 +
6 8 @registry :lambent
7 9 @s "ꙭ📡"
8 10 @filtered_prefixes ["br", "docker", "lo"]
@@ -12,6 +14,7 @@ defmodule LambentEx.Scan.ESP8266x7777 do
12 14 end
13 15
14 16 def init(_opts) do
17 + {:ok, socket} = :gen_udp.open(0, [:binary])
15 18 Process.send_after(self(), :arp, :timer.seconds(1))
16 19 Process.send_after(self(), :scan, :timer.seconds(3))
17 20 interfaces = get_valid_interfaces()
@@ -20,7 +23,8 @@ defmodule LambentEx.Scan.ESP8266x7777 do
20 23 %{
21 24 interfaces: interfaces,
22 25 arps: %{},
23 − devices: %{"10:52:1c:02:d4:d2" => %{"ip" => {192, 168, 13, 226}, "name" => "AssFace3k"}}
26 + devices: %{"10:52:1c:02:d4:d2" => %{"ip" => {192, 168, 13, 226}, "name" => "AssFace3k", "ord" => :rgb}},
27 + socket: socket
24 28 }}
25 29 end
26 30
@@ -83,6 +87,35 @@ defmodule LambentEx.Scan.ESP8266x7777 do
83 87 end
84 88 end
85 89
90 + defp rgb_shift(stream, device) do
91 + chunked = Enum.chunk_by(stream, 3)
92 + case device |> Map.get(:ord) do
93 + :rgb -> chunked
94 + :grb -> chunked |> Enum.map(fn [r, g, b] -> [g, r, b] end)
95 + :rgbww -> chunked |> Enum.map(fn [r, g, b] -> [r, g, b, Enum.min(C.wrgb([r, g, b]), r)] end)
96 + :rgbnw -> chunked |> Enum.map(fn [r, g, b] -> [r, g, b, C.wrgb([r, g, b])] end)
97 + :rgbcw -> chunked |> Enum.map(fn [r, g, b] -> [r, g, b, Enum.min([C.wrgb([r, g, b]), b])] end)
98 + :rgbaw -> chunked |> Enum.map(fn [r, g, b] -> [r, g, b, C.argb([r, g, b]), C.wrgb([r, g, b])] end)
99 + :rgbxw -> chunked |> Enum.map(fn [r, g, b] -> [r, g, b, 0] end)
100 + nil -> chunked
101 + end
102 + |> List.flatten
103 + end
104 +
105 + defp rgb_stream(stream) do
106 + stream # TOOD FIGURE OUT HOW TO RECREATE THE PYTHON PACKING
107 + end
108 +
109 + @impl
110 + def handle_cast({:submit, dvc, stream}, state) do
111 + case state[:devices] |> Map.get(dvc) do
112 + nil -> {:noreply, state}
113 + device ->
114 + :gen_udp.send(state.socket, device[:ip], 7777, rgb_shift(stream) |> rgb_stream)
115 + {:noreply, state}
116 + end
117 + end
118 +
86 119 @impl true
87 120 def handle_info(:arp, state) do
88 121 Process.send_after(self(), :arp, :timer.seconds(15))
modified lib/lambent_ex/util/color.ex
+27 −0
@@ -1,4 +1,10 @@
1 1 defmodule LambentEx.Utils.Color do
2 +
3 + # Int-ify
4 + defp iiy(v) when is_float(v), do: trunc(v)
5 + defp iiy(v) when is_binary(v), do: Integer.parse(v)
6 + defp iiy(v), do: v
7 +
2 8 # floating point modulo 🌀💍💍💍
3 9 @spec _fmod(float, float) :: float
4 10 defp _fmod(mod, b) when mod >= b do
@@ -86,4 +92,25 @@ defmodule LambentEx.Utils.Color do
86 92 |> _hsv_to_rgb
87 93 |> sys_1_to_255
88 94 end
95 +
96 + # color generation and expansion functions
97 + def wrgb([r,g,b], coef \\ 2) do
98 + Enum.min([r,g,b]) / coef |> iiy
99 + end
100 +
101 + def wwrgb([r,g,b]) do
102 +
103 + end
104 +
105 + def argb([r,g,b]) do
106 + w = wrgb([r,g,b])
107 + a = r - w
108 + a = if (a > (g - w) * 2) do
109 + a = (g - w) * 2
110 + else
111 + a
112 + end
113 +
114 + a |> iiy
115 + end
89 116 end

Parents: 11845b4