Fix test failures.

This commit is contained in:
James Harton 2020-01-08 12:20:39 +13:00
parent a1b5513c11
commit c5f280315c
8 changed files with 59 additions and 48 deletions

View file

@ -55,14 +55,12 @@ defimpl Wafer.GPIO, for: Wafer.Driver.Circuits.GPIO do
case(Wrapper.read(ref)) do
value when is_pin_value(value) -> {:ok, value}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
def write(%{ref: ref} = conn, value) when is_reference(ref) and is_pin_value(value) do
case(Wrapper.write(ref, value)) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
with :ok <- Wrapper.write(ref, value), do: {:ok, conn}
end
def direction(%{direction: :in} = conn, :in), do: {:ok, conn}
@ -70,12 +68,9 @@ defimpl Wafer.GPIO, for: Wafer.Driver.Circuits.GPIO do
def direction(%{ref: ref} = conn, direction)
when is_reference(ref) and is_pin_direction(direction) do
pin_dir = String.to_atom(Enum.join([direction, "put"], ""))
case(Wrapper.set_direction(ref, pin_dir)) do
:ok -> {:ok, %{conn | direction: direction}}
{:error, reason} -> {:error, reason}
end
with pin_dir <- translate_pin_direction(direction),
:ok <- Wrapper.set_direction(ref, pin_dir),
do: {:ok, %{conn | direction: direction}}
end
def enable_interrupt(conn, pin_condition, metadata \\ nil)
@ -88,11 +83,11 @@ defimpl Wafer.GPIO, for: Wafer.Driver.Circuits.GPIO do
end
def pull_mode(%{ref: ref} = conn, mode) when is_reference(ref) and is_pin_pull_mode(mode) do
case Wrapper.set_pull_mode(ref, mode) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
with :ok <- Wrapper.set_pull_mode(ref, mode), do: {:ok, conn}
end
defp translate_pin_direction(:in), do: :input
defp translate_pin_direction(:out), do: :output
end
defimpl Wafer.DeviceID, for: Wafer.Driver.Circuits.GPIO do

View file

@ -57,10 +57,7 @@ defimpl Wafer.Chip, for: Wafer.Driver.Circuits.I2C do
def write_register(%{ref: ref, address: address} = conn, register_address, data)
when is_reference(ref) and is_i2c_address(address) and is_register_address(register_address) and
is_binary(data) do
case Wrapper.write(ref, address, <<register_address, data::binary>>) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
with :ok <- Wrapper.write(ref, address, <<register_address, data::binary>>), do: {:ok, conn}
end
def write_register(_conn, _register_address, _data), do: {:error, "Invalid argument"}
@ -82,23 +79,26 @@ defimpl Wafer.I2C, for: Wafer.Driver.Circuits.I2C do
def read(%{ref: ref, address: address}, bytes, options \\ [])
when is_reference(ref) and is_i2c_address(address) and is_byte_size(bytes) and
is_list(options),
do: Wrapper.read(ref, address, bytes, options)
is_list(options) do
case Wrapper.read(ref, address, bytes, options) do
{:ok, data} when is_binary(data) and byte_size(data) == bytes -> {:ok, data}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
def write(%{ref: ref, address: address} = conn, data, options \\ [])
when is_reference(ref) and is_i2c_address(address) and is_binary(data) and is_list(options) do
case Wrapper.write(ref, address, data, options) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
with :ok <- Wrapper.write(ref, address, data, options), do: {:ok, conn}
end
def write_read(%{ref: ref, address: address} = conn, data, bytes, options \\ [])
when is_reference(ref) and is_i2c_address(address) and is_binary(data) and
is_byte_size(bytes) and is_list(options) do
case Wrapper.write_read(ref, address, data, bytes, options) do
{:ok, data} -> {:ok, data, conn}
{:ok, data} when is_binary(data) and byte_size(data) == bytes -> {:ok, data, conn}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
@ -106,6 +106,7 @@ defimpl Wafer.I2C, for: Wafer.Driver.Circuits.I2C do
case Wrapper.detect_devices(ref) do
devices when is_list(devices) -> {:ok, devices}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
end

View file

@ -47,8 +47,14 @@ defimpl Wafer.SPI, for: Wafer.Driver.Circuits.SPI do
def transfer(%{ref: ref} = conn, data) when is_reference(ref) and is_binary(data) do
case Wrapper.transfer(ref, data) do
{:ok, data} -> {:ok, data, conn}
{:error, reason} -> {:error, reason}
{:ok, read_data} when is_binary(read_data) and byte_size(data) == byte_size(read_data) ->
{:ok, read_data, conn}
{:error, reason} ->
{:error, reason}
other ->
{:error, "Invalid response from driver: #{inspect(other)}"}
end
end
end

View file

