fetch relay pin from env

This commit is contained in:
Pim Kunis 2019-07-24 22:33:14 +02:00
parent 74bb465a38
commit ef38b81dd6
3 changed files with 10 additions and 3 deletions

View file

@ -5,7 +5,6 @@ defmodule Morse do
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_short 200
@sleep_delay 400 @sleep_delay 400
@sleep_long 700 @sleep_long 700
@ -25,7 +24,7 @@ defmodule Morse do
end end
def signal(symbols) do def signal(symbols) do
{:ok, gpio} = GPIO.open(@relay_pin, :output) {:ok, gpio} = GPIO.open(relay_pin(), :output)
GPIO.write(gpio, @off) GPIO.write(gpio, @off)
Process.sleep(@sleep_start) Process.sleep(@sleep_start)
signal_sentence(gpio, String.graphemes(symbols)) signal_sentence(gpio, String.graphemes(symbols))
@ -40,10 +39,12 @@ defmodule Morse do
defp signal_sentence(gpio, [symbol | rest]) when symbol in [".", "-"] do defp signal_sentence(gpio, [symbol | rest]) when symbol in [".", "-"] do
GPIO.write(gpio, @on) GPIO.write(gpio, @on)
case symbol do case symbol do
"." -> Process.sleep(@sleep_short) "." -> Process.sleep(@sleep_short)
"-" -> Process.sleep(@sleep_long) "-" -> Process.sleep(@sleep_long)
end end
GPIO.write(gpio, @off) GPIO.write(gpio, @off)
Process.sleep(@sleep_delay) Process.sleep(@sleep_delay)
@ -59,4 +60,8 @@ defmodule Morse do
defp signal_sentence(_gpio, [symbol | _rest]) do defp signal_sentence(_gpio, [symbol | _rest]) do
{:error, "Undefined symbol: " <> symbol} {:error, "Undefined symbol: " <> symbol}
end end
defp relay_pin() do
Application.fetch_env!(:morse, :relay_pin)
end
end end

View file

@ -15,7 +15,7 @@ defmodule Morse.MixProject do
def application do def application do
[ [
extra_applications: [:logger], extra_applications: [:logger],
env: [morse_message: "...---..."] env: [morse_message: "...---...", relay_pin: 17]
] ]
end end

View file

@ -22,6 +22,8 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix # Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason config :phoenix, :json_library, Jason
config :morse, :relay_pin, 17
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs" import_config "#{Mix.env()}.exs"