Skip to content

Commit

Permalink
Update to Circuits.UART and Circuits.GPIO
Browse files Browse the repository at this point in the history
  • Loading branch information
connor rigby authored and ConnorRigby committed Apr 8, 2019
1 parent dfe01ae commit c559ef3
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/farmbot/firmware/stub_handler.ex
Expand Up @@ -189,7 +189,7 @@ defmodule Farmbot.Firmware.StubHandler do
end

def handle_call({:set_pin_mode, pin, mode}, _from, state) do
response = [:done]
response = [{:report_pin_mode, pin, mode}, :done]
{:reply, build_reply(:ok), response, state}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/farmbot/firmware/uart_handler/auto_detector.ex
Expand Up @@ -11,7 +11,7 @@ defmodule Farmbot.Firmware.UartHandler.AutoDetector do
* Configures Farmbot.Behaviour.FirmwareHandler -> StubHandler
"""

alias Nerves.UART
alias Circuits.UART
alias Farmbot.Firmware.{UartHandler, StubHandler, Utils}
import Utils
use Farmbot.Logger
Expand Down
2 changes: 1 addition & 1 deletion lib/farmbot/firmware/uart_handler/framing.ex
@@ -1,5 +1,5 @@
defmodule Farmbot.Firmware.UartHandler.Framing do
@behaviour Nerves.UART.Framing
@behaviour Circuits.UART.Framing
import Farmbot.Firmware.Gcode.Parser
use Farmbot.Logger

Expand Down
2 changes: 1 addition & 1 deletion lib/farmbot/firmware/uart_handler/uart_handler.ex
Expand Up @@ -4,7 +4,7 @@ defmodule Farmbot.Firmware.UartHandler do
"""

use GenStage
alias Nerves.UART
alias Circuits.UART
use Farmbot.Logger
alias Farmbot.System.ConfigStorage
import ConfigStorage, only: [update_config_value: 4, get_config_value: 3]
Expand Down
27 changes: 14 additions & 13 deletions lib/farmbot/firmware/uart_handler/update.ex
Expand Up @@ -4,6 +4,7 @@ defmodule Farmbot.Firmware.UartHandler.Update do
use Farmbot.Logger

@uart_speed 115_200
alias Circuits.UART

def maybe_update_firmware(hardware \\ nil) do
tty = Application.get_all_env(:farmbot)[:uart_handler][:tty]
Expand All @@ -30,15 +31,15 @@ defmodule Farmbot.Firmware.UartHandler.Update do
end

defp do_connect_and_maybe_update(tty, hardware) do
case Nerves.UART.start_link() do
case UART.start_link() do
{:ok, uart} ->
opts = [
active: true,
framing: {Nerves.UART.Framing.Line, separator: "\r\n"},
framing: {UART.Framing.Line, separator: "\r\n"},
speed: @uart_speed
]
:ok = Nerves.UART.open(uart, tty, [speed: @uart_speed])
:ok = Nerves.UART.configure(uart, opts)
:ok = UART.open(uart, tty, [speed: @uart_speed])
:ok = UART.configure(uart, opts)
Logger.busy 3, "Waiting for firmware idle report."
do_fw_loop(uart, tty, :idle, hardware)
close(uart)
Expand All @@ -60,7 +61,7 @@ defmodule Farmbot.Firmware.UartHandler.Update do
:version ->
Process.sleep(500)
# tell the FW to report its version.
Nerves.UART.write(uart, "F83")
UART.write(uart, "F83")
Logger.busy 3, "Waiting for firmware version report."
do_wait_version(uart, tty, hardware)
end
Expand Down Expand Up @@ -138,8 +139,8 @@ defmodule Farmbot.Firmware.UartHandler.Update do

