Skip to content

Commit

Permalink
Make device a required argument to Reader.start_link (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
electricshaman authored Nov 6, 2017
1 parent 3124741 commit 9ffc4d9
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .iex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ defmodule Helpers do
Utils.to_hex_string(tag[:epc]) |> String.replace(" ", "")
end

def start_async_test(device \\ "/dev/ttyACM0", pulse_width \\ 100, period \\ 500, power \\ 2000) do
def start_async_test(device \\ "/dev/ttyACM0", on_time_ms \\ 100, off_time_ms \\ 400, power \\ 2000) do
Logger.configure(level: :info)

reader =
case Reader.start_link(device: device) do
case Reader.start_link(device) do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end

:ok = Reader.set_read_tx_power(reader, power)

{:ok, listener} = Task.start_link(&print_tags_async/0)
Reader.read_async_start(reader, listener, pulse_width, period)
Reader.read_async_start(reader, listener, on_time_ms, off_time_ms)
reader
end

Expand Down
15 changes: 7 additions & 8 deletions lib/tm_mercury/reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ defmodule TM.Mercury.Reader do
Start a process and open a connection
with the reader over UART connected via TTL / USB
## Options
The reader requires the following keys:
The reader requires the following parameters:
* `device` - the device file for the reader serial connection.
## Optional Parameters
The following keys are optional:
* `speed` - the serial port speed. Defaults to `115200`.
Expand Down Expand Up @@ -327,9 +327,8 @@ defmodule TM.Mercury.Reader do
* `:max_save`
* `:sleep`
"""
@spec start_link(keyword) :: {:ok, pid} | error
def start_link(opts) do
device = opts[:device]
@spec start_link(String.t, keyword) :: {:ok, pid} | error
def start_link(device, opts \\ []) do
name = Path.basename(device) |> String.to_atom
GenServer.start_link(__MODULE__, {device, opts}, name: name)
end
Expand All @@ -338,9 +337,9 @@ defmodule TM.Mercury.Reader do
Logger.debug "Starting RFID reader process for #{device} with pid #{inspect self()}"

# Pass this subset of options along to the transport process
ts_opts = Keyword.take(opts, [:device, :speed, :timeout, :framing])
ts_opts = Keyword.take(opts, [:speed, :timeout, :framing])

case Connection.start_link(Transport, {self(), ts_opts}) do
case Connection.start_link(Transport, {device, self(), ts_opts}) do
{:ok, ts} ->
new_reader = struct(%__MODULE__{}, opts)

Expand Down
4 changes: 2 additions & 2 deletions lib/tm_mercury/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ defmodule TM.Mercury.Transport do

# Connection API

def init({owner, opts}) do
def init({device, owner, opts}) do
opts = Keyword.merge(@defaults, opts)
{:ok, uart} = Nerves.UART.start_link

s = %{
device: opts[:device],
device: device,
opts: opts,
uart: uart,
owner: owner,
Expand Down
2 changes: 1 addition & 1 deletion test/reader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule TM.Mercury.ReaderTest do
alias TM.Mercury.{Reader, SimpleReadPlan, StopTriggerReadPlan}

setup_all do
{:ok, pid} = Reader.start_link(device: "/dev/ttyACM0", speed: 115200)
{:ok, pid} = Reader.start_link("/dev/ttyACM0", speed: 115200)
{:ok, %{pid: pid}}
end

Expand Down

0 comments on commit 9ffc4d9

Please sign in to comment.