I2C Server

Hex.pm API docs CI

I2cServer is a thin wrapper of the Circuits.I2C library and creates a separate process for communicating with each I2C device. A I2C device process is stored in Registry and identified with a composite key of bus name and bus address.

Installation

Just add i2c_server to your list of dependencies in mix.exs:

def deps do
  [
    {:i2c_server, "~> 0.1"}
  ]
end

Usage

# Get a PID for a device at address 0x77 on "i2c-1" bus
iex> device1 = I2cServer.server_process("i2c-1", 0x77)
#PID<0.233.0>

# A different device has a different PID
iex> device2 = I2cServer.server_process("i2c-1", 0x38)
#PID<0.239.0>

# Write 0xff to register 0x8A
iex> I2cServer.write(device1, 0x8A, 0xff)
iex> I2cServer.write(device1, 0x8A, <<0xff>>)
iex> I2cServer.write(device1, <<0x8A, 0xff>>)
iex> I2cServer.write(device1, [0x8A, 0xff])
iex> I2cServer.write(device1, [0x8A, <<0xff>>])
:ok

# Read 3 bytes from register 0xE1
iex> I2cServer.write_read(device1, 0xE1, 3)
iex> I2cServer.write_read(device1, <<0xE1>>, 3)
{:ok, <<0, 0, 0>>}

Global registration

You can replace default registry with :global in your config file such as config/config.exs file.

config :i2c_server, registry_module: :global