esrom/morse/lib/morse.ex

63 lines
1.3 KiB
Elixir
Raw Normal View History

2019-07-14 13:05:45 +00:00
defmodule Morse 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
@relay_pin 17
@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.
Also setup the GPIO.
2019-07-14 13:05:45 +00:00
"""
2019-07-14 15:23:37 +00:00
def signal do
signal(Application.fetch_env!(:morse, :morse_message))
2019-07-14 15:23:37 +00:00
end
def signal(symbols) do
{:ok, gpio} = GPIO.open(@relay_pin, :output)
GPIO.write(gpio, @off)
Process.sleep(@sleep_start)
signal_sentence(gpio, String.graphemes(symbols))
end
# Signal a whole sentence of symbols with GPIO.
defp signal_sentence(gpio, []) do
GPIO.write(gpio, @off)
GPIO.close(gpio)
:ok
end
defp signal_sentence(gpio, [symbol | rest]) when symbol in [".", "-"] do
GPIO.write(gpio, @on)
case symbol do
"." -> Process.sleep(@sleep_short)
"-" -> Process.sleep(@sleep_long)
end
GPIO.write(gpio, @off)
Process.sleep(@sleep_delay)
signal_sentence(gpio, rest)
end
defp signal_sentence(gpio, [" " | rest]) do
Process.sleep(@sleep_pause)
signal_sentence(gpio, rest)
end
defp signal_sentence(_gpio, [symbol | _rest]) do
{:error, "Undefined symbol: " <> symbol}
2019-07-14 13:05:45 +00:00
end
end