defp close(uart) do
if Process.alive?(uart) do
close = Nerves.UART.close(uart)
stop = Nerves.UART.stop(uart)
close = UART.close(uart)
stop = UART.stop(uart)
Logger.info 3, "CLOSE: #{inspect close} STOP: #{stop}"
Process.sleep(2000) # to allow the FD to be closed.
end
Expand Down Expand Up @@ -169,14 +170,14 @@ defmodule Farmbot.Firmware.UartHandler.Update do
if Farmbot.Project.target() in [:rpi0, :rpi] do
@reset_pin 19
defp reset_init do
{:ok, pid} = ElixirALE.GPIO.start_link(@reset_pin, :output)
pid
{:ok, gpio} = Circuits.GPIO.open(@reset_pin, :output)
gpio
end
defp do_reset(pid) do
ElixirALE.GPIO.write(pid, 1)
ElixirALE.GPIO.write(pid, 0)
defp do_reset(gpio) do
Circuits.GPIO.write(gpio, 1)
Circuits.GPIO.write(gpio, 0)
end
defp reset_close(pid), do: ElixirALE.GPIO.release(pid)
defp reset_close(gpio), do: Circuits.GPIO.close(gpio)
else
defp reset_init(), do: nil
defp do_reset(nil), do: :ok
Expand Down
2 changes: 1 addition & 1 deletion lib/farmbot/logger/logger.ex
Expand Up @@ -7,7 +7,7 @@ defmodule Farmbot.Logger do
def how_many_logs do
alias IO.ANSI

count = LoggerBackendSqlite.count_logs()
count = LoggerBackendSqlite.all_logs() |> Enum.count()
size_mb = LoggerBackendSqlite.stat().size * 1.0e-6
total = Application.get_env(:logger, LoggerBackendSqlite)[:max_logs]

Expand Down
12 changes: 6 additions & 6 deletions mix.exs
Expand Up @@ -95,7 +95,7 @@ defmodule Farmbot.Mixfile do
{:jason, "~> 1.1"},
{:timex, "~> 3.4"},
{:fs, "~> 3.4"},
{:nerves_uart, "~> 1.2"},
{:circuits_uart, "~> 1.3"},
{:cowboy, "~> 2.5"},
{:plug, "~> 1.6"},
{:cors_plug, "~> 1.5"},
Expand Down Expand Up @@ -131,7 +131,7 @@ defmodule Farmbot.Mixfile do
{:nerves_network, "~> 0.5", targets: @all_targets},
{:nerves_wpa_supplicant, "~> 0.5.1", targets: @all_targets},
{:dhcp_server, "~> 0.7", targets: @all_targets},
{:elixir_ale, "~> 1.1", targets: @all_targets},
{:circuits_gpio, "~> 0.4.0", targets: @all_targets},
{:mdns, "~> 1.0", targets: @all_targets},

{:farmbot_system_rpi3, "1.6.3-farmbot.0", runtime: false, targets: :rpi3},
Expand All @@ -148,23 +148,23 @@ defmodule Farmbot.Mixfile do
]
end

defp elixirc_paths(:test, "host") do
defp elixirc_paths(:test, :host) do
["./lib", "./platform/host", "./test/support"]
end

defp elixirc_paths(_, "host") do
defp elixirc_paths(_, :host) do
["./lib", "./platform/host"]
end

defp elixirc_paths(_env, _target) do
["./lib", "./platform/target"]
end

defp aliases(:test, "host") do
defp aliases(:test, :host) do
[test: ["ecto.drop", "ecto.create --quiet", "ecto.migrate", "test"]]
end

defp aliases(_env, "host"),
defp aliases(_env, :host),
do: []

