Begin impl of firmware flasher RPC, add `clear_enigma` helper

pull/974/head
Rick Carlino 2019-03-20 11:59:21 -05:00 committed by Connor Rigby
parent 3bc84a3d3e
commit 17d24bdc0e
No known key found for this signature in database
GPG Key ID: 29A88B24B70456E0
4 changed files with 84 additions and 3 deletions

View File

@ -1,7 +1,8 @@
defmodule FarmbotCore.Asset.Private do
@moduledoc """
Private Assets are those that are internal to Farmbot that _are not_ stored in
the API, but _are_ stored in Farmbot's database.
Private Assets are those that are internal to
Farmbot that _are not_ stored in the API, but
_are_ stored in Farmbot's database.
"""
alias FarmbotCore.{Asset.Repo,
@ -18,6 +19,20 @@ defmodule FarmbotCore.Asset.Private do
|> Repo.insert()
end
@doc """
Clear in-system enigmas that match a particular
problem_tag.
"""
def clear_enigma(problem_tag) do
Repo.get_by(problem_tag: problem_tag)
|> case do
nil -> :ok
%Enigma{} = enigma ->
Repo.delete!(enigma)
:ok
end
end
@doc "Lists `module` objects that still need to be POSTed to the API."
def list_local(module) do
Repo.all(from(data in module, where: is_nil(data.id)))

View File

@ -81,7 +81,6 @@ defmodule FarmbotFirmware do
alias FarmbotFirmware, as: State
alias FarmbotFirmware.{GCODE, Command, Request}
@type status :: :boot | :no_config | :configuration | :idle | :emergency_lock
defstruct [
@ -145,6 +144,14 @@ defmodule FarmbotFirmware do
"""
defdelegate request(server \\ __MODULE__, code), to: Request
def close_transport(server \\ __MODULE__) do
_ = command(server, {nil, {:emergency_lock, []}})
GenServer.call(server, :close_transport)
end
def open_transport(server \\ __MODULE__) do
end
@doc """
Starting the Firmware server requires at least:
* `:transport` - a module implementing the Transport GenServer behaviour.
@ -220,6 +227,11 @@ defmodule FarmbotFirmware do
{:noreply, state}
end
def handle_call(:close_transport, _from, state) do
emergency_lock()
{:reply, :ok, state}
end
def handle_call({_tag, _code} = gcode, from, state) do
handle_command(gcode, from, state)
end

View File

@ -0,0 +1,22 @@
defmodule Avrdude do
@moduledoc """
It's AVR, my dudes.
"""
@uart_speed 115_200
def flash(hex_path, tty_path) do
# STEP 1: Is the UART in use?
args = [
"-patmega2560",
"-cwiring",
"-P#{tty_path}",
"-b#{@uart_speed}",
"-D",
"-V",
"-Uflash:w:#{hex_path}:i"
]
MuonTrap.cmd("avrdude", args, into: IO.stream(:stdio, :line))
end
end

View File

@ -2,6 +2,38 @@ defmodule FarmbotOS.SysCalls.FlashFirmware do
alias FarmbotFirmware
def flash_firmware(package) do
FarmbotCore.Asset.Private.clear_enigma("firmware.missing")
hex_file = find_hex_file(package)
tty = find_tty()
Avrdude.flash_firmware(hex_file, "?")
raise package
end
defp find_tty() do
File.ls("/dev/ttylol*")
end
defp find_hex_file("arduino") do
Application.app_dir(:farmbot_core, ["priv", "arduino_firmware.hex"]) |> assert_exists()
end
defp find_hex_file("farmduino") do
Application.app_dir(:farmbot_core, ["priv", "farmduino.hex"]) |> assert_exists()
end
defp find_hex_file("farmduino_k14") do
Application.app_dir(:farmbot_core, ["priv", "farmduino_k14.hex"]) |> assert_exists()
end
defp assert_exists(fname) do
if File.exists?(fname) do
fname
else
raise """
File does not exist: #{fname}
The arduino firmware is a git submodule to the farmbot project.
Please call `make arudino_firmware`.
"""
end
end
end