Port morse code.
This commit is contained in:
parent
cbec08aae8
commit
e93bbcf242
6 changed files with 67 additions and 7 deletions
|
@ -1,13 +1,67 @@
|
|||
defmodule Morse do
|
||||
alias Circuits.GPIO
|
||||
|
||||
@moduledoc """
|
||||
Functions to control the signal lamp connected with GPIO.
|
||||
"""
|
||||
|
||||
@relay_pin 17
|
||||
@sleep_short 200
|
||||
@sleep_delay 400
|
||||
@sleep_long 700
|
||||
@sleep_pause 1000
|
||||
@sleep_start 3000
|
||||
|
||||
@on 0
|
||||
@off 1
|
||||
|
||||
@doc """
|
||||
Takes a string composed of dots and dashes, and signals them with the
|
||||
signal lamp.
|
||||
Signal the provided symbols using GPIO.
|
||||
Also setup the GPIO.
|
||||
"""
|
||||
def signal(msg) do
|
||||
IO.puts(msg)
|
||||
|
||||
def signal do
|
||||
case System.get_env("MORSE_MESSAGE") do
|
||||
nil ->
|
||||
# Signal for SOS if the env variable wasn't found :)
|
||||
signal("...---...")
|
||||
message -> signal(message)
|
||||
end
|
||||
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}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue