Port morse code.

This commit is contained in:
Pim Kunis 2019-07-14 17:23:37 +02:00
parent cbec08aae8
commit e93bbcf242
6 changed files with 67 additions and 7 deletions

View file

@ -1,4 +1,5 @@
%{ %{
"circuits_gpio": {:hex, :circuits_gpio, "0.4.1", "344dd34f2517687fd28723e5552571babff5469db05b697181cab860fe7eff23", [:make, :mix], [{:elixir_make, "~> 0.5", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"}, "cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"}, "cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"},

View file

@ -1,13 +1,67 @@
defmodule Morse do defmodule Morse do
alias Circuits.GPIO
@moduledoc """ @moduledoc """
Functions to control the signal lamp connected with GPIO. 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 """ @doc """
Takes a string composed of dots and dashes, and signals them with the Signal the provided symbols using GPIO.
signal lamp. 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
end end

View file

@ -21,8 +21,7 @@ defmodule Morse.MixProject do
# Run "mix help deps" to learn about dependencies. # Run "mix help deps" to learn about dependencies.
defp deps do defp deps do
[ [
# {:dep_from_hexpm, "~> 0.3.0"}, {:circuits_gpio, "~> 0.4.1"}
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
] ]
end end
end end

4
morse/mix.lock Normal file
View file

@ -0,0 +1,4 @@
%{
"circuits_gpio": {:hex, :circuits_gpio, "0.4.1", "344dd34f2517687fd28723e5552571babff5469db05b697181cab860fe7eff23", [:make, :mix], [{:elixir_make, "~> 0.5", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.6.0", "38349f3e29aff4864352084fc736fa7fa0f2995a819a737554f7ebd28b85aaab", [:mix], [], "hexpm"},
}

View file

@ -22,7 +22,7 @@ defmodule UiWeb.PageController do
case get_start_time() do case get_start_time() do
start_time when start_time + 35 <= now -> start_time when start_time + 35 <= now ->
System.put_env("MORSE_START_TIME", Integer.to_string(now)) System.put_env("MORSE_START_TIME", Integer.to_string(now))
Morse.signal("...---...") Morse.signal()
text(conn, "Done.") text(conn, "Done.")
_ -> _ ->
text(conn, "It is still in progress...") text(conn, "It is still in progress...")

View file

@ -1,6 +1,8 @@
%{ %{
"circuits_gpio": {:hex, :circuits_gpio, "0.4.1", "344dd34f2517687fd28723e5552571babff5469db05b697181cab860fe7eff23", [:make, :mix], [{:elixir_make, "~> 0.5", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"},
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"}, "cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"}, "cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"},
"elixir_make": {:hex, :elixir_make, "0.6.0", "38349f3e29aff4864352084fc736fa7fa0f2995a819a737554f7ebd28b85aaab", [:mix], [], "hexpm"},
"file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"}, "file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"},
"gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"}, "gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"},
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},