esrom/morse/lib/morse_server.ex
Pim Kunis 6a5a9eece7 Test if GPIO is alive using Process.alive?/1 instead of using
GenServer's state.
Fix signal sending twice because of copy-paste error.
2019-07-25 14:58:11 +02:00

34 lines
606 B
Elixir

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__, nil, 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, pid) do
cond do
pid == nil or not Process.alive?(pid) ->
pid = spawn(&MorseSignaler.signal/0)
{:reply, :ok, pid}
true ->
{:reply, {:error, :already_started}, pid}
end
end
end