elixir_ale v0.7.0 ElixirALE.I2C
This module allows Elixir code to communicate with devices on an I2C bus.
Summary
Functions
Scan the I2C bus for devices by performing a read at each device address and returning a list of device addresses that respond
Return a list of available I2C bus device names. If nothing is returned,
it’s possible that the kernel driver for that I2C bus is not enabled or the
kernel’s device tree is not configured. On Raspbian, run raspi-config
and
look in the advanced options
Initiate a read transaction on the I2C bus of count
bytes
Initiate a read transaction to the device at the specified address
. This
is the same as read/2
except that an arbitrary device address may be given
Stop the GenServer and release all resources
Start and link the I2c GenServer
Write the specified data
to the device
Write the specified data
to the device at address
Write the specified data
to the device and then read
the specified number of bytes
Write the specified data
to the device and then read
the specified number of bytes. This is similar to write_read/3
except
with an I2C device address
Types
Functions
Scan the I2C bus for devices by performing a read at each device address and returning a list of device addresses that respond.
WARNING: This is intended to be a debugging aid. Reading bytes from devices can advance internal state machines and might cause them to get out of sync with other code.
iex> ElixirALE.I2C.detect_devices("i2c-1")
[4]
The return value is a list of device addresses that were detected on the
specified I2C bus. If you get back 'Hh'
or other letters, then IEx
converted the list to an Erlang string. Run i v()
to get information about
the return value and look at the raw string representation for addresses.
If you already have an ElixirALE.I2C
GenServer
running, then you may
pass its pid
to detect_devices/1
instead.
Return a list of available I2C bus device names. If nothing is returned,
it’s possible that the kernel driver for that I2C bus is not enabled or the
kernel’s device tree is not configured. On Raspbian, run raspi-config
and
look in the advanced options.
iex> ElixirALE.I2C.device_names
["i2c-1"]
Initiate a read transaction on the I2C bus of count
bytes.
Initiate a read transaction to the device at the specified address
. This
is the same as read/2
except that an arbitrary device address may be given.
Start and link the I2c GenServer.
devname
is the I2C bus name (e.g., “i2c-1”)
address
is the device’s 7-bit address on the I2C bus
Note that address
can be confusing when reading a datasheet
since sometimes the datasheet mentions the 8-bit address. For an 8-bit
address the least significant bit indicates whether the access is for a
read or a write. Microcontrollers like those on Arduinos often use the 8-bit
address. To convert an 8-bit address to a 7-bit one, divide the address by
two.
All calls to read/2
, write/2
, and write_read/3
access the device
specified by address
. Some I2C devices can be switched into different
modes where they respond to an alternate address. Rather than having to
create a second I2c
process, see read_device/3
and related routines.
If your application is interacting with many devices on the bus and
you’re only going to call read_device/3
, etc., then pass in any number
for the i2c_address
here.
Write the specified data
to the device at address
.
Write the specified data
to the device and then read
the specified number of bytes.
write_read_device(pid, i2c_address, binary, integer) :: binary | {:error, term}
Write the specified data
to the device and then read
the specified number of bytes. This is similar to write_read/3
except
with an I2C device address.