2019-07-24 21:56:42 +00:00
|
|
|
defmodule MorseSignaler do
|
2019-07-14 15:23:37 +00:00
|
|
|
alias Circuits.GPIO
|
|
|
|
|
2019-07-14 13:05:45 +00:00
|
|
|
@moduledoc """
|
|
|
|
Functions to control the signal lamp connected with GPIO.
|
|
|
|
"""
|
|
|
|
|
2019-07-14 15:23:37 +00:00
|
|
|
@sleep_short 200
|
|
|
|
@sleep_delay 400
|
|
|
|
@sleep_long 700
|
|
|
|
@sleep_pause 1000
|
|
|
|
@sleep_start 3000
|
|
|
|
|
|
|
|
@on 0
|
|
|
|
@off 1
|
|
|
|
|
2019-07-14 13:05:45 +00:00
|
|
|
@doc """
|
2019-07-14 15:23:37 +00:00
|
|
|
Signal the provided symbols using GPIO.
|
2019-07-24 21:56:42 +00:00
|
|
|
Notifies the parent when the signalling is done.
|
2019-07-14 13:05:45 +00:00
|
|
|
"""
|
2019-07-14 15:23:37 +00:00
|
|
|
|
2019-07-25 12:58:11 +00:00
|
|
|
def signal() do
|
2019-07-24 22:00:13 +00:00
|
|
|
{:ok, gpio} = GPIO.open(relay_pin(), :output)
|
|
|
|
GPIO.write(gpio, @off)
|
2019-07-14 15:23:37 +00:00
|
|
|
Process.sleep(@sleep_start)
|
2019-07-30 22:20:09 +00:00
|
|
|
update_progress(gpio, String.graphemes(secret_code()))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update progress for clients, and signals the rest of the sentence.
|
|
|
|
defp update_progress(gpio, symbols) do
|
|
|
|
100 - length(symbols) / String.length(secret_code()) * 100
|
|
|
|
|> MorseServer.update_progress()
|
|
|
|
if symbols != [] do
|
|
|
|
signal_sentence(gpio, symbols)
|
|
|
|
end
|
2019-07-14 15:23:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Signal a whole sentence of symbols with GPIO.
|
|
|
|
defp signal_sentence(gpio, []) do
|
|
|
|
GPIO.write(gpio, @off)
|
|
|
|
GPIO.close(gpio)
|
2019-07-30 22:20:09 +00:00
|
|
|
update_progress(gpio, [])
|
2019-07-14 15:23:37 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
defp signal_sentence(gpio, [symbol | rest]) when symbol in [".", "-"] do
|
|
|
|
GPIO.write(gpio, @on)
|
2019-07-24 20:33:14 +00:00
|
|
|
|
2019-07-14 15:23:37 +00:00
|
|
|
case symbol do
|
|
|
|
"." -> Process.sleep(@sleep_short)
|
|
|
|
"-" -> Process.sleep(@sleep_long)
|
|
|
|
end
|
2019-07-24 20:33:14 +00:00
|
|
|
|
2019-07-14 15:23:37 +00:00
|
|
|
GPIO.write(gpio, @off)
|
|
|
|
|
|
|
|
Process.sleep(@sleep_delay)
|
|
|
|
signal_sentence(gpio, rest)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp signal_sentence(gpio, [" " | rest]) do
|
|
|
|
Process.sleep(@sleep_pause)
|
|
|
|
|
2019-07-30 22:20:09 +00:00
|
|
|
update_progress(gpio, rest)
|
2019-07-14 15:23:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp signal_sentence(_gpio, [symbol | _rest]) do
|
|
|
|
{:error, "Undefined symbol: " <> symbol}
|
2019-07-14 13:05:45 +00:00
|
|
|
end
|
2019-07-24 20:33:14 +00:00
|
|
|
|
|
|
|
defp relay_pin() do
|
|
|
|
Application.fetch_env!(:morse, :relay_pin)
|
|
|
|
end
|
2019-07-24 21:56:42 +00:00
|
|
|
|
|
|
|
defp secret_code do
|
2019-07-25 12:58:11 +00:00
|
|
|
Application.fetch_env!(:morse, :morse_message)
|
2019-07-24 21:56:42 +00:00
|
|
|
end
|
2019-07-14 13:05:45 +00:00
|
|
|
end
|