Execute GPIO functions on the production server.

This commit is contained in:
Pim Kunis 2019-08-24 16:51:43 +02:00
parent fc54fa1761
commit 66291ac548
4 changed files with 33 additions and 23 deletions

View file

@ -21,11 +21,10 @@ defmodule Firmware.Application do
# List all child processes to be supervised
def children(:host) do
[
# Children that only run on the host
# Starts a worker by calling: Firmware.Worker.start_link(arg)
# {Firmware.Worker, arg},
]
{:ok, _} = Node.start(:"host@0.0.0.0")
Node.set_cookie(:testcookie)
true = Node.connect(:"esrom@nerves.local")
[]
end
def children(_target) do

View file

@ -63,7 +63,7 @@ defmodule Firmware.MixProject do
def release do
[
overwrite: true,
cookie: "#{@app}_cookie",
cookie: "testcookie",
include_erts: &Nerves.Release.erts/0,
steps: [&Nerves.Release.init/1, :assemble]
]

View file

@ -1,6 +1,4 @@
defmodule Morse.Worker do
alias Circuits.GPIO
@moduledoc """
Functions to control the signal lamp connected with GPIO.
"""
@ -24,32 +22,31 @@ defmodule Morse.Worker do
update_progress(0, 100)
{:ok, gpio} = GPIO.open(relay_pin(), :output)
GPIO.write(gpio, @off)
toggle_lamp(@off)
Process.sleep(@sleep_start)
code
|> Enum.with_index()
|> Enum.each(&signal_symbol(gpio, &1, code_length))
|> Enum.each(&signal_symbol(&1, code_length))
update_progress(100, 100)
end
defp signal_symbol(gpio, {?., _index}, _length) do
GPIO.write(gpio, @on)
defp signal_symbol({?., _index}, _length) do
toggle_lamp(@on)
Process.sleep(@sleep_short)
GPIO.write(gpio, @off)
toggle_lamp(@off)
Process.sleep(@sleep_delay)
end
defp signal_symbol(gpio, {?-, _index}, _length) do
GPIO.write(gpio, @on)
defp signal_symbol({?-, _index}, _length) do
toggle_lamp(@on)
Process.sleep(@sleep_long)
GPIO.write(gpio, @off)
toggle_lamp(@off)
Process.sleep(@sleep_delay)
end
defp signal_symbol(_gpio, {?\s, index}, length) do
defp signal_symbol({?\s, index}, length) do
Process.sleep(@sleep_pause)
update_progress(index, length)
end
@ -58,12 +55,27 @@ defmodule Morse.Worker do
Morse.Server.update_progress(index / length * 100)
end
defp relay_pin() do
Application.fetch_env!(:morse, :relay_pin)
end
defp secret_code do
Application.fetch_env!(:morse, :morse_message)
|> String.to_charlist()
end
case Mix.Nerves.Utils.mix_target() do
:host ->
def toggle_lamp(state) do
:rpc.call(:"esrom@nerves.local", Morse.Worker, :toggle_lamp, [state])
end
_ ->
alias Circuits.GPIO
def toggle_lamp(state) do
{:ok, gpio} = GPIO.open(relay_pin(), :output)
GPIO.write(gpio, state)
end
defp relay_pin() do
Application.fetch_env!(:morse, :relay_pin)
end
end
end

View file

@ -32,7 +32,6 @@ defmodule UiWeb.UserSocket do
def id(_socket), do: nil
def broadcast_progress(progress) do
IO.puts("in user socket!")
UiWeb.Endpoint.broadcast("morse:progress", "update", %{value: progress})
end
end