defp aliases(_env, _system) do
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Expand Up @@ -6,6 +6,7 @@
"bbmustache": {:hex, :bbmustache, "1.6.1", "9fb63fa60bd53afbf47f02e6d8bd6b2beafc068e02e20975254dc7461fd4f397", [:rebar3], [], "hexpm"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"circuits_uart": {:hex, :circuits_uart, "1.3.1", "8c0a56f06828133a0b08363ecb994350a9529d388a912357d31829ae617cd801", [:mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
"cors_plug": {:hex, :cors_plug, "1.5.2", "72df63c87e4f94112f458ce9d25800900cc88608c1078f0e4faddf20933eda6e", [:mix], [{:plug, "~> 1.3 or ~> 1.4 or ~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
16 changes: 8 additions & 8 deletions platform/target/gpio/leds_ale_handler.ex
@@ -1,5 +1,5 @@
defmodule Farmbot.Target.Leds.AleHandler do
alias ElixirALE.GPIO
alias Circuits.GPIO
@behaviour Farmbot.Leds.Handler

@slow_blink_speed 200
Expand Down Expand Up @@ -29,24 +29,24 @@ defmodule Farmbot.Target.Leds.AleHandler do

state =
Map.new(leds, fn color ->
{:ok, pid} = GPIO.start_link(color_to_pin(color), :output)
:ok = GPIO.write(pid, 0)
{color, %{pid: pid, status: :off, blink_timer: nil, state: 0}}
{:ok, gpio} = GPIO.open(color_to_pin(color), :output)
:ok = GPIO.write(gpio, 0)
{color, %{gpio: gpio, status: :off, blink_timer: nil, state: 0}}
end)

{:ok, state}
end

def handle_call({color, :off}, _from, state) do
r = GPIO.write(state[color].pid, 0)
r = GPIO.write(state[color].gpio, 0)
:ok = cancel_timer(state[color].blink_timer)

{:reply, r,
update_color(state, color, %{state[color] | state: 0, blink_timer: nil, status: :off})}
end

def handle_call({color, :solid}, _from, state) do
r = GPIO.write(state[color].pid, 1)
r = GPIO.write(state[color].gpio, 1)
:ok = cancel_timer(state[color].blink_timer)

{:reply, r,
Expand All @@ -72,14 +72,14 @@ defmodule Farmbot.Target.Leds.AleHandler do
case state[color] do
%{status: :slow_blink} ->
new_led_state = invert(state[color].state)
:ok = GPIO.write(state[color].pid, new_led_state)
:ok = GPIO.write(state[color].gpio, new_led_state)
timer = restart_timer(state[color].blink_timer, color, @slow_blink_speed)
n = %{state[color] | state: new_led_state, blink_timer: timer, status: :slow_blink}
update_color(state, color, n)

%{status: :fast_blink} ->
new_led_state = invert(state[color].state)
:ok = GPIO.write(state[color].pid, new_led_state)
:ok = GPIO.write(state[color].gpio, new_led_state)
timer = restart_timer(state[color].blink_timer, color, @fast_blink_speed)
n = %{state[color] | state: new_led_state, blink_timer: timer, status: :fast_blink}
update_color(state, color, n)
Expand Down
20 changes: 10 additions & 10 deletions platform/target/gpio/pin_binding_ale_handler.ex
Expand Up @@ -2,7 +2,7 @@ defmodule Farmbot.Target.PinBinding.AleHandler do
@moduledoc "PinBinding handler that uses Elixir.Ale"

use GenServer
alias ElixirALE.GPIO
alias Circuits.GPIO
@behaviour Farmbot.PinBinding.Handler

# PinBinding.Handler Callbacks
Expand All @@ -27,21 +27,21 @@ defmodule Farmbot.Target.PinBinding.AleHandler do

defmodule PinState do
@moduledoc false
defstruct [:pin, :state, :signal, :pid]
defstruct [:pin, :state, :signal, :gpio]
end

def init([]) do
{:ok, %State{pins: %{}}}
end

def handle_call({:register_pin, num}, _from, state) do
with {:ok, out_pid} <- GPIO.start_link(num, :output),
:ok <- GPIO.write(out_pid, 0),
:ok <- GPIO.release(out_pid),
{:ok, pid} <- GPIO.start_link(num, :input),
:ok <- GPIO.set_int(pid, :both),
with {:ok, out_gpio} <- GPIO.open(num, :output),
:ok <- GPIO.write(out_gpio, 0),
:ok <- GPIO.close(out_gpio),
{:ok, gpio} <- GPIO.start_link(num, :input),
:ok <- GPIO.set_int(gpio, :both),
new_pins <-
Map.put(state.pins, num, %PinState{pin: num, pid: pid, state: nil, signal: :rising}) do
Map.put(state.pins, num, %PinState{pin: num, gpio: gpio, state: nil, signal: :rising}) do
{:reply, :ok, %{state | pins: new_pins}}
else
{:error, _} = err -> {:reply, err, state}
Expand All @@ -54,8 +54,8 @@ defmodule Farmbot.Target.PinBinding.AleHandler do
nil ->
{:reply, :ok, state}

%PinState{pid: pid} ->
GPIO.release(pid)
%PinState{gpio: gpio} ->
GPIO.close(gpio)
{:reply, :ok, %{state | pins: Map.delete(state.pins, num)}}
end
end
Expand Down

0 comments on commit c559ef3

Please sign in to comment.