Read and write mailbox bytes separately.

This commit is contained in:
James Harton 2020-01-22 14:48:24 +13:00
parent 5b5110a4d1
commit caaaff3571
3 changed files with 23 additions and 10 deletions

View file

@ -44,6 +44,7 @@ defmodule PCA9641 do
:bus_hung | :mbox_full | :mbox_empty | :test_int | :lock_grant | :bus_lost | :int_in
@type t :: %PCA9641{conn: Conn.t(), int_pin: Conn.t()}
@type mailbox_data :: <<_::16>>
@type acquire_options :: [acquire_option]
@type acquire_option :: {:conn, Conn.t(), int_pin: Conn.t()}
@ -971,17 +972,21 @@ defmodule PCA9641 do
@doc """
Read shared mailbox.
"""
@spec read_mailbox(t) :: {:ok, binary, t} | {:error, term}
@spec read_mailbox(t) :: {:ok, mailbox_data, t} | {:error, term}
def read_mailbox(%PCA9641{conn: conn} = dev) do
with {:ok, mailbox} <- Registers.read_mailbox(conn), do: {:ok, mailbox, %{dev | conn: conn}}
with {:ok, <<lsb>>} <- Registers.read_mailbox_lsb(conn),
{:ok, <<msb>>} <- Registers.read_mailbox_msb(conn),
do: {:ok, <<msb, lsb>>, %{dev | conn: conn}}
end
@doc """
Write shared mailbox.
"""
@spec write_mailbox(pid, binary) :: :ok | {:error, term}
def write_mailbox(%PCA9641{conn: conn} = dev, data) when byte_size(data) == 2 do
with {:ok, conn} <- Registers.write_mailbox(conn, data), do: {:ok, %{dev | conn: conn}}
@spec write_mailbox(pid, mailbox_data) :: :ok | {:error, term}
def write_mailbox(%PCA9641{conn: conn} = dev, <<msb, lsb>>) do
with {:ok, conn} <- Registers.write_mailbox_lsb(conn, <<lsb>>),
{:ok, conn} <- Registers.write_mailbox_msb(conn, <<msb>>),
do: {:ok, %{dev | conn: conn}}
end
@doc """

View file

@ -12,5 +12,6 @@ defmodule PCA9641.Registers do
defregister(:reserve_time, 0x03, :rw, 1)
defregister(:interrupt_status, 0x04, :rw, 1)
defregister(:interrupt_mask, 0x05, :rw, 1)
defregister(:mailbox, 0x06, :rw, 2)
defregister(:mailbox_lsb, 0x06, :rw, 1)
defregister(:mailbox_msb, 0x07, :rw, 1)
end

View file

@ -892,8 +892,11 @@ defmodule PCA9641Test do
describe "read_mailbox/1" do
test "returns the contents of the MAILBOX register" do
Registers
|> expect(:read_mailbox, 1, fn _conn ->
{:ok, <<0x1, 0x2>>}
|> expect(:read_mailbox_msb, 1, fn _conn ->
{:ok, <<0x1>>}
end)
|> expect(:read_mailbox_lsb, 1, fn _conn ->
{:ok, <<0x2>>}
end)
assert {:ok, <<0x1, 0x2>>, _conn} = PCA9641.read_mailbox(conn())
@ -903,8 +906,12 @@ defmodule PCA9641Test do
describe "write_mailbox/2" do
test "writes the contents of the MAILBOX register" do
Registers
|> expect(:write_mailbox, 1, fn conn, data ->
assert <<0x1, 0x2>> == data
|> expect(:write_mailbox_msb, 1, fn conn, data ->
assert <<0x1>> == data
{:ok, conn}
end)
|> expect(:write_mailbox_lsb, 1, fn conn, data ->
assert <<0x2>> == data
{:ok, conn}
end)