adafruit_bus_device.i2c_device - I2C Bus Device

class adafruit_bus_device.i2c_device.I2CDevice(i2c: I2C, device_address: int, probe: bool = True)[source]

Represents a single I2C device and manages locking the bus and the device address.

Parameters:
  • i2c (I2C) – The I2C bus the device is on

  • device_address (int) – The 7 bit device address

  • probe (bool) – Probe for the device upon object creation, default is true

Note

This class is NOT built into CircuitPython. See here for install instructions.

Example:

import busio
from board import *
from adafruit_bus_device.i2c_device import I2CDevice

with busio.I2C(SCL, SDA) as i2c:
    device = I2CDevice(i2c, 0x70)
    bytes_read = bytearray(4)
    with device:
        device.readinto(bytes_read)
    # A second transaction
    with device:
        device.write(bytes_read)
readinto(buf: array | bytearray | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray, *, start: int = 0, end: int | None = None) None[source]

Read into buf from the device. The number of bytes read will be the length of buf.

If start or end is provided, then the buffer will be sliced as if buf[start:end]. This will not cause an allocation like buf[start:end] will so it saves memory.

Parameters:
  • buffer (~WriteableBuffer) – buffer to write into

  • start (int) – Index to start writing at

  • end (int) – Index to write up to but not include; if None, use len(buf)

write(buf: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray, *, start: int = 0, end: int | None = None) None[source]

Write the bytes from buffer to the device, then transmit a stop bit.

If start or end is provided, then the buffer will be sliced as if buffer[start:end]. This will not cause an allocation like buffer[start:end] will so it saves memory.

Parameters:
  • buffer (~ReadableBuffer) – buffer containing the bytes to write

  • start (int) – Index to start writing from

  • end (int) – Index to read up to but not include; if None, use len(buf)

write_then_readinto(out_buffer: array | bytearray | bytes | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray, in_buffer: array | bytearray | memoryview | rgbmatrix.RGBMatrix | ulab.numpy.ndarray, *, out_start: int = 0, out_end: int | None = None, in_start: int = 0, in_end: int | None = None) None[source]

Write the bytes from out_buffer to the device, then immediately reads into in_buffer from the device. The number of bytes read will be the length of in_buffer.

If out_start or out_end is provided, then the output buffer will be sliced as if out_buffer[out_start:out_end]. This will not cause an allocation like buffer[out_start:out_end] will so it saves memory.

If in_start or in_end is provided, then the input buffer will be sliced as if in_buffer[in_start:in_end]. This will not cause an allocation like in_buffer[in_start:in_end] will so it saves memory.

Parameters:
  • out_buffer (~ReadableBuffer) – buffer containing the bytes to write

  • in_buffer (~WriteableBuffer) – buffer containing the bytes to read into

  • out_start (int) – Index to start writing from

  • out_end (int) – Index to read up to but not include; if None, use len(out_buffer)

  • in_start (int) – Index to start writing at

  • in_end (int) – Index to write up to but not include; if None, use len(in_buffer)

adafruit_bus_device.spi_device - SPI Bus Device

class adafruit_bus_device.spi_device.SPIDevice(spi: SPI, chip_select: DigitalInOut | None = None, *, cs_active_value: bool = False, baudrate: int = 100000, polarity: int = 0, phase: int = 0, extra_clocks: int = 0)[source]

Represents a single SPI device and manages locking the bus and the device address.

Parameters:
  • spi (SPI) – The SPI bus the device is on

  • chip_select (DigitalInOut) – The chip select pin object that implements the DigitalInOut API.

  • cs_active_value (bool) – Set to True if your device requires CS to be active high. Defaults to False.

  • baudrate (int) – The desired SCK clock rate in Hertz. The actual clock rate may be higher or lower due to the granularity of available clock settings (MCU dependent).

  • polarity (int) – The base state of the SCK clock pin (0 or 1).

  • phase (int) – The edge of the clock that data is captured. First (0) or second (1). Rising or falling depends on SCK clock polarity.

  • extra_clocks (int) – The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)

Note

This class is NOT built into CircuitPython. See here for install instructions.

Example:

import busio
import digitalio
from board import *
from adafruit_bus_device.spi_device import SPIDevice

with busio.SPI(SCK, MOSI, MISO) as spi_bus:
    cs = digitalio.DigitalInOut(D10)
    device = SPIDevice(spi_bus, cs)
    bytes_read = bytearray(4)
    # The object assigned to spi in the with statements below
    # is the original spi_bus object. We are using the busio.SPI
    # operations busio.SPI.readinto() and busio.SPI.write().
    with device as spi:
        spi.readinto(bytes_read)
    # A second transaction
    with device as spi:
        spi.write(bytes_read)