I2C Server wraps Circuits.I2C
reference in a GenServer
, creating a separate
process per I2C bus. I2C bus processes are
identified with a bus name (e.g., "i2c-1"
).
Just add i2c_server
to your list of dependencies in mix.exs
:
def deps do
[
{:i2c_server, "~> 0.2"}
]
end
# Get a PID for the device at the address 0x77 on the "i2c-1" bus
iex> {:ok, device} = I2cServer.start_link(bus_name: "i2c-1", bus_address: 0x77)
#PID<0.233.0>
# Write 0xff to the register 0x8A
iex> I2cServer.write(device, [0x8A, <<0xff>>])
:ok
# Read 3 bytes
iex> I2cServer.read(device, 3)
:ok
# Read 3 bytes from the register 0xE1
iex> I2cServer.write_read(device, 0xE1, 3)
{:ok, <<0, 0, 0>>}
# Do multiple operations sequentially blocking the bus worker process
iex> I2cServer.bulk(device, [
...> {:write, [0xBA]},
...> {:sleep, 10},
...> {:write, [0xAC, <<0x33, 0x00>>]},
...> {Process, :sleep, [10]},
...> fn(_) -> "something" end
...> ])
[:ok, :ok, :ok, :ok, "something"]
I2C bus processes will be created under I2cServer.I2cBusSupervisor
dynamically.
You can change settings in your config file such as config/config.exs
file.
By default, I2C bus processes are
stored in Registry
, but you can alternatively use
:global
.
config :i2c_server,
bus_registry_module: :global