@ -48,19 +48,18 @@ end
defimpl Wafer.GPIO, for: Wafer.Driver.ElixirALE.GPIO do
alias Wafer.Driver.ElixirALE.GPIO.Wrapper
alias Wafer.Driver.ElixirALE.GPIO.Dispatcher
import Wafer.Guards
def read(%{pid: pid} = _conn) do
case Wrapper.read(pid) do
value when value in [0, 1] -> {:ok, value}
value when is_pin_value(value) -> {:ok, value}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
def write(%{pid: pid} = conn, value) when value in [0, 1] do
case Wrapper.write(pid, value) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
def write(%{pid: pid} = conn, value) when is_pid(pid) and is_pin_value(value) do
with :ok <- Wrapper.write(pid, value), do: {:ok, conn}
end
def direction(_conn, _direction), do: {:error, :not_supported}

View file

@ -50,8 +50,9 @@ defimpl Wafer.Chip, for: Wafer.Driver.ElixirALE.I2C do
def read_register(%{pid: pid}, register_address, bytes)
when is_pid(pid) and is_register_address(register_address) and is_byte_size(bytes) do
case Wrapper.write_read(pid, <<register_address>>, bytes) do
data when is_binary(data) -> {:ok, data}
data when is_binary(data) and byte_size(data) == bytes -> {:ok, data}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
@ -59,10 +60,7 @@ defimpl Wafer.Chip, for: Wafer.Driver.ElixirALE.I2C do
def write_register(%{pid: pid} = conn, register_address, data)
when is_pid(pid) and is_register_address(register_address) and is_binary(data) do
case Wrapper.write(pid, <<register_address, data::binary>>) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
with :ok <- Wrapper.write(pid, <<register_address, data::binary>>), do: {:ok, conn}
end
def write_register(_conn, _register_address, _data), do: {:error, "Invalid argument"}
@ -85,24 +83,23 @@ defimpl Wafer.I2C, for: Wafer.Driver.ElixirALE.I2C do
def read(%{pid: pid}, bytes, options \\ [])
when is_pid(pid) and is_byte_size(bytes) and is_list(options) do
case Wrapper.read(pid, bytes) do
data when is_binary(data) -> {:ok, data}
data when is_binary(data) and byte_size(data) == bytes -> {:ok, data}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
def write(%{pid: pid} = conn, data, options \\ [])
when is_pid(pid) and is_binary(data) and is_list(options) do
case Wrapper.write(pid, data) do
:ok -> {:ok, conn}
{:error, reason} -> {:error, reason}
end
with :ok <- Wrapper.write(pid, data), do: {:ok, conn}
end
def write_read(%{pid: pid} = conn, data, bytes, options \\ [])
when is_pid(pid) and is_binary(data) and is_byte_size(bytes) and is_list(options) do
case Wrapper.write_read(pid, data, bytes) do
data when is_binary(data) -> {:ok, data, conn}
data when is_binary(data) and byte_size(data) == bytes -> {:ok, data, conn}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
@ -110,6 +107,7 @@ defimpl Wafer.I2C, for: Wafer.Driver.ElixirALE.I2C do
case Wrapper.detect_devices(pid) do
devices when is_list(devices) -> {:ok, devices}
{:error, reason} -> {:error, reason}
other -> {:error, "Invalid response from driver: #{inspect(other)}"}
end
end
end

View file

@ -48,8 +48,14 @@ defimpl Wafer.SPI, for: Wafer.Driver.ElixirALE.SPI do
def transfer(%{pid: pid} = conn, data) when is_pid(pid) and is_binary(data) do
case Wrapper.transfer(pid, data) do
data when is_binary(data) -> {:ok, data, conn}
{:error, reason} -> {:error, reason}
read_data when is_binary(read_data) and byte_size(read_data) == byte_size(data) ->
{:ok, read_data, conn}
{:error, reason} ->
{:error, reason}
other ->
{:error, "Invalid response from driver: #{inspect(other)}"}
end
end
end

View file

@ -73,12 +73,12 @@ defmodule WaferDriverCircuits.GPIOTest do
Wrapper
|> reject(:set_direction, 2)
assert {:ok, %Subject{} = conn} = Subject.acquire(pin: 1, direction: :out)
conn = conn(direction: :out)
assert {:ok, ^conn} = GPIO.direction(conn, :out)
end
test "when the direction is changing" do
assert {:ok, %Subject{} = conn} = Subject.acquire(pin: 1, direction: :out)
conn = conn(direction: :out)
Wrapper
|> expect(:set_direction, 1, fn ref, direction ->

View file

@ -3,6 +3,12 @@ defmodule WaferInterruptRegistryTest do
alias Wafer.InterruptRegistry, as: IR
@moduledoc false
setup do
Supervisor.terminate_child(Wafer.Supervisor, IR)
Supervisor.restart_child(Wafer.Supervisor, IR)
{:ok, []}
end
describe "subscribe/3" do
test "it subscibes the caller with `nil` metadata" do
self = self()