diff --git a/morse/lib/morse_server.ex b/morse/lib/morse_server.ex new file mode 100644 index 0000000..25f207b --- /dev/null +++ b/morse/lib/morse_server.ex @@ -0,0 +1,37 @@ +defmodule MorseServer do + use GenServer + use Application + + @impl true + def start(_type, _args) do + start_link() + end + + def start_link do + GenServer.start_link(__MODULE__, :off, name: __MODULE__) + end + + def start_morse do + GenServer.call(__MODULE__, :start) + end + + @impl true + def init(state) do + {:ok, state} + end + + @impl true + def handle_call(:start, _from, :on) do + {:reply, {:error, :already_started}, :on} + end + + def handle_call(:start, _from, :off) do + spawn(MorseSignaler, :signal, [self()]) + {:reply, :ok, :on} + end + + @impl true + def handle_cast(:done, :on) do + {:noreply, :off} + end +end diff --git a/morse/lib/morse.ex b/morse/lib/morse_signaler.ex similarity index 78% rename from morse/lib/morse.ex rename to morse/lib/morse_signaler.ex index 9eb0c9f..3326858 100644 --- a/morse/lib/morse.ex +++ b/morse/lib/morse_signaler.ex @@ -1,4 +1,4 @@ -defmodule Morse do +defmodule MorseSignaler do alias Circuits.GPIO @moduledoc """ @@ -16,18 +16,17 @@ defmodule Morse do @doc """ Signal the provided symbols using GPIO. - Also setup the GPIO. + Notifies the parent when the signalling is done. """ - def signal do - signal(Application.fetch_env!(:morse, :morse_message)) - end - - def signal(symbols) do - {:ok, gpio} = GPIO.open(relay_pin(), :output) - GPIO.write(gpio, @off) + def signal(server_pid) do + # {:ok, gpio} = GPIO.open(relay_pin(), :output) + # GPIO.write(gpio, @off) Process.sleep(@sleep_start) - signal_sentence(gpio, String.graphemes(symbols)) + # signal_sentence(gpio, String.graphemes(secret_code())) + + GenServer.cast(server_pid, :done) + :ok end # Signal a whole sentence of symbols with GPIO. @@ -64,4 +63,8 @@ defmodule Morse do defp relay_pin() do Application.fetch_env!(:morse, :relay_pin) end + + defp secret_code do + signal(Application.fetch_env!(:morse, :morse_message)) + end end diff --git a/morse/mix.exs b/morse/mix.exs index 97e8ffb..e59828b 100644 --- a/morse/mix.exs +++ b/morse/mix.exs @@ -15,7 +15,8 @@ defmodule Morse.MixProject do def application do [ extra_applications: [:logger], - env: [morse_message: "...---...", relay_pin: 17] + env: [morse_message: "...---...", relay_pin: 17], + mod: {MorseServer, []} ] end diff --git a/ui/lib/ui_web/controllers/page_controller.ex b/ui/lib/ui_web/controllers/page_controller.ex index c7be9ba..6bb88f2 100644 --- a/ui/lib/ui_web/controllers/page_controller.ex +++ b/ui/lib/ui_web/controllers/page_controller.ex @@ -2,37 +2,24 @@ defmodule UiWeb.PageController do use UiWeb, :controller def index(conn, _params) do - conn - |> send_resp(201, "") + conn |> send_resp(201, "") end def instructions(conn, _params) do - conn - |> render(:instructions) + conn |> render(:instructions) end def morse(conn, _params) do - conn - |> render(:morse) + conn |> render(:morse) end def start(conn, _params) do - now = System.system_time(:second) + response = + case MorseServer.start_morse() do + :ok -> "Started." + {:error, :already_started} -> "The process is still in progress..." + end - case get_start_time() do - start_time when start_time + 35 <= now -> - System.put_env("MORSE_START_TIME", Integer.to_string(now)) - Morse.signal() - text(conn, "Done.") - _ -> - text(conn, "It is still in progress...") - end - end - - defp get_start_time() do - case System.get_env("MORSE_START_TIME") do - nil -> 0 - start_time -> String.to_integer(start_time) - end + conn |> text(response) end end