Add ability to stop the morse.
This commit is contained in:
parent
0e722b813e
commit
7ea4dd1752
5 changed files with 37 additions and 29 deletions
|
@ -5,8 +5,8 @@ defmodule Morse.Server do
|
|||
GenServer.start_link(__MODULE__, {nil, 0}, name: __MODULE__)
|
||||
end
|
||||
|
||||
def start_morse do
|
||||
GenServer.call(__MODULE__, :start)
|
||||
def toggle_morse do
|
||||
GenServer.call(__MODULE__, :toggle)
|
||||
end
|
||||
|
||||
def update_progress(progress) do
|
||||
|
@ -27,9 +27,11 @@ defmodule Morse.Server do
|
|||
end
|
||||
|
||||
@impl true
|
||||
def handle_call(:start, _from, {pid, _progress} = state) do
|
||||
def handle_call(:toggle, _from, {pid, _progress}) do
|
||||
if worker_alive?(pid) do
|
||||
{:reply, {:error, :already_started}, state}
|
||||
Morse.Worker.kill(pid)
|
||||
apply(pubsub(), :broadcast, [Ui.PubSub, "morse_progress", 0])
|
||||
{:reply, :ok, {nil, 0}}
|
||||
else
|
||||
pid = spawn(&Morse.Worker.signal/0)
|
||||
{:reply, :ok, {pid, 0}}
|
||||
|
|
|
@ -32,6 +32,11 @@ defmodule Morse.Worker do
|
|||
update_progress(100, 100)
|
||||
end
|
||||
|
||||
def kill(pid) do
|
||||
Process.exit(pid, :kill)
|
||||
toggle_lamp(@off)
|
||||
end
|
||||
|
||||
defp signal_symbol({?., _index}, _length) do
|
||||
toggle_lamp(@on)
|
||||
Process.sleep(@sleep_short)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue