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

i2c_address()
i2c_address() :: 0..127

Functions

detect_devices(pid_or_devname)
detect_devices(pid | binary) :: [integer] | {:error, term}

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.

device_names()
device_names() :: [binary]

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"]
read(pid, count)
read(pid, integer) :: binary | {:error, term}

Initiate a read transaction on the I2C bus of count bytes.

read_device(pid, address, count)
read_device(pid, i2c_address, integer) ::
  binary |
  {:error, term}

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.

release(pid)
release(pid) :: :ok

Stop the GenServer and release all resources.

start_link(devname, address, opts \\ [])
start_link(binary, i2c_address, [term]) :: {:ok, pid}

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(pid, data)
write(pid, binary) :: :ok | {:error, term}

Write the specified data to the device.

write_device(pid, address, data)
write_device(pid, i2c_address, binary) :: :ok | {:error, term}

Write the specified data to the device at address.

write_read(pid, write_data, read_count)
write_read(pid, binary, integer) :: binary | {:error, term}

Write the specified data to the device and then read the specified number of bytes.

write_read_device(pid, address, write_data, read_count)